Reading, saving, and displaying an image using Matplotlib

The next code block shows how to use the imread() function from matplotlib.image to read an image in a floating-point numpy ndarray. The pixel values are represented as real values between 0 and 1:

im = mpimg.imread("../images/hill.png")  # read the image from disk as a numpy ndarrayprint(im.shape, im.dtype, type(im))      # this image contains an α channel, hence num_channels= 4# (960, 1280, 4) float32 <class 'numpy.ndarray'>plt.figure(figsize=(10,10))plt.imshow(im) # display the imageplt.axis('off')plt.show()

The following figure shows the output of the previous code:

The next code snippet ...

Get Hands-On Image Processing with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.