Contents
Pillow – Get Image Width
To get the width of an image in pixels with Pillow in Python, you can use PIL.Image.width attribute.
The attribute returns the width of this image in pixels.
In this tutorial, you will learn how to get the width of a given image with Pillow library, by writing some example programs.
Examples
1. Get width of given image in pixels
In the following program, we open an image using Image.open(), and then get the width of this image using width attribute of Image class object.
The input image we have taken has dimensions of 640x427, of which 640 is the width and 427 is the height. Therefore, when we get the width of the image, the width attribute must return 640.
Python Program
from PIL import Image
# Open an image
img = Image.open("test_image_house.jpg")
# Get image width
width = img.width
print(width)
CopyOutput
640
Summary
In this Python Pillow Tutorial, we described how to get the width of an image in pixels using PIL.Image.width attribute, with the help of examples.