Calculate the area of an image using Matplotlib
Let us see how to calculate the area of an image in Python using Matplotlib.
Algorith :
- Import the
matplotlib.pyplotmodule. - Import an image using the
imread()method. - Use the
shapeattribute of the image to get the height and width of the image. It fetches the number of of channels in the image. - Calculate the area as,
area = height * width. - Dislpay the area.
Example 1 : Consider the following image :

“GFG.jpg”
# import necessary libraryimport matplotlib.pyplot as plt # read an imageimg = plt.imread("GFG.jpg") # fetch the height and widthheight, width, _ = img.shape # area is calculated as “height x width”area = height * width # display the areaprint("Area of the image is : ", area) |
Output :
Area of the image is : 50244
Example 2 : Consider the following image :

“image.jpg”
# import necessary libraryimport matplotlib.pyplot as plt # read an imageimg = plt.imread("image.jpg") # fetch the height and widthheight, width, _ = img.shape # area is calculated as “height x width”area = height * width # display the areaprint("Area of the image is : ", area) |
Output :
Area of the image is : 213200
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


