Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5b132aa

Browse files
committed
Deprecate imread() reading from URLs
1 parent 825f518 commit 5b132aa

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
``imread()`` reading from URLs
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
Passing a URL to `~.pyplot.imread()` is deprecated. Please open the URL before
5+
reading using ``urllib.request.urlopen()``.

lib/matplotlib/image.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1431,6 +1431,9 @@ def imread(fname, format=None):
14311431
fname : str or file-like
14321432
The image file to read: a filename, a URL or a file-like object opened
14331433
in read-binary mode.
1434+
1435+
Passing a URL is deprecated. Please open the URL before reading using
1436+
``urllib.request.urlopen()``.
14341437
format : str, optional
14351438
The image file format assumed for reading the data. If not
14361439
given, the format is deduced from the filename. If nothing can
@@ -1473,9 +1476,12 @@ def imread(fname, format=None):
14731476
img_open = (
14741477
PIL.PngImagePlugin.PngImageFile if ext == 'png' else PIL.Image.open)
14751478
if isinstance(fname, str):
1476-
14771479
parsed = parse.urlparse(fname)
14781480
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 before "
1484+
"reading using urllib.request.urlopen().")
14791485
# hide imports to speed initial import on systems with slow linkers
14801486
from urllib import request
14811487
with request.urlopen(fname,

lib/matplotlib/tests/test_image.py

Lines changed: 5 additions & 3 deletions
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+
cbook, 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
@@ -705,7 +705,8 @@ def test_load_from_url():
705705
url = ('file:'
706706
+ ('///' if sys.platform == 'win32' else '')
707707
+ path.resolve().as_posix())
708-
plt.imread(url)
708+
with cbook._suppress_matplotlib_deprecation_warning():
709+
plt.imread(url)
709710
plt.imread(urllib.request.urlopen(url))
710711

711712

@@ -1124,7 +1125,8 @@ def test_exact_vmin():
11241125
@pytest.mark.network
11251126
@pytest.mark.flaky
11261127
def test_https_imread_smoketest():
1127-
v = mimage.imread('https://matplotlib.org/1.5.0/_static/logo2.png')
1128+
with cbook._suppress_matplotlib_deprecation_warning():
1129+
v = mimage.imread('https://matplotlib.org/1.5.0/_static/logo2.png')
11281130

11291131

11301132
# A basic ndarray subclass that implements a quantity

0 commit comments

Comments
 (0)