Added epub_max_image_width configuration option.

This commit is contained in:
Roland Meister 2011-02-28 21:17:52 +01:00
parent fe07254b9c
commit 246d07807d
5 changed files with 26 additions and 5 deletions

View File

@ -39,6 +39,7 @@ epub_exclude_files = ['_static/opensearch.xml', '_static/doctools.js',
'_static/jquery.js', '_static/searchtools.js', '_static/underscore.js', '_static/jquery.js', '_static/searchtools.js', '_static/underscore.js',
'_static/basic.css', 'search.html'] '_static/basic.css', 'search.html']
epub_fix_images = False epub_fix_images = False
epub_max_image_width = 0
latex_documents = [('contents', 'sphinx.tex', 'Sphinx Documentation', latex_documents = [('contents', 'sphinx.tex', 'Sphinx Documentation',
'Georg Brandl', 'manual', 1)] 'Georg Brandl', 'manual', 1)]

View File

@ -812,6 +812,15 @@ the `Dublin Core metadata <http://dublincore.org/>`_.
to use this option. The default value is ``False`` because the automatic to use this option. The default value is ``False`` because the automatic
conversion may loose information. 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: .. _latex-options:
Options for LaTeX output Options for LaTeX output

View File

@ -311,7 +311,7 @@ class EpubBuilder(StandaloneHTMLBuilder):
def copy_image_files_pil(self): def copy_image_files_pil(self):
"""Copy images using the PIL. """Copy images using the PIL.
The method tries to read and write the files with 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')) ensuredir(path.join(self.outdir, '_images'))
for src in self.status_iterator(self.images, 'copying 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' % self.warn('cannot copy image file %r: %s' %
(path.join(self.srcdir, src), err)) (path.join(self.srcdir, src), err))
continue continue
if self.config.epub_fix_images:
if img.mode in ('P',): if img.mode in ('P',):
# See PIL documentation for Image.convert() # See PIL documentation for Image.convert()
img = img.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: try:
img.save(path.join(self.outdir, '_images', dest)) img.save(path.join(self.outdir, '_images', dest))
except IOError, err: except IOError, err:
@ -343,7 +350,7 @@ class EpubBuilder(StandaloneHTMLBuilder):
This overwritten method can use the PIL to convert image files. This overwritten method can use the PIL to convert image files.
""" """
if self.images: 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: if not Image:
self.warn('PIL not found - copying image files') self.warn('PIL not found - copying image files')
super(EpubBuilder, self).copy_image_files() super(EpubBuilder, self).copy_image_files()

View File

@ -127,6 +127,7 @@ class Config(object):
epub_tocdepth = (3, 'env'), epub_tocdepth = (3, 'env'),
epub_tocdup = (True, 'env'), epub_tocdup = (True, 'env'),
epub_fix_images = (False, 'html'), epub_fix_images = (False, 'html'),
epub_max_image_width = (0, 'html'),
# LaTeX options # LaTeX options
latex_documents = ([], None), latex_documents = ([], None),

View File

@ -288,6 +288,9 @@ epub_copyright = u'%(copyright_str)s'
# Fix unsupported image types using the PIL. # Fix unsupported image types using the PIL.
#epub_fix_images = False #epub_fix_images = False
# Scale large images.
#epub_max_image_width = 0
''' '''
INTERSPHINX_CONFIG = ''' INTERSPHINX_CONFIG = '''