Merge pull request #2302 from tk0miya/2199_obtain_image_size_without_pillow

Fix #2199: Use ``imagesize`` package to obtain size of images
This commit is contained in:
Takeshi KOMIYA 2016-02-10 12:10:02 +09:00
commit 906bf68cd8
5 changed files with 50 additions and 20 deletions

View File

@ -53,6 +53,7 @@ Features added
* #1779: Add EPUB 3 builder * #1779: Add EPUB 3 builder
* #1751: Add :confval:`todo_link_only` to avoid file path and line indication on * #1751: Add :confval:`todo_link_only` to avoid file path and line indication on
:rst:dir:`todolist`. Thanks to Francesco Montesano. :rst:dir:`todolist`. Thanks to Francesco Montesano.
* #2199: Use ``imagesize`` package to obtain size of images
Bugs fixed Bugs fixed
---------- ----------

View File

@ -53,6 +53,7 @@ requires = [
'snowballstemmer>=1.1', 'snowballstemmer>=1.1',
'babel>=1.3,!=2.0', 'babel>=1.3,!=2.0',
'alabaster>=0.7,<0.8', 'alabaster>=0.7,<0.8',
'imagesize',
] ]
extras_require = { extras_require = {
# Environment Marker works for wheel 0.24 or later # Environment Marker works for wheel 0.24 or later

39
sphinx/util/images.py Normal file
View File

@ -0,0 +1,39 @@
# -*- coding: utf-8 -*-
"""
sphinx.util.images
~~~~~~~~~~~
Image utility functions for Sphinx.
:copyright: Copyright 2007-2016 by the Sphinx team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
import imagesize
try:
from PIL import Image # check for the Python Imaging Library
except ImportError:
try:
import Image
except ImportError:
Image = None
def get_image_size(filename):
try:
size = imagesize.get(filename)
if size[0] == -1:
size = None
if Image: # fallback to PIL
im = Image.open(filename)
size = im.size
try:
im.fp.close()
except Exception:
pass
return size
except:
return None

View File

@ -20,16 +20,9 @@ from docutils.writers.html4css1 import Writer, HTMLTranslator as BaseTranslator
from sphinx import addnodes from sphinx import addnodes
from sphinx.locale import admonitionlabels, _ from sphinx.locale import admonitionlabels, _
from sphinx.util.images import get_image_size
from sphinx.util.smartypants import sphinx_smarty_pants from sphinx.util.smartypants import sphinx_smarty_pants
try:
from PIL import Image # check for the Python Imaging Library
except ImportError:
try:
import Image
except ImportError:
Image = None
# A good overview of the purpose behind these classes can be found here: # A good overview of the purpose behind these classes can be found here:
# http://www.arnebrodowski.de/blog/write-your-own-restructuredtext-writer.html # http://www.arnebrodowski.de/blog/write-your-own-restructuredtext-writer.html
@ -481,21 +474,16 @@ class HTMLTranslator(BaseTranslator):
# Try to figure out image height and width. Docutils does that too, # Try to figure out image height and width. Docutils does that too,
# but it tries the final file name, which does not necessarily exist # but it tries the final file name, which does not necessarily exist
# yet at the time the HTML file is written. # yet at the time the HTML file is written.
if Image and not ('width' in node and 'height' in node): if not ('width' in node and 'height' in node):
try: size = get_image_size(os.path.join(self.builder.srcdir, olduri))
im = Image.open(os.path.join(self.builder.srcdir, olduri)) if size is None:
except (IOError, # Source image can't be found or opened self.builder.env.warn_node('Could not obtain image size. '
UnicodeError): # PIL doesn't like Unicode paths. ':scale: option is ignored.', node)
pass
else: else:
if 'width' not in node: if 'width' not in node:
node['width'] = str(im.size[0]) node['width'] = str(size[0])
if 'height' not in node: if 'height' not in node:
node['height'] = str(im.size[1]) node['height'] = str(size[1])
try:
im.fp.close()
except Exception:
pass
BaseTranslator.visit_image(self, node) BaseTranslator.visit_image(self, node)
def visit_toctree(self, node): def visit_toctree(self, node):

View File

@ -11,3 +11,4 @@ sqlalchemy>=0.9
whoosh>=2.0 whoosh>=2.0
alabaster alabaster
sphinx_rtd_theme sphinx_rtd_theme
imagesize