Python Pillow – Get Image Size

Python Pillow – Image Size

To get the size of image using Python Pillow, use size property of Image object. The size property returns the width and height of the image.

In this tutorial, we shall learn how to get the size of image, in other words, width and height of an image, using Pillow library.

Syntax – Image.size

The syntax to use size property of PIL Image object is given below.

im = Image.open("sample-image.png")
im.size
Copy

Examples

1. Get size of given image

In the following program, we will read and image and then print its size using size property of the Image.

Python Program

from PIL import Image

#read the image
im = Image.open("sample-image.png")

#image size
print(im.size)
Copy

Output

(640, 400)

2. Access width and height from image size

You can access the height and width from size property using index. In the following example, we will get width and height of the image.

Python Program

from PIL import Image

#read the image
im = Image.open("sample-image.png")

#image size
width = im.size[0]
height = im.size[1]

print('Width  of the image is:', width)
print('Height of the image is:', height)
Copy

Output

Width  of the image is: 640
Height of the image is: 400

Summary

In this tutorial of Python Examples, we learned how to get the size of an image using Python Pillow library, with the help of well detailed example programs.

Code copied to clipboard successfully 👍