| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 959b2a0 commit 2e9fe2d
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -49,7 +49,8 @@ | |||
| 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: | |
@@ -74,20 +75,13 @@ | |||
| 74 | 75 | # | |
| 75 | 76 | # And here we go... | |
| 76 | 77 | ||
| 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)) | ||
| 79 | 80 | ||
| 80 | 81 | ############################################################################### | |
| 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. | ||
| 91 | 85 | # | |
| 92 | 86 | # Each inner list represents a pixel. Here, with an RGB image, there | |
| 93 | 87 | # are 3 values. Since it's a black and white image, R, G, and B are all | |
@@ -188,7 +182,7 @@ | |||
| 188 | 182 | # interesting regions is the histogram. To create a histogram of our | |
| 189 | 183 | # image data, we use the :func:`~matplotlib.pyplot.hist` function. | |
| 190 | 184 | ||
| 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') | ||
| 192 | 186 | ||
| 193 | 187 | ############################################################################### | |
| 194 | 188 | # Most often, the "interesting" part of the image is around the peak, | |
@@ -205,20 +199,19 @@ | |||
| 205 | 199 | # | |
| 206 | 200 | # You can specify the clim in the call to ``plot``. | |
| 207 | 201 | ||
| 208 | - imgplot = plt.imshow(lum_img, clim=(0.0, 0.7)) | ||
| 202 | + imgplot = plt.imshow(lum_img, clim=(0, 175)) | ||
| 209 | 203 | ||
| 210 | 204 | ############################################################################### | |
| 211 | 205 | # 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) | ||
| 214 | 207 | 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) | ||
| 218 | 211 | 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') | ||
| 222 | 215 | ||
| 223 | 216 | ############################################################################### | |
| 224 | 217 | # .. _Interpolation: | |
@@ -242,19 +235,17 @@ | |||
| 242 | 235 | # We'll use the Pillow library that we used to load the image also to resize | |
| 243 | 236 | # the image. | |
| 244 | 237 | ||
| 245 | - from PIL import Image | ||
| 246 | - | ||
| 247 | 238 | img = Image.open('../../doc/_static/stinkbug.png') | |
| 248 | 239 | img.thumbnail((64, 64), Image.ANTIALIAS) # resizes image in-place | |
| 249 | 240 | imgplot = plt.imshow(img) | |
| 250 | 241 | ||
| 251 | 242 | ############################################################################### | |
| 252 | - # Here we have the default interpolation, bilinear, since we did not | ||
| 243 | + # Here we have the default interpolation ("nearest"), since we did not | ||
| 253 | 244 | # give :func:`~matplotlib.pyplot.imshow` any interpolation argument. | |
| 254 | 245 | # | |
| 255 | - # Let's try some others. Here's "nearest", which does no interpolation. | ||
| 246 | + # Let's try some others. Here's "bilinear": | ||
| 256 | 247 | ||
| 257 | - imgplot = plt.imshow(img, interpolation="nearest") | ||
| 248 | + imgplot = plt.imshow(img, interpolation="bilinear") | ||
| 258 | 249 | ||
| 259 | 250 | ############################################################################### | |
| 260 | 251 | # and bicubic: | |
| Back | FazBrowse Home | New Git URL |
0 commit comments