88import logging
99from numbers import Number
1010from pathlib import Path
11- import urllib .parse
1211
1312import numpy as np
1413import PIL .PngImagePlugin
@@ -1443,9 +1442,12 @@ def imread(fname, format=None):
14431442 - (M, N, 3) for RGB images.
14441443 - (M, N, 4) for RGBA images.
14451444 """
1445+ # hide imports to speed initial import on systems with slow linkers
1446+ from urllib import parse
1447+
14461448 if format is None :
14471449 if isinstance (fname , str ):
1448- parsed = urllib . parse .urlparse (fname )
1450+ parsed = parse .urlparse (fname )
14491451 # If the string is a URL (Windows paths appear as if they have a
14501452 # length-1 scheme), assume png.
14511453 if len (parsed .scheme ) > 1 :
@@ -1468,10 +1470,18 @@ def imread(fname, format=None):
14681470 img_open = (
14691471 PIL .PngImagePlugin .PngImageFile if ext == 'png' else PIL .Image .open )
14701472 if isinstance (fname , str ):
1471- parsed = urllib .parse .urlparse (fname )
1473+
1474+ parsed = parse .urlparse (fname )
14721475 if len (parsed .scheme ) > 1 : # Pillow doesn't handle URLs directly.
1476+ # hide imports to speed initial import on systems with slow linkers
14731477 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 ())
14751485 return imread (response , format = ext )
14761486 with img_open (fname ) as image :
14771487 return (_pil_png_to_float_array (image )
0 commit comments