home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1010 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  3.0 KB  |  98 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from __future__ import with_statement
  5. __license__ = 'GPL v3'
  6. __copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
  7. __docformat__ = 'restructuredtext en'
  8. import cStringIO
  9. from calibre import fit_image
  10.  
  11. class RescaleImages(object):
  12.     
  13.     def __call__(self, oeb, opts):
  14.         self.oeb = oeb
  15.         self.opts = opts
  16.         self.log = oeb.log
  17.         is_ok_to_use_qt = is_ok_to_use_qt
  18.         import calibre.gui2
  19.         self.rescale(qt = is_ok_to_use_qt())
  20.  
  21.     
  22.     def rescale(self, qt = True):
  23.         QImage = QImage
  24.         Qt = Qt
  25.         import PyQt4.Qt
  26.         pixmap_to_data = pixmap_to_data
  27.         import calibre.gui2
  28.         
  29.         try:
  30.             PILImage = Image
  31.             import PIL
  32.             PILImage
  33.         except ImportError:
  34.             import Image as PILImage
  35.  
  36.         is_image_collection = getattr(self.opts, 'is_image_collection', False)
  37.         if is_image_collection:
  38.             (page_width, page_height) = self.opts.dest.comic_screen_size
  39.         else:
  40.             page_width = self.opts.dest.width
  41.             page_height = self.opts.dest.height
  42.             page_width -= (self.opts.margin_left + self.opts.margin_right) * self.opts.dest.dpi / 72
  43.             page_height -= (self.opts.margin_top + self.opts.margin_bottom) * self.opts.dest.dpi / 72
  44.         for item in self.oeb.manifest:
  45.             if item.media_type.startswith('image'):
  46.                 ext = item.media_type.split('/')[-1].upper()
  47.                 if ext == 'JPG':
  48.                     ext = 'JPEG'
  49.                 
  50.                 if ext not in ('PNG', 'JPEG'):
  51.                     ext = 'JPEG'
  52.                 
  53.                 raw = item.data
  54.                 if not raw:
  55.                     continue
  56.                 
  57.                 if qt:
  58.                     img = QImage(10, 10, QImage.Format_ARGB32_Premultiplied)
  59.                     
  60.                     try:
  61.                         if not img.loadFromData(raw):
  62.                             continue
  63.                     except:
  64.                         continue
  65.  
  66.                     width = img.width()
  67.                     height = img.height()
  68.                 else:
  69.                     f = cStringIO.StringIO(raw)
  70.                     
  71.                     try:
  72.                         im = PILImage.open(f)
  73.                     except IOError:
  74.                         continue
  75.  
  76.                     (width, height) = im.size
  77.                 (scaled, new_width, new_height) = fit_image(width, height, page_width, page_height)
  78.                 if scaled:
  79.                     data = None
  80.                     self.log('Rescaling image from %dx%d to %dx%d' % (width, height, new_width, new_height), item.href)
  81.                     if qt:
  82.                         img = img.scaled(new_width, new_height, Qt.IgnoreAspectRatio, Qt.SmoothTransformation)
  83.                         data = pixmap_to_data(img, format = ext)
  84.                     else:
  85.                         im = im.resize((int(new_width), int(new_height)), PILImage.ANTIALIAS)
  86.                         of = cStringIO.StringIO()
  87.                         im.convert('RGB').save(of, ext)
  88.                         data = of.getvalue()
  89.                     if data is not None:
  90.                         item.data = data
  91.                         item.unload_data_from_memory()
  92.                     
  93.                 
  94.             scaled
  95.         
  96.  
  97.  
  98.