home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1008 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  2.4 KB  |  70 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. from calibre import fit_image
  9.  
  10. class RescaleImages(object):
  11.     
  12.     def __call__(self, oeb, opts):
  13.         self.oeb = oeb
  14.         self.opts = opts
  15.         self.log = oeb.log
  16.         is_ok_to_use_qt = is_ok_to_use_qt
  17.         import calibre.gui2
  18.         self.rescale(qt = is_ok_to_use_qt())
  19.  
  20.     
  21.     def rescale(self, qt = True):
  22.         Image = Image
  23.         import calibre.utils.magick.draw
  24.         is_image_collection = getattr(self.opts, 'is_image_collection', False)
  25.         if is_image_collection:
  26.             (page_width, page_height) = self.opts.dest.comic_screen_size
  27.         else:
  28.             page_width = self.opts.dest.width
  29.             page_height = self.opts.dest.height
  30.             page_width -= (self.opts.margin_left + self.opts.margin_right) * self.opts.dest.dpi / 72
  31.             page_height -= (self.opts.margin_top + self.opts.margin_bottom) * self.opts.dest.dpi / 72
  32.         for item in self.oeb.manifest:
  33.             if item.media_type.startswith('image'):
  34.                 ext = item.media_type.split('/')[-1].upper()
  35.                 if ext == 'JPG':
  36.                     ext = 'JPEG'
  37.                 
  38.                 if ext not in ('PNG', 'JPEG', 'GIF'):
  39.                     ext = 'JPEG'
  40.                 
  41.                 raw = item.data
  42.                 if not raw:
  43.                     continue
  44.                 
  45.                 
  46.                 try:
  47.                     img = Image()
  48.                     img.load(raw)
  49.                 except:
  50.                     continue
  51.  
  52.                 (width, height) = img.size
  53.                 (scaled, new_width, new_height) = fit_image(width, height, page_width, page_height)
  54.                 if scaled:
  55.                     self.log('Rescaling image from %dx%d to %dx%d' % (width, height, new_width, new_height), item.href)
  56.                     
  57.                     try:
  58.                         img.size = (new_width, new_height)
  59.                         data = img.export(ext.lower())
  60.                     except:
  61.                         self.log.exception('Failed to rescale image')
  62.  
  63.                     item.data = data
  64.                     item.unload_data_from_memory()
  65.                 
  66.             scaled
  67.         
  68.  
  69.  
  70.