Commits

Anonymous committed 1ea93b3

Added epub_max_image_width configuration option.

Comments (0)

Files changed (5)

     '_static/jquery.js', '_static/searchtools.js', '_static/underscore.js',
     '_static/basic.css', 'search.html']
 epub_fix_images = False
+epub_max_image_width = 0
 
 latex_documents = [('contents', 'sphinx.tex', 'Sphinx Documentation',
                     'Georg Brandl', 'manual', 1)]
    to use this option.  The default value is ``False`` because the automatic
    conversion may loose information.
 
+.. confval:: epub_max_image_width
+
+   This option specifies the maximum width of images.  If it is set to a value
+   greater than zero, images with a width larger than the given value are
+   scaled accordingly.  If it is zero, no scaling is performed. The default
+   value is ``0``.  You need the Python Image Library (PIL) installed to use
+   this option.
+
+
 .. _latex-options:
 
 Options for LaTeX output

sphinx/builders/epub.py

     def copy_image_files_pil(self):
         """Copy images using the PIL.
         The method tries to read and write the files with the PIL,
-        converting the format if necessary/possible.
+        converting the format and resizing the image if necessary/possible.
         """
         ensuredir(path.join(self.outdir, '_images'))
         for src in self.status_iterator(self.images, 'copying images... ',
                     self.warn('cannot copy image file %r: %s' %
                               (path.join(self.srcdir, src), err))
                 continue
-            if img.mode in ('P',):
-                # See PIL documentation for Image.convert()
-                img = img.convert()
+            if self.config.epub_fix_images:
+                if img.mode in ('P',):
+                    # See PIL documentation for Image.convert()
+                    img = img.convert()
+            if self.config.epub_max_image_width > 0:
+                (width, height) = img.size
+                nw = self.config.epub_max_image_width
+                if width > nw:
+                    nh = (height * nw) / width
+                    img = img.resize((nw, nh), Image.BICUBIC)
             try:
                 img.save(path.join(self.outdir, '_images', dest))
             except IOError, err:
         This overwritten method can use the PIL to convert image files.
         """
         if self.images:
-            if self.config.epub_fix_images:
+            if self.config.epub_fix_images or self.config.epub_max_image_width:
                 if not Image:
                     self.warn('PIL not found - copying image files')
                     super(EpubBuilder, self).copy_image_files()
         epub_tocdepth = (3, 'env'),
         epub_tocdup = (True, 'env'),
         epub_fix_images = (False, 'html'),
+        epub_max_image_width = (0, 'html'),
 
         # LaTeX options
         latex_documents = ([], None),

sphinx/quickstart.py

 
 # Fix unsupported image types using the PIL.
 #epub_fix_images = False
+
+# Scale large images.
+#epub_max_image_width = 0
 '''
 
 INTERSPHINX_CONFIG = '''