How do I get Image size and dimensions in python

Published January 31, 2022

We will learn how to get the image size and the dimensions in Python in this post. We will create a Python program using the Python Imaging Library Module to get this information. Various classes within this module will allow us to determine the dimensions and size of an image.

To create an application that displays the size and dimension of an image, follow the steps below:

  • Step 1: Open your Python IDE and create a new Python application.

  • Step 2: Now, import the PIL module using from PIL import Image

Step 3: Using the open() method,  specify the path of  your image

img=Image.open("C:/Users/user2/Pictures/cache.PNG")

Step 4: You can obtain the size and the dimensions of your image using size= (W,H), where W is the width of the image and H is the height

w,h=img.size

 

Step 5: Now Print out your values

print("Width =",w,end="\t")

print("Height =",h)

 

In general your code should be

from PIL import Image

img=Image.open("C:/Users/user2/Pictures/cache.PNG")

w,h=img.size    # w=Width and h=Height

print("Width =",w,end="\t")

print("Height =",h)

 

Python get Image size

 

Python get Image size 2

 

Conlusion: In this python example we covered how to get Image information like image type, size...

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

225 Views