diff --git a/doc/api/next_api_changes/deprecations/18649-TH.rst b/doc/api/next_api_changes/deprecations/18649-TH.rst new file mode 100644 index 000000000000..bac9f68df193 --- /dev/null +++ b/doc/api/next_api_changes/deprecations/18649-TH.rst @@ -0,0 +1,5 @@ +``imread()`` reading from URLs +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Passing a URL to `~.pyplot.imread()` is deprecated. Please open the URL before +reading using ``urllib.request.urlopen()``. diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index 0dc4449b4abc..7ba78c6494c9 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -1431,6 +1431,9 @@ def imread(fname, format=None): fname : str or file-like The image file to read: a filename, a URL or a file-like object opened in read-binary mode. + + Passing a URL is deprecated. Please open the URL before reading using + ``urllib.request.urlopen()``. format : str, optional The image file format assumed for reading the data. If not given, the format is deduced from the filename. If nothing can @@ -1473,9 +1476,12 @@ def imread(fname, format=None): img_open = ( PIL.PngImagePlugin.PngImageFile if ext == 'png' else PIL.Image.open) if isinstance(fname, str): - parsed = parse.urlparse(fname) if len(parsed.scheme) > 1: # Pillow doesn't handle URLs directly. + cbook.warn_deprecated( + "3.4", message="Directly reading images from URLs is " + "deprecated. Please open the URL before " + "reading using urllib.request.urlopen().") # hide imports to speed initial import on systems with slow linkers from urllib import request with request.urlopen(fname, diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index 1fea19fbb8ba..a182476ea92a 100644 --- a/lib/matplotlib/tests/test_image.py +++ b/lib/matplotlib/tests/test_image.py @@ -12,7 +12,7 @@ from PIL import Image from matplotlib import ( - colors, image as mimage, patches, pyplot as plt, style, rcParams) + cbook, colors, image as mimage, patches, pyplot as plt, style, rcParams) from matplotlib.image import (AxesImage, BboxImage, FigureImage, NonUniformImage, PcolorImage) from matplotlib.testing.decorators import check_figures_equal, image_comparison @@ -705,7 +705,8 @@ def test_load_from_url(): url = ('file:' + ('///' if sys.platform == 'win32' else '') + path.resolve().as_posix()) - plt.imread(url) + with cbook._suppress_matplotlib_deprecation_warning(): + plt.imread(url) plt.imread(urllib.request.urlopen(url)) @@ -1124,7 +1125,8 @@ def test_exact_vmin(): @pytest.mark.network @pytest.mark.flaky def test_https_imread_smoketest(): - v = mimage.imread('https://matplotlib.org/1.5.0/_static/logo2.png') + with cbook._suppress_matplotlib_deprecation_warning(): + v = mimage.imread('https://matplotlib.org/1.5.0/_static/logo2.png') # A basic ndarray subclass that implements a quantity