Write a Python Program to Find the Size (Resolution) of a Image

As a Python programmer, it’s essential to know how to find the size or resolution of an image, especially when dealing with image processing, computer vision, and machine learning projects.

The size or resolution of an image represents the number of pixels in width and height.

In this tutorial, we’ll cover how to find the size of an image using Python.


To get started, we’ll need to install the Pillow library, which is a fork of the Python Imaging Library (PIL).

Pillow provides extensive support for opening, manipulating, and saving many different image file formats.

We can install the Pillow library using pip, a package manager for Python, by running the following command:

pip install Pillow

After installing the Pillow library, we can use the Image module to open and load an image.

We can then use the size attribute to find the size of the image in pixels.

Here’s an example:

from PIL import Image

# Open the image file
image = Image.open('path/to/image.jpg')

# Get the size of the image in pixels
width, height = image.size

# Print the size of the image
print(f'The size of the image is {width} x {height} pixels.')

In the example above, we first import the Image module from the Pillow library.

We then open the image file using the Image.open() method and pass the path to the image file as an argument.

We then use the size attribute to get the width and height of the image in pixels.

Finally, we print the size of the image using an f-string.

It’s important to note that the size attribute returns a tuple of the form (width, height), where width is the number of pixels in the horizontal direction and height is the number of pixels in the vertical direction.


In conclusion, finding the size or resolution of an image using Python is straightforward using the Pillow library.

We can use the size attribute of the Image module to get the width and height of an image in pixels.

Knowing the size of an image is crucial when working with image processing, computer vision, and machine learning projects, as it provides essential information for further analysis and manipulation of the image.