Skip to content

Allow URL strings to be passed to imread #4256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/users/whats_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ directive - ``close-figs`` - that closes any previous figure windows before
creating the plots. This can help avoid some surprising duplicates of plots
when using ``plot_directive``.

Support for URL string arguments to ``imread``
----------------------------------------------

The ``imread`` function now accepts URL strings that point to remote PNG
files. This circumvents the generation of a HTTPResponse object directly.


.. _whats-new-1-4:

new in matplotlib-1.4
Expand Down
39 changes: 25 additions & 14 deletions lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
unicode_literals)

import six
from six.moves.urllib.parse import urlparse
from six.moves.urllib.request import urlopen
from six import BytesIO

import os
import warnings
Expand Down Expand Up @@ -1201,8 +1204,9 @@ def imread(fname, format=None):
"""
Read an image from a file into an array.

*fname* may be a string path or a Python file-like object. If
using a file object, it must be opened in binary mode.
*fname* may be a string path, a valid URL, or a Python
file-like object. If using a file object, it must be opened in binary
mode.

If *format* is provided, will try to read file of that type,
otherwise the format is deduced from the filename. If nothing can
Expand All @@ -1215,7 +1219,9 @@ def imread(fname, format=None):
matplotlib can only read PNGs natively, but if `PIL
<http://www.pythonware.com/products/pil/>`_ is installed, it will
use it to load the image and return an array (if possible) which
can be used with :func:`~matplotlib.pyplot.imshow`.
can be used with :func:`~matplotlib.pyplot.imshow`. Note, URL strings
may not be compatible with PIL. Check the PIL documentation for more
information.
"""

def pilread(fname):
Expand All @@ -1224,20 +1230,19 @@ def pilread(fname):
from PIL import Image
except ImportError:
return None
if cbook.is_string_like(fname):
# force close the file after reading the image
with open(fname, "rb") as fh:
image = Image.open(fh)
return pil_to_array(image)
else:
image = Image.open(fname)
return pil_to_array(image)
image = Image.open(fname)
return pil_to_array(image)

handlers = {'png': _png.read_png, }
if format is None:
if cbook.is_string_like(fname):
basename, ext = os.path.splitext(fname)
ext = ext.lower()[1:]
parsed = urlparse(fname)
# If the string is a URL, assume png
if parsed.scheme != '':
ext = 'png'
else:
basename, ext = os.path.splitext(fname)
ext = ext.lower()[1:]
elif hasattr(fname, 'name'):
basename, ext = os.path.splitext(fname.name)
ext = ext.lower()[1:]
Expand All @@ -1260,8 +1265,14 @@ def pilread(fname):
# reader extension, since Python handles them quite well, but it's
# tricky in C.
if cbook.is_string_like(fname):
with open(fname, 'rb') as fd:
parsed = urlparse(fname)
# If fname is a URL, download the data
if parsed.scheme != '':
fd = BytesIO(urlopen(fname).read())
return handler(fd)
else:
with open(fname, 'rb') as fd:
return handler(fd)
else:
return handler(fname)

Expand Down