From 1e12cad9242c94f2647ab47d4713dae634addfd1 Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Sat, 3 Oct 2020 22:42:54 +0200 Subject: [PATCH 1/2] Deprecate imread() reading from URLs --- doc/api/next_api_changes/deprecations/18649-TH.rst | 5 +++++ lib/matplotlib/image.py | 8 +++++++- lib/matplotlib/tests/test_image.py | 8 +++++--- 3 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 doc/api/next_api_changes/deprecations/18649-TH.rst 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 a155d6472aee..360714f4afb5 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -1430,6 +1430,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 @@ -1472,9 +1475,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 ssl_ctx = mpl._get_ssl_context() diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index 6486fcf84e6f..9657968de7eb 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) + _api, 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 @@ -714,7 +714,8 @@ def test_load_from_url(): url = ('file:' + ('///' if sys.platform == 'win32' else '') + path.resolve().as_posix()) - plt.imread(url) + with _api.suppress_matplotlib_deprecation_warning(): + plt.imread(url) with urllib.request.urlopen(url) as file: plt.imread(file) @@ -1128,7 +1129,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 _api.suppress_matplotlib_deprecation_warning(): + v = mimage.imread('https://matplotlib.org/1.5.0/_static/logo2.png') # A basic ndarray subclass that implements a quantity From 93023925854a09cf88c6eba7b98a23f97d55787d Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Tue, 26 Jan 2021 17:28:12 +0100 Subject: [PATCH 2/2] Document use of Pillow as alternative. --- doc/api/next_api_changes/deprecations/18649-TH.rst | 6 ++++-- lib/matplotlib/image.py | 10 ++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/doc/api/next_api_changes/deprecations/18649-TH.rst b/doc/api/next_api_changes/deprecations/18649-TH.rst index bac9f68df193..7d73d9bbe362 100644 --- a/doc/api/next_api_changes/deprecations/18649-TH.rst +++ b/doc/api/next_api_changes/deprecations/18649-TH.rst @@ -1,5 +1,7 @@ ``imread()`` reading from URLs ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -Passing a URL to `~.pyplot.imread()` is deprecated. Please open the URL before -reading using ``urllib.request.urlopen()``. +Passing a URL to `~.pyplot.imread()` is deprecated. Please open the URL for +reading and directly use the Pillow API +(``PIL.Image.open(urllib.request.urlopen(url))``, or +``PIL.Image.open(io.BytesIO(requests.get(url).content))``) instead. diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index 360714f4afb5..14b3f78f703b 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -1431,8 +1431,9 @@ def imread(fname, format=None): 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()``. + Passing a URL is deprecated. Please open the URL + for reading and pass the result to Pillow, e.g. with + ``PIL.Image.open(urllib.request.urlopen(url))``. 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 @@ -1479,8 +1480,9 @@ def imread(fname, format=None): 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().") + "deprecated. Please open the URL for reading and pass the " + "result to Pillow, e.g. with " + "``PIL.Image.open(urllib.request.urlopen(url))``.") # hide imports to speed initial import on systems with slow linkers from urllib import request ssl_ctx = mpl._get_ssl_context()