8
8
import logging
9
9
from numbers import Number
10
10
from pathlib import Path
11
- import urllib .parse
12
11
13
12
import numpy as np
14
13
import PIL .PngImagePlugin
@@ -1443,9 +1442,12 @@ def imread(fname, format=None):
1443
1442
- (M, N, 3) for RGB images.
1444
1443
- (M, N, 4) for RGBA images.
1445
1444
"""
1445
+ # hide imports to speed initial import on systems with slow linkers
1446
+ from urllib import parse
1447
+
1446
1448
if format is None :
1447
1449
if isinstance (fname , str ):
1448
- parsed = urllib . parse .urlparse (fname )
1450
+ parsed = parse .urlparse (fname )
1449
1451
# If the string is a URL (Windows paths appear as if they have a
1450
1452
# length-1 scheme), assume png.
1451
1453
if len (parsed .scheme ) > 1 :
@@ -1468,10 +1470,18 @@ def imread(fname, format=None):
1468
1470
img_open = (
1469
1471
PIL .PngImagePlugin .PngImageFile if ext == 'png' else PIL .Image .open )
1470
1472
if isinstance (fname , str ):
1471
- parsed = urllib .parse .urlparse (fname )
1473
+
1474
+ parsed = parse .urlparse (fname )
1472
1475
if len (parsed .scheme ) > 1 : # Pillow doesn't handle URLs directly.
1476
+ # hide imports to speed initial import on systems with slow linkers
1473
1477
from urllib import request
1474
- with urllib .request .urlopen (fname ) as response :
1478
+ with request .urlopen (fname ,
1479
+ context = mpl ._get_ssl_context ()) as response :
1480
+ import io
1481
+ try :
1482
+ response .seek (0 )
1483
+ except (AttributeError , io .UnsupportedOperation ):
1484
+ response = io .BytesIO (response .read ())
1475
1485
return imread (response , format = ext )
1476
1486
with img_open (fname ) as image :
1477
1487
return (_pil_png_to_float_array (image )
0 commit comments