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

Skip to content

Commit cb63559

Browse files
authored
Remove use of the discouraged plt.imread() in the docs. (#31188)
1 parent 54d3856 commit cb63559

6 files changed

Lines changed: 24 additions & 14 deletions

File tree

galleries/examples/images_contours_and_fields/image_clip_path.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
66
Demo of image that's been clipped by a circular patch.
77
"""
8+
9+
from PIL import Image
10+
811
import matplotlib.pyplot as plt
912

1013
import matplotlib.cbook as cbook
1114
import matplotlib.patches as patches
1215

13-
with cbook.get_sample_data('grace_hopper.jpg') as image_file:
14-
image = plt.imread(image_file)
16+
image = Image.open(cbook.get_sample_data("grace_hopper.jpg"))
1517

1618
fig, ax = plt.subplots()
1719
im = ax.imshow(image)

galleries/examples/images_contours_and_fields/image_demo.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
functionality of imshow and the many images you can create.
99
"""
1010

11+
from PIL import Image
12+
1113
import matplotlib.pyplot as plt
1214
import numpy as np
1315

@@ -40,8 +42,7 @@
4042
# It is also possible to show images of pictures.
4143

4244
# A sample image
43-
with cbook.get_sample_data('grace_hopper.jpg') as image_file:
44-
image = plt.imread(image_file)
45+
image = Image.open(cbook.get_sample_data("grace_hopper.jpg"))
4546

4647
# And another image, using 256x256 16-bit integers.
4748
w, h = 256, 256

galleries/examples/misc/demo_ribbon_box.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
66
"""
77

8+
from PIL import Image
9+
810
import matplotlib.pyplot as plt
911
import numpy as np
1012

@@ -16,8 +18,9 @@
1618

1719
class RibbonBox:
1820

19-
original_image = plt.imread(
20-
cbook.get_sample_data("Minduka_Present_Blue_Pack.png"))
21+
# Image.open reads this image as 8-bit; divide by 255 to convert to 0-1.
22+
original_image = np.asarray(Image.open(
23+
cbook.get_sample_data("Minduka_Present_Blue_Pack.png"))) / 255
2124
cut_location = 70
2225
b_and_h = original_image[:, :, 2:3]
2326
color = original_image[:, :, 2:3] - original_image[:, :, 0:1]

galleries/examples/text_labels_and_annotations/demo_text_path.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
Using a text as a Path
44
======================
55
6-
`~matplotlib.text.TextPath` creates a `.Path` that is the outline of the
7-
characters of a text. The resulting path can be employed e.g. as a clip path
8-
for an image.
6+
`.TextPath` creates a `.Path` that is the outline of the characters of a text.
7+
The resulting path can be employed e.g. as a clip path for an image.
98
"""
109

10+
from PIL import Image
11+
1112
import matplotlib.pyplot as plt
1213
import numpy as np
1314

@@ -50,7 +51,7 @@ def draw(self, renderer=None):
5051

5152
# EXAMPLE 1
5253

53-
arr = plt.imread(get_sample_data("grace_hopper.jpg"))
54+
arr = Image.open(get_sample_data("grace_hopper.jpg"))
5455

5556
text_path = TextPath((0, 0), "!?", size=150)
5657
p = PathClippedImagePatch(text_path, arr, ec="k")

galleries/examples/text_labels_and_annotations/mathtext_asarray.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66

77
from io import BytesIO
88

9+
from PIL import Image
10+
911
import matplotlib.pyplot as plt
12+
import numpy as np
1013

1114
from matplotlib.figure import Figure
1215
from matplotlib.transforms import IdentityTransform
@@ -17,7 +20,7 @@ def text_to_rgba(s, *, dpi, **kwargs):
1720
# - draw it on an empty and transparent figure;
1821
# - save the figure to a temporary buffer using ``bbox_inches="tight",
1922
# pad_inches=0`` which will pick the correct area to save;
20-
# - load the buffer using ``plt.imread``.
23+
# - load the buffer using ``PIL.Image.open``.
2124
#
2225
# (If desired, one can also directly save the image to the filesystem.)
2326
fig = Figure(facecolor="none")
@@ -26,7 +29,7 @@ def text_to_rgba(s, *, dpi, **kwargs):
2629
fig.savefig(buf, dpi=dpi, format="png", bbox_inches="tight",
2730
pad_inches=0)
2831
buf.seek(0)
29-
rgba = plt.imread(buf)
32+
rgba = np.asarray(Image.open(buf))
3033
return rgba
3134

3235

galleries/examples/user_interfaces/mplcvd.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import functools
1111
from pathlib import Path
1212

13+
from PIL import Image
1314
import colorspacious
1415

1516
import numpy as np
@@ -289,8 +290,7 @@ def _setup_wx(tb):
289290
fig.colorbar(imt)
290291

291292
# A sample image
292-
with cbook.get_sample_data('grace_hopper.jpg') as image_file:
293-
photo = plt.imread(image_file)
293+
photo = Image.open(cbook.get_sample_data("grace_hopper.jpg"))
294294
axd['photo'].imshow(photo)
295295

296296
th = np.linspace(0, 2*np.pi, 1024)

0 commit comments

Comments
 (0)