|
49 | 49 | """ |
50 | 50 |
|
51 | 51 | import matplotlib.pyplot as plt |
52 | | -import matplotlib.image as mpimg |
| 52 | +import numpy as np |
| 53 | +from PIL import Image |
53 | 54 |
|
54 | 55 | ############################################################################### |
55 | 56 | # .. _importing_data: |
|
72 | 73 | # <https://raw.githubusercontent.com/matplotlib/matplotlib/main/doc/_static/stinkbug.png>`_ |
73 | 74 | # to your computer for the rest of this tutorial. |
74 | 75 | # |
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. |
76 | 79 |
|
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)) |
79 | 82 |
|
80 | 83 | ############################################################################### |
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 | | -# |
92 | 84 | # Each inner list represents a pixel. Here, with an RGB image, there |
93 | 85 | # are 3 values. Since it's a black and white image, R, G, and B are all |
94 | 86 | # similar. An RGBA (where A is alpha, or transparency), has 4 values |
|
188 | 180 | # interesting regions is the histogram. To create a histogram of our |
189 | 181 | # image data, we use the :func:`~matplotlib.pyplot.hist` function. |
190 | 182 |
|
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') |
192 | 184 |
|
193 | 185 | ############################################################################### |
194 | 186 | # Most often, the "interesting" part of the image is around the peak, |
195 | 187 | # and you can get extra contrast by clipping the regions above and/or |
196 | 188 | # below the peak. In our histogram, it looks like there's not much |
197 | 189 | # useful information in the high end (not many white things in the |
198 | 190 | # 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. |
205 | 193 | # |
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``. |
207 | 196 |
|
208 | | -imgplot = plt.imshow(lum_img, clim=(0.0, 0.7)) |
| 197 | +plt.imshow(lum_img, clim=(0, 175)) |
209 | 198 |
|
210 | 199 | ############################################################################### |
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 | + |
218 | 206 | 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) |
222 | 208 |
|
223 | 209 | ############################################################################### |
224 | 210 | # .. _Interpolation: |
|
242 | 228 | # We'll use the Pillow library that we used to load the image also to resize |
243 | 229 | # the image. |
244 | 230 |
|
245 | | -from PIL import Image |
246 | | - |
247 | 231 | img = Image.open('../../doc/_static/stinkbug.png') |
248 | 232 | img.thumbnail((64, 64), Image.ANTIALIAS) # resizes image in-place |
249 | 233 | imgplot = plt.imshow(img) |
250 | 234 |
|
251 | 235 | ############################################################################### |
252 | | -# Here we have the default interpolation, bilinear, since we did not |
| 236 | +# Here we use the default interpolation ("nearest"), since we did not |
253 | 237 | # give :func:`~matplotlib.pyplot.imshow` any interpolation argument. |
254 | 238 | # |
255 | | -# Let's try some others. Here's "nearest", which does no interpolation. |
| 239 | +# Let's try some others. Here's "bilinear": |
256 | 240 |
|
257 | | -imgplot = plt.imshow(img, interpolation="nearest") |
| 241 | +imgplot = plt.imshow(img, interpolation="bilinear") |
258 | 242 |
|
259 | 243 | ############################################################################### |
260 | 244 | # and bicubic: |
|
0 commit comments