Skip to content

Commit 03d1884

Browse files
timhoffmanntzer
authored andcommitted
Deprecate imread() reading from URLs
1 parent 408b1ab commit 03d1884

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
``imread()`` reading from URLs
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
Passing a URL to `~.pyplot.imread()` is deprecated. Please open the URL before
5+
reading using ``urllib.request.urlopen()``.

lib/matplotlib/image.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1430,6 +1430,9 @@ def imread(fname, format=None):
14301430
fname : str or file-like
14311431
The image file to read: a filename, a URL or a file-like object opened
14321432
in read-binary mode.
1433+
1434+
Passing a URL is deprecated. Please open the URL before reading using
1435+
``urllib.request.urlopen()``.
14331436
format : str, optional
14341437
The image file format assumed for reading the data. If not
14351438
given, the format is deduced from the filename. If nothing can
@@ -1472,9 +1475,12 @@ def imread(fname, format=None):
14721475
img_open = (
14731476
PIL.PngImagePlugin.PngImageFile if ext == 'png' else PIL.Image.open)
14741477
if isinstance(fname, str):
1475-
14761478
parsed = parse.urlparse(fname)
14771479
if len(parsed.scheme) > 1: # Pillow doesn't handle URLs directly.
1480+
cbook.warn_deprecated(
1481+
"3.4", message="Directly reading images from URLs is "
1482+
"deprecated. Please open the URL before "
1483+
"reading using urllib.request.urlopen().")
14781484
# hide imports to speed initial import on systems with slow linkers
14791485
from urllib import request
14801486
ssl_ctx = mpl._get_ssl_context()

lib/matplotlib/tests/test_image.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from PIL import Image
1313

1414
from matplotlib import (
15-
colors, image as mimage, patches, pyplot as plt, style, rcParams)
15+
cbook, colors, image as mimage, patches, pyplot as plt, style, rcParams)
1616
from matplotlib.image import (AxesImage, BboxImage, FigureImage,
1717
NonUniformImage, PcolorImage)
1818
from matplotlib.testing.decorators import check_figures_equal, image_comparison
@@ -714,7 +714,8 @@ def test_load_from_url():
714714
url = ('file:'
715715
+ ('///' if sys.platform == 'win32' else '')
716716
+ path.resolve().as_posix())
717-
plt.imread(url)
717+
with cbook._suppress_matplotlib_deprecation_warning():
718+
plt.imread(url)
718719
with urllib.request.urlopen(url) as file:
719720
plt.imread(file)
720721

@@ -1128,7 +1129,8 @@ def test_exact_vmin():
11281129
@pytest.mark.network
11291130
@pytest.mark.flaky
11301131
def test_https_imread_smoketest():
1131-
v = mimage.imread('https://matplotlib.org/1.5.0/_static/logo2.png')
1132+
with cbook._suppress_matplotlib_deprecation_warning():
1133+
v = mimage.imread('https://matplotlib.org/1.5.0/_static/logo2.png')
11321134

11331135

11341136
# A basic ndarray subclass that implements a quantity

0 commit comments

Comments
 (0)