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

Skip to content

Commit 2e9fe2d

Browse files
committed
Update image tutorial.
- Use Pillow to load the image rather than the discouraged plt.imread. - Remove discussion about rescaling to 0-1 as dropping the use of plt.imread also gets rid of that (not so nice) behavior. Also drop the lengthy discussion about uint8 and float32, which seems out of place here (if anything it should be in the Pillow docs). Make corresponding fixes to code to use 0-255 instead of 0-1. - Fix subplot management to go full pyplot (the local switch to OO-style is a bit weird). - Fix interpolation discussion as the default is now "nearest" (actually it's now "antialiased", but let's sweep that under the rug for this tutorial for now).
1 parent 959b2a0 commit 2e9fe2d

File tree

1 file changed

+19
-28
lines changed

1 file changed

+19
-28
lines changed

tutorials/introductory/images.py

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949
"""
5050

5151
import matplotlib.pyplot as plt
52-
import matplotlib.image as mpimg
52+
import numpy as np
53+
from PIL import Image
5354

5455
###############################################################################
5556
# .. _importing_data:
@@ -74,20 +75,13 @@
7475
#
7576
# And here we go...
7677

77-
img = mpimg.imread('../../doc/_static/stinkbug.png')
78-
print(img)
78+
img = np.asarray(Image.open('../../doc/_static/stinkbug.png'))
79+
print(repr(img))
7980

8081
###############################################################################
81-
# Note the dtype there - float32. Matplotlib has rescaled the 8 bit
82-
# data from each channel to floating point data between 0.0 and 1.0. As
83-
# a side note, the only datatype that Pillow can work with is uint8.
84-
# Matplotlib plotting can handle float32 and uint8, but image
85-
# reading/writing for any format other than PNG is limited to uint8
86-
# data. Why 8 bits? Most displays can only render 8 bits per channel
87-
# worth of color gradation. Why can they only render 8 bits/channel?
88-
# Because that's about all the human eye can see. More here (from a
89-
# photography standpoint): `Luminous Landscape bit depth tutorial
90-
# <https://luminous-landscape.com/bit-depth/>`_.
82+
# Here, we used Pillow to open an image (with `PIL.Image.open`), and
83+
# immediately converted the `PIL.Image.Image` object into a 8-bit
84+
# (``dtype=uint8``) numpy array.
9185
#
9286
# Each inner list represents a pixel. Here, with an RGB image, there
9387
# are 3 values. Since it's a black and white image, R, G, and B are all
@@ -188,7 +182,7 @@
188182
# interesting regions is the histogram. To create a histogram of our
189183
# image data, we use the :func:`~matplotlib.pyplot.hist` function.
190184

191-
plt.hist(lum_img.ravel(), bins=256, range=(0.0, 1.0), fc='k', ec='k')
185+
plt.hist(lum_img.ravel(), bins=range(256), fc='k', ec='k')
192186

193187
###############################################################################
194188
# Most often, the "interesting" part of the image is around the peak,
@@ -205,20 +199,19 @@
205199
#
206200
# You can specify the clim in the call to ``plot``.
207201

208-
imgplot = plt.imshow(lum_img, clim=(0.0, 0.7))
202+
imgplot = plt.imshow(lum_img, clim=(0, 175))
209203

210204
###############################################################################
211205
# You can also specify the clim using the returned object
212-
fig = plt.figure()
213-
ax = fig.add_subplot(1, 2, 1)
206+
plt.subplot(1, 2, 1)
214207
imgplot = plt.imshow(lum_img)
215-
ax.set_title('Before')
216-
plt.colorbar(ticks=[0.1, 0.3, 0.5, 0.7], orientation='horizontal')
217-
ax = fig.add_subplot(1, 2, 2)
208+
plt.title('Before')
209+
plt.colorbar(orientation='horizontal')
210+
plt.subplot(1, 2, 2)
218211
imgplot = plt.imshow(lum_img)
219-
imgplot.set_clim(0.0, 0.7)
220-
ax.set_title('After')
221-
plt.colorbar(ticks=[0.1, 0.3, 0.5, 0.7], orientation='horizontal')
212+
imgplot.set_clim(0, 175)
213+
plt.title('After')
214+
plt.colorbar(orientation='horizontal')
222215

223216
###############################################################################
224217
# .. _Interpolation:
@@ -242,19 +235,17 @@
242235
# We'll use the Pillow library that we used to load the image also to resize
243236
# the image.
244237

245-
from PIL import Image
246-
247238
img = Image.open('../../doc/_static/stinkbug.png')
248239
img.thumbnail((64, 64), Image.ANTIALIAS) # resizes image in-place
249240
imgplot = plt.imshow(img)
250241

251242
###############################################################################
252-
# Here we have the default interpolation, bilinear, since we did not
243+
# Here we have the default interpolation ("nearest"), since we did not
253244
# give :func:`~matplotlib.pyplot.imshow` any interpolation argument.
254245
#
255-
# Let's try some others. Here's "nearest", which does no interpolation.
246+
# Let's try some others. Here's "bilinear":
256247

257-
imgplot = plt.imshow(img, interpolation="nearest")
248+
imgplot = plt.imshow(img, interpolation="bilinear")
258249

259250
###############################################################################
260251
# and bicubic:

0 commit comments

Comments
 (0)