diff --git a/doc/conf.py b/doc/conf.py index 81c645c1c..8b76d23f1 100644 --- a/doc/conf.py +++ b/doc/conf.py @@ -39,6 +39,7 @@ epub_exclude_files = ['_static/opensearch.xml', '_static/doctools.js', '_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)] diff --git a/doc/config.rst b/doc/config.rst index b5b65079b..ea9b731ad 100644 --- a/doc/config.rst +++ b/doc/config.rst @@ -812,6 +812,15 @@ the `Dublin Core metadata `_. 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 diff --git a/sphinx/builders/epub.py b/sphinx/builders/epub.py index 101cb936b..fbe88c3b2 100644 --- a/sphinx/builders/epub.py +++ b/sphinx/builders/epub.py @@ -311,7 +311,7 @@ class EpubBuilder(StandaloneHTMLBuilder): 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... ', @@ -329,9 +329,16 @@ class EpubBuilder(StandaloneHTMLBuilder): 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: @@ -343,7 +350,7 @@ class EpubBuilder(StandaloneHTMLBuilder): 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() diff --git a/sphinx/config.py b/sphinx/config.py index aa689f2e0..9841ba46a 100644 --- a/sphinx/config.py +++ b/sphinx/config.py @@ -127,6 +127,7 @@ class Config(object): epub_tocdepth = (3, 'env'), epub_tocdup = (True, 'env'), epub_fix_images = (False, 'html'), + epub_max_image_width = (0, 'html'), # LaTeX options latex_documents = ([], None), diff --git a/sphinx/quickstart.py b/sphinx/quickstart.py index e1a5d9e84..db18ef4eb 100644 --- a/sphinx/quickstart.py +++ b/sphinx/quickstart.py @@ -288,6 +288,9 @@ epub_copyright = u'%(copyright_str)s' # Fix unsupported image types using the PIL. #epub_fix_images = False + +# Scale large images. +#epub_max_image_width = 0 ''' INTERSPHINX_CONFIG = '''