2
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
3
from __future__ import with_statement
6
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
7
__docformat__ = 'restructuredtext en'
9
from calibre import fit_image
11
class RescaleImages(object):
12
'Rescale all images to fit inside given screen size'
14
def __call__(self, oeb, opts):
15
from PyQt4.Qt import QApplication, QImage, Qt
16
from calibre.gui2 import pixmap_to_data
17
self.oeb, self.opts, self.log = oeb, opts, oeb.log
18
page_width, page_height = opts.dest.width, opts.dest.height
19
for item in oeb.manifest:
20
if item.media_type.startswith('image'):
23
if QApplication.instance() is None:
26
img = QImage(10, 10, QImage.Format_ARGB32_Premultiplied)
27
if not img.loadFromData(raw): continue
28
width, height = img.width(), img.height()
29
scaled, new_width, new_height = fit_image(width, height,
30
page_width, page_height)
32
self.log('Rescaling image', item.href)
33
img = img.scaled(new_width, new_height,
34
Qt.IgnoreAspectRatio, Qt.SmoothTransformation)
35
item.data = pixmap_to_data(img)