mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-08 10:44:09 -04:00
Fix #7306 (Rescaled GIFs are 0 bytes)
This commit is contained in:
parent
50151bca5e
commit
19d3a38ee9
@ -6,8 +6,6 @@ __license__ = 'GPL v3'
|
||||
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
|
||||
__docformat__ = 'restructuredtext en'
|
||||
|
||||
import cStringIO
|
||||
|
||||
from calibre import fit_image
|
||||
|
||||
class RescaleImages(object):
|
||||
@ -19,13 +17,7 @@ class RescaleImages(object):
|
||||
self.rescale(qt=is_ok_to_use_qt())
|
||||
|
||||
def rescale(self, qt=True):
|
||||
from PyQt4.Qt import QImage, Qt
|
||||
from calibre.gui2 import pixmap_to_data
|
||||
try:
|
||||
from PIL import Image as PILImage
|
||||
PILImage
|
||||
except ImportError:
|
||||
import Image as PILImage
|
||||
from calibre.utils.magick.draw import Image
|
||||
|
||||
is_image_collection = getattr(self.opts, 'is_image_collection', False)
|
||||
|
||||
@ -35,6 +27,7 @@ class RescaleImages(object):
|
||||
page_width, page_height = self.opts.dest.width, self.opts.dest.height
|
||||
page_width -= (self.opts.margin_left + self.opts.margin_right) * self.opts.dest.dpi/72.
|
||||
page_height -= (self.opts.margin_top + self.opts.margin_bottom) * self.opts.dest.dpi/72.
|
||||
|
||||
for item in self.oeb.manifest:
|
||||
if item.media_type.startswith('image'):
|
||||
ext = item.media_type.split('/')[-1].upper()
|
||||
@ -44,42 +37,25 @@ class RescaleImages(object):
|
||||
|
||||
raw = item.data
|
||||
if not raw: continue
|
||||
if qt:
|
||||
img = QImage(10, 10, QImage.Format_ARGB32_Premultiplied)
|
||||
try:
|
||||
if not img.loadFromData(raw): continue
|
||||
except:
|
||||
continue
|
||||
width, height = img.width(), img.height()
|
||||
else:
|
||||
f = cStringIO.StringIO(raw)
|
||||
try:
|
||||
im = PILImage.open(f)
|
||||
except IOError:
|
||||
continue
|
||||
width, height = im.size
|
||||
|
||||
try:
|
||||
img = Image()
|
||||
img.load(raw)
|
||||
except:
|
||||
continue
|
||||
width, height = img.size
|
||||
|
||||
|
||||
scaled, new_width, new_height = fit_image(width, height,
|
||||
page_width, page_height)
|
||||
if scaled:
|
||||
data = None
|
||||
self.log('Rescaling image from %dx%d to %dx%d'%(
|
||||
width, height, new_width, new_height), item.href)
|
||||
if qt:
|
||||
img = img.scaled(new_width, new_height,
|
||||
Qt.IgnoreAspectRatio, Qt.SmoothTransformation)
|
||||
data = pixmap_to_data(img, format=ext)
|
||||
try:
|
||||
img.size = (new_width, new_height)
|
||||
data = img.export(ext.lower())
|
||||
except:
|
||||
self.log.exception('Failed to rescale image')
|
||||
else:
|
||||
try:
|
||||
im = im.resize((int(new_width), int(new_height)), PILImage.ANTIALIAS)
|
||||
of = cStringIO.StringIO()
|
||||
im.convert('RGB').save(of, ext)
|
||||
data = of.getvalue()
|
||||
except:
|
||||
self.log.exception('Failed to rescale image')
|
||||
if data is not None:
|
||||
item.data = data
|
||||
item.unload_data_from_memory()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user