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
Example 1: Get Image Size
In the following program, we will read and image and then print its size using size property of the Image.
from PIL import Image #read the image im = Image.open("sample-image.png") #image size print(im.size)
Example 2: Access Width and Height of Image using Index
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.
from PIL import Image #read the image im = Image.open("sample-image.png") #image size width = im.size height = im.size print('Width of the image is:', width) print('Height of the image is:', height)
Width of the image is: 640 Height of the image is: 400
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.