8000 FIX: fix reading from http/https urls via imread · matplotlib/matplotlib@5d5d919 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5d5d919

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 5d5d919

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

lib/matplotlib/image.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import logging
99
from numbers import Number
1010
from pathlib import Path
11-
import urllib.parse
1211

1312
import numpy as np
1413
import 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,17 @@ 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) as response:
1479+
import io
1480+
try:
1481+
response.seek(0)
1482+
except (AttributeError, io.UnsupportedOperation):
1483+
response = io.BytesIO(response.read())
14751484
return imread(response, format=ext)
14761485
with img_open(fname) as image:
14771486
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 reach out 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)
0