Skip to content

Commit 2cb39f9

Browse files
committed
MNT: Remove ability to pass URLs to imread()
This follows the deprecation period.
1 parent feaf336 commit 2cb39f9

File tree

3 files changed

+17
-32
lines changed

3 files changed

+17
-32
lines changed

doc/api/next_api_changes/removals/23XXX-GL.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,11 @@ Parameters of the Axes constructor other than *fig* and *rect* are now keyword o
2626
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2727

2828
Use ``docutils.parsers.rst.directives.images.Image.align`` instead.
29+
30+
``imread()`` no longer accepts URLs
31+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
32+
33+
Passing a URL to `~.pyplot.imread()` has been removed. Please open the URL for
34+
reading and directly use the Pillow API
35+
(``PIL.Image.open(urllib.request.urlopen(url))``, or
36+
``PIL.Image.open(io.BytesIO(requests.get(url).content))``) instead.

lib/matplotlib/image.py

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,29 +1526,13 @@ def imread(fname, format=None):
15261526
ext = format
15271527
img_open = (
15281528
PIL.PngImagePlugin.PngImageFile if ext == 'png' else PIL.Image.open)
1529-
if isinstance(fname, str):
1530-
parsed = parse.urlparse(fname)
1531-
if len(parsed.scheme) > 1: # Pillow doesn't handle URLs directly.
1532-
_api.warn_deprecated(
1533-
"3.4", message="Directly reading images from URLs is "
1534-
"deprecated since %(since)s and will no longer be supported "
1535-
"%(removal)s. Please open the URL for reading and pass the "
1536-
"result to Pillow, e.g. with "
1537-
"``np.array(PIL.Image.open(urllib.request.urlopen(url)))``.")
1538-
# hide imports to speed initial import on systems with slow linkers
1539-
from urllib import request
1540-
ssl_ctx = mpl._get_ssl_context()
1541-
if ssl_ctx is None:
1542-
_log.debug(
1543-
"Could not get certifi ssl context, https may not work."
1544-
)
1545-
with request.urlopen(fname, context=ssl_ctx) as response:
1546-
import io
1547-
try:
1548-
response.seek(0)
1549-
except (AttributeError, io.UnsupportedOperation):
1550-
response = io.BytesIO(response.read())
1551-
return imread(response, format=ext)
1529+
if isinstance(fname, str) and len(parse.urlparse(fname).scheme) > 1:
1530+
# Pillow doesn't handle URLs directly.
1531+
raise ValueError(
1532+
"Please open the URL for reading and pass the "
1533+
"result to Pillow, e.g. with "
1534+
"``np.array(PIL.Image.open(urllib.request.urlopen(url)))``."
1535+
)
15521536
with img_open(fname) as image:
15531537
return (_pil_png_to_float_array(image)
15541538
if isinstance(image, PIL.PngImagePlugin.PngImageFile) else

lib/matplotlib/tests/test_image.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
import matplotlib as mpl
1515
from matplotlib import (
16-
_api, colors, image as mimage, patches, pyplot as plt, style, rcParams)
16+
colors, image as mimage, patches, pyplot as plt, style, rcParams)
1717
from matplotlib.image import (AxesImage, BboxImage, FigureImage,
1818
NonUniformImage, PcolorImage)
1919
from matplotlib.testing.decorators import check_figures_equal, image_comparison
@@ -720,7 +720,7 @@ def test_load_from_url():
720720
url = ('file:'
721721
+ ('///' if sys.platform == 'win32' else '')
722722
+ path.resolve().as_posix())
723-
with _api.suppress_matplotlib_deprecation_warning():
723+
with pytest.raises(ValueError, match="Please open the URL"):
724724
plt.imread(url)
725725
with urllib.request.urlopen(url) as file:
726726
plt.imread(file)
@@ -1139,13 +1139,6 @@ def test_exact_vmin():
11391139
assert np.all(from_image == direct_computation)
11401140

11411141

1142-
@pytest.mark.network
1143-
@pytest.mark.flaky
1144-
def test_https_imread_smoketest():
1145-
with _api.suppress_matplotlib_deprecation_warning():
1146-
v = mimage.imread('https://matplotlib.org/1.5.0/_static/logo2.png')
1147-
1148-
11491142
# A basic ndarray subclass that implements a quantity
11501143
# It does not implement an entire unit system or all quantity math.
11511144
# There is just enough implemented to test handling of ndarray

0 commit comments

Comments
 (0)