Reading and displaying images
Matplotlib.pyplot
has features that enable us to read JPEG and PNG images and covert it to pixel format and display it back as images.
Getting ready
Import required library:
import matplotlib.pyplot as plt
How to do it...
- Here is the code block that reads a JPEG file as a pixel valued list and displays it back as an image onto the screen:
# Read the image louvre.jpg into 3 dimensional array(color images have 3 channels, whereas black & white #images have one channel only) image = plt.imread('louvre.jpg') # Print the dimensions of the image print("Dimensions of the image: ", image.shape) # Display the image onto the screen plt.imshow(image) plt.show()
How it works...
plt.imread()
method reads the image into an array of pixels.plt.imshow()
method displays the image onto the screen.image.shape
gives the dimensions of the list into which the image was read to.print()
displays the dimensions of the image onto the screen.
