Skip to content
Related Articles

Related Articles

How to find width and height of an image using Python?
  • Last Updated : 13 Jan, 2021

In this article, we are going to discuss how to get the height and width of a particular image.

In order to find the height and width of an image, there are two approaches. The first approach is by using the PIL(Pillow) library and the second approach is by using the Open-CV library.

Approach 1:

PIL is the Python Imaging Library is an important module which is used for image processing. It supports many formats of images such as “jpeg”, “png”, “ppm”, “tiff”, “bmp”, “gif”.  It provides many image editing capabilities. The Image module provides a class with the same name which is used to represent a PIL image. 

PIL.Image.open() is used to open the image and then .width and .height property of Image are used to get the height and width of the image. The same results can be obtained by using .size property.



To use pillow library run the following command:

pip install pillow

Image Used:

Image Used

Code:

Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# import required module
from PIL import Image
  
# get image
filepath = "geeksforgeeks.png"
img = Image.open(filepath)
  
# get width and height
width = img.width
height = img.height
  
# display width and height
print("The height of the image is: ", height)
print("The width of the image is: ", width)

chevron_right


Output:

Alternative:

An alternate way to get height and width is using .size property.



Example:

Image Used:

Image Used

Code:

Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# import required module
from PIL import Image
  
# get image
filepath = "geeksforgeeks.png"
img = Image.open(filepath)
  
# get width and height
width,height = img.size
  
# display width and height
print("The height of the image is: ", height)
print("The width of the image is: ", width)

chevron_right


Output:

Approach 2:

OpenCV in python is a library which is used for computer vision, image processing and much more.  The imread(filepath) function is used to load an image from the file path specified. The .shape stores a tuple of height, width and no of channels for each pixel. The .shape[:2] will get the height and width of the image.

To install OpenCV run the following command:

pip install opencv-python

Image Used:

Code:

Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# import required module
import cv2
  
# get image
filepath = "geeksforgeeks.jpg"
image = cv2.imread(filepath)
#print(image.shape)
  
# get width and height
height, width = image.shape[:2]
  
# display width and height
print("The height of the image is: ", height)
print("The width of the image is: ", width)

chevron_right


Output:


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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :