Deprecate imread() reading from URLs by anntzer · Pull Request #19367 · matplotlib/matplotlib · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/api/next_api_changes/deprecations/18649-TH.rst
10 changes: 9 additions & 1 deletion lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1430,6 +1430,10 @@ 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
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
Expand Down Expand Up @@ -1472,9 +1476,13 @@ 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 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()
Expand Down
8 changes: 5 additions & 3 deletions lib/matplotlib/tests/test_image.py