Skip to content

Commit dbe3622

Browse files
authored
Merge pull request #19367 from anntzer/deprecate-imread-url
Deprecate imread() reading from URLs
2 parents 476b7e4 + 9302392 commit dbe3622

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
``imread()`` reading from URLs
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
Passing a URL to `~.pyplot.imread()` is deprecated. Please open the URL for
5+
reading and directly use the Pillow API
6+
(``PIL.Image.open(urllib.request.urlopen(url))``, or
7+
``PIL.Image.open(io.BytesIO(requests.get(url).content))``) instead.

lib/matplotlib/image.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -1430,6 +1430,10 @@ 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
1435+
for reading and pass the result to Pillow, e.g. with
1436+
``PIL.Image.open(urllib.request.urlopen(url))``.
14331437
format : str, optional
14341438
The image file format assumed for reading the data. If not
14351439
given, the format is deduced from the filename. If nothing can
@@ -1472,9 +1476,13 @@ def imread(fname, format=None):
14721476
img_open = (
14731477
PIL.PngImagePlugin.PngImageFile if ext == 'png' else PIL.Image.open)
14741478
if isinstance(fname, str):
1475-
14761479
parsed = parse.urlparse(fname)
14771480
if len(parsed.scheme) > 1: # Pillow doesn't handle URLs directly.
1481+
cbook.warn_deprecated(
1482+
"3.4", message="Directly reading images from URLs is "
1483+
"deprecated. Please open the URL for reading and pass the "
1484+
"result to Pillow, e.g. with "
1485+
"``PIL.Image.open(urllib.request.urlopen(url))``.")
14781486
# hide imports to speed initial import on systems with slow linkers
14791487
from urllib import request
14801488
ssl_ctx = mpl._get_ssl_context()

lib/matplotlib/tests/test_image.py

+5-3
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+
_api, 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 _api.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 _api.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)