Skip to content

Commit 980c649

Browse files
tacaswelltimhoffm
andcommitted
FIX: fix reading from http/https urls via imread
closes #18129 Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com>
1 parent 5c4eeac commit 980c649

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

lib/matplotlib/image.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,10 +1468,16 @@ def imread(fname, format=None):
14681468
img_open = (
14691469
PIL.PngImagePlugin.PngImageFile if ext == 'png' else PIL.Image.open)
14701470
if isinstance(fname, str):
1471+
import io
1472+
from urllib import request
1473+
14711474
parsed = urllib.parse.urlparse(fname)
14721475
if len(parsed.scheme) > 1: # Pillow doesn't handle URLs directly.
1473-
from urllib import request
1474-
with urllib.request.urlopen(fname) as response:
1476+
with request.urlopen(fname) as response:
1477+
try:
1478+
response.seek(0)
1479+
except (AttributeError, io.UnsupportedOperation):
1480+
response = io.BytesIO(response.read())
14751481
return imread(response, format=ext)
14761482
with img_open(fname) as image:
14771483
return (_pil_png_to_float_array(image)

lib/matplotlib/testing/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def pytest_configure(config):
1616
("markers", "style: Set alternate Matplotlib style temporarily."),
1717
("markers", "baseline_images: Compare output against references."),
1818
("markers", "pytz: Tests that require pytz to be installed."),
19+
("markers", "network: Tests that reachout to the network."),
1920
("filterwarnings", "error"),
2021
]:
2122
config.addinivalue_line(key, value)

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)