1. Georg Brandl
  2. sphinx


georg.brandl  committed acf2dfb

Support "scale" option for images in HTML output.

  • Participants
  • Parent commits d4e04f4
  • Branches default

Comments (0)

Files changed (2)


View file
 Bugs fixed
+* Support the "scale" option for images in HTML output.
 * Properly escape quotes in HTML help attribute values.
 * Fix LaTeX build for some description environments with ``:noindex:``.

File sphinx/htmlwriter.py

View file
 import sys
 import posixpath
+import os
 from docutils import nodes
 from docutils.writers.html4css1 import Writer, HTMLTranslator as BaseTranslator
 from sphinx.highlighting import PygmentsBridge
 from sphinx.util.smartypants import sphinx_smarty_pants
+    import Image                        # check for the Python Imaging Library
+except ImportError:
+    Image = None
 class HTMLWriter(Writer):
     def __init__(self, builder):
         if olduri in self.builder.images:
             node['uri'] = posixpath.join(self.builder.imgpath,
+        if node.has_key('scale'):
+            if Image and not (node.has_key('width')
+                              and node.has_key('height')):
+                try:
+                    im = Image.open(os.path.join(self.builder.srcdir, 
+                                                    olduri))
+                except (IOError, # Source image can't be found or opened
+                        UnicodeError):  # PIL doesn't like Unicode paths.
+                    print olduri
+                    pass
+                else:
+                    if not node.has_key('width'):
+                        node['width'] = str(im.size[0])
+                    if not node.has_key('height'):
+                        node['height'] = str(im.size[1])
+                    del im
         BaseTranslator.visit_image(self, node)
     def visit_toctree(self, node):