Fix #5195 (cbr -> epub runs out of memory when coverting massive files ;-))

This commit is contained in:
Kovid Goyal 2010-04-13 08:35:52 +05:30
parent 7cdf57969c
commit f25d0f1ef2
2 changed files with 22 additions and 2 deletions

View File

@ -1048,6 +1048,22 @@ class Manifest(object):
self._data = None self._data = None
return property(fget, fset, fdel, doc=doc) return property(fget, fset, fdel, doc=doc)
def unload_data_from_memory(self):
if isinstance(self._data, (str, bytes)):
from calibre.ptempfile import PersistentTemporaryFile
pt = PersistentTemporaryFile('_oeb_base_mem_unloader')
pt.write(self._data)
pt.close()
def loader(*args):
with open(pt.name, 'rb') as f:
ans = f.read()
os.remove(pt.name)
return ans
self._loader = loader
self._data = None
def __str__(self): def __str__(self):
data = self.data data = self.data
if isinstance(data, etree._Element): if isinstance(data, etree._Element):

View File

@ -56,17 +56,21 @@ class RescaleImages(object):
scaled, new_width, new_height = fit_image(width, height, scaled, new_width, new_height = fit_image(width, height,
page_width, page_height) page_width, page_height)
if scaled: if scaled:
data = None
self.log('Rescaling image from %dx%d to %dx%d'%( self.log('Rescaling image from %dx%d to %dx%d'%(
width, height, new_width, new_height), item.href) width, height, new_width, new_height), item.href)
if qt: if qt:
img = img.scaled(new_width, new_height, img = img.scaled(new_width, new_height,
Qt.IgnoreAspectRatio, Qt.SmoothTransformation) Qt.IgnoreAspectRatio, Qt.SmoothTransformation)
item.data = pixmap_to_data(img) data = pixmap_to_data(img)
else: else:
im = im.resize((int(new_width), int(new_height)), PILImage.ANTIALIAS) im = im.resize((int(new_width), int(new_height)), PILImage.ANTIALIAS)
of = cStringIO.StringIO() of = cStringIO.StringIO()
im.convert('RGB').save(of, 'JPEG') im.convert('RGB').save(of, 'JPEG')
item.data = of.getvalue() data = of.getvalue()
if data is not None:
item.data = data
item.unload_data_from_memory()