Image plotting
In analyzing images, the first step is to convert colors into numerical values. Matplotlib provides APIs to read and show an image matrix of RGB values.
The following is a quick code example of reading an image into a NumPy array with plt.imread('image_path')
, and we show it with plt.imshow(image_ndarray)
. Make sure that the Pillow package is installed so that more image types other than PNG can be handled:
import matplotlib.pyplot as plt # Source image downloaded under CC0 license: Free for personal and commercial use. No attribution required. # Source image address: https://pixabay.com/en/rose-pink-blossom-bloom-flowers-693155/ img = plt.imread('ch04.img/mpldev_ch04_rose.jpg') plt.imshow(img)
Here is the original image displayed with the preceding code:

After showing the original image, we will try to work with transforming the image by changing the color values in the image matrix. We will create a high-contrast image by setting the RGB values to either 0
or 255
(max) at the...