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

Skip to content

Commit cb71f65

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 cb71f65

File tree

1 file changed

+23
-39
lines changed

1 file changed

+23
-39
lines changed

tutorials/introductory/images.py

Lines changed: 23 additions & 39 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:
@@ -72,23 +73,14 @@
7273
# <https://raw.githubusercontent.com/matplotlib/matplotlib/main/doc/_static/stinkbug.png>`_
7374
# to your computer for the rest of this tutorial.
7475
#
75-
# And here we go...
76+
# We use Pillow to open an image (with `PIL.Image.open`), and immediately
77+
# convert the `PIL.Image.Image` object into an 8-bit (``dtype=uint8``) numpy
78+
# array.
7679

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

8083
###############################################################################
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/>`_.
91-
#
9284
# Each inner list represents a pixel. Here, with an RGB image, there
9385
# are 3 values. Since it's a black and white image, R, G, and B are all
9486
# similar. An RGBA (where A is alpha, or transparency), has 4 values
@@ -188,37 +180,31 @@
188180
# interesting regions is the histogram. To create a histogram of our
189181
# image data, we use the :func:`~matplotlib.pyplot.hist` function.
190182

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

193185
###############################################################################
194186
# Most often, the "interesting" part of the image is around the peak,
195187
# and you can get extra contrast by clipping the regions above and/or
196188
# below the peak. In our histogram, it looks like there's not much
197189
# useful information in the high end (not many white things in the
198190
# image). Let's adjust the upper limit, so that we effectively "zoom in
199-
# on" part of the histogram. We do this by passing the clim argument to
200-
# imshow. You could also do this by calling the
201-
# :meth:`~matplotlib.cm.ScalarMappable.set_clim` method of the image plot
202-
# object, but make sure that you do so in the same cell as your plot
203-
# command when working with the Jupyter Notebook - it will not change
204-
# plots from earlier cells.
191+
# on" part of the histogram. We do this by setting *clim*, the colormap
192+
# limits.
205193
#
206-
# You can specify the clim in the call to ``plot``.
194+
# This can be done by passing a *clim* keyword argument in the call to
195+
# ``imshow``.
207196

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

210199
###############################################################################
211-
# You can also specify the clim using the returned object
212-
fig = plt.figure()
213-
ax = fig.add_subplot(1, 2, 1)
214-
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)
200+
# This can also be done by calling the
201+
# :meth:`~matplotlib.cm.ScalarMappable.set_clim` method of the returned image
202+
# plot object, but make sure that you do so in the same cell as your plot
203+
# command when working with the Jupyter Notebook - it will not change
204+
# plots from earlier cells.
205+
218206
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')
207+
imgplot.set_clim(0, 175)
222208

223209
###############################################################################
224210
# .. _Interpolation:
@@ -242,19 +228,17 @@
242228
# We'll use the Pillow library that we used to load the image also to resize
243229
# the image.
244230

245-
from PIL import Image
246-
247231
img = Image.open('../../doc/_static/stinkbug.png')
248232
img.thumbnail((64, 64), Image.ANTIALIAS) # resizes image in-place
249233
imgplot = plt.imshow(img)
250234

251235
###############################################################################
252-
# Here we have the default interpolation, bilinear, since we did not
236+
# Here we use the default interpolation ("nearest"), since we did not
253237
# give :func:`~matplotlib.pyplot.imshow` any interpolation argument.
254238
#
255-
# Let's try some others. Here's "nearest", which does no interpolation.
239+
# Let's try some others. Here's "bilinear":
256240

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

259243
###############################################################################
260244
# and bicubic:

0 commit comments

Comments
 (0)