7
7
unicode_literals )
8
8
9
9
import six
10
+ from six .moves .urllib .parse import urlparse
11
+ from six .moves .urllib .request import urlopen
12
+ from six import BytesIO
10
13
11
14
import os
12
15
import warnings
@@ -1230,8 +1233,9 @@ def imread(fname, format=None):
1230
1233
"""
1231
1234
Read an image from a file into an array.
1232
1235
1233
- *fname* may be a string path or a Python file-like object. If
1234
- using a file object, it must be opened in binary mode.
1236
+ *fname* may be a string path, a valid URL, or a Python
1237
+ file-like object. If using a file object, it must be opened in binary
1238
+ mode.
1235
1239
1236
1240
If *format* is provided, will try to read file of that type,
1237
1241
otherwise the format is deduced from the filename. If nothing can
@@ -1244,7 +1248,9 @@ def imread(fname, format=None):
1244
1248
matplotlib can only read PNGs natively, but if `PIL
1245
1249
<http://www.pythonware.com/products/pil/>`_ is installed, it will
1246
1250
use it to load the image and return an array (if possible) which
1247
- can be used with :func:`~matplotlib.pyplot.imshow`.
1251
+ can be used with :func:`~matplotlib.pyplot.imshow`. Note, URL strings
1252
+ may not be compatible with PIL. Check the PIL documentation for more
1253
+ information.
1248
1254
"""
1249
1255
1250
1256
def pilread (fname ):
@@ -1253,20 +1259,19 @@ def pilread(fname):
1253
1259
from PIL import Image
1254
1260
except ImportError :
1255
1261
return None
1256
- if cbook .is_string_like (fname ):
1257
- # force close the file after reading the image
1258
- with open (fname , "rb" ) as fh :
1259
- image = Image .open (fh )
1260
- return pil_to_array (image )
1261
- else :
1262
- image = Image .open (fname )
1263
- return pil_to_array (image )
1262
+ image = Image .open (fname )
1263
+ return pil_to_array (image )
1264
1264
1265
1265
handlers = {'png' : _png .read_png , }
1266
1266
if format is None :
1267
1267
if cbook .is_string_like (fname ):
1268
- basename , ext = os .path .splitext (fname )
1269
- ext = ext .lower ()[1 :]
1268
+ parsed = urlparse (fname )
1269
+ # If the string is a URL, assume png
1270
+ if parsed .scheme != '' :
1271
+ ext = 'png'
1272
+ else :
1273
+ basename , ext = os .path .splitext (fname )
1274
+ ext = ext .lower ()[1 :]
1270
1275
elif hasattr (fname , 'name' ):
1271
1276
basename , ext = os .path .splitext (fname .name )
1272
1277
ext = ext .lower ()[1 :]
@@ -1289,8 +1294,14 @@ def pilread(fname):
1289
1294
# reader extension, since Python handles them quite well, but it's
1290
1295
# tricky in C.
1291
1296
if cbook .is_string_like (fname ):
1292
- with open (fname , 'rb' ) as fd :
1297
+ parsed = urlparse (fname )
1298
+ # If fname is a URL, download the data
1299
+ if parsed .scheme != '' :
1300
+ fd = BytesIO (urlopen (fname ).read ())
1293
1301
return handler (fd )
1302
+ else :
1303
+ with open (fname , 'rb' ) as fd :
1304
+ return handler (fd )
1294
1305
else :
1295
1306
return handler (fname )
1296
1307
0 commit comments