Skip to content

Commit 66ff15a

Browse files
committed
FIX: fix reading from http/https urls via imread
closes #18129
1 parent 5c4eeac commit 66ff15a

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

lib/matplotlib/image.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from numbers import Number
1010
from pathlib import Path
1111
import urllib.parse
12+
import urllib.request
13+
from io import BytesIO
1214

1315
import numpy as np
1416
import PIL.PngImagePlugin
@@ -1470,9 +1472,8 @@ def imread(fname, format=None):
14701472
if isinstance(fname, str):
14711473
parsed = urllib.parse.urlparse(fname)
14721474
if len(parsed.scheme) > 1: # Pillow doesn't handle URLs directly.
1473-
from urllib import request
14741475
with urllib.request.urlopen(fname) as response:
1475-
return imread(response, format=ext)
1476+
return imread(BytesIO(response.read()), format=ext)
14761477
with img_open(fname) as image:
14771478
return (_pil_png_to_float_array(image)
14781479
if isinstance(image, PIL.PngImagePlugin.PngImageFile) else

lib/matplotlib/tests/test_image.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,3 +1118,9 @@ def test_exact_vmin():
11181118

11191119
# check than the RBGA values are the same
11201120
assert np.all(from_image == direct_computation)
1121+
1122+
1123+
@pytest.mark.network
1124+
@pytest.mark.flaky
1125+
def test_https_imread_smoketest():
1126+
v = mimage.imread('https://matplotlib.org/1.5.0/_static/logo2.png')

0 commit comments

Comments
 (0)