Our Objective: To find image size using Python. We intend to find the width and the height/ resolution of any given image using 3 different python libraries.

Find the Image Size Using Python
Find Image Size Using Python

In this tutorial, we will be focussing on finding the resolution of any image using Python. We will be solving this problem statement using three different approaches. We are going to use Python’s OpenCV library, pillow library, and its pygame library for this purpose. There are more than these 3 approaches to this problem, but we’re mainly discussing these 3 in this article.

Working Structure

Before proceeding further, make sure to make a project folder with the project name. It will make things easy to locate. Make a file with the name index.py to write the code and also download one image on which you want to run the test code. Copy the image in your project folder only. In the code below, you will see that I have used one image named “Lions.jpg” to test my code.

Our test image "Lions.jpg"
Our test image “Lions.jpg”

Find Image Size Using Python’s Pillow Library

Python’s Pillow is a very powerful and known library. It is an open-source and free-to-use library that needs to be installed in your system additionally. Pillow is known for its image manipulation capabilities like resizing, opening, saving image files. It supports a wide variety of file formats.

Installing Pillow

Pillow installation is as easy and direct as installing any other external library. Just open your terminal and type in the following command.

pip install Pillow

Using Python Pillow

After the successful installation of Pillow, head over to your editor and open your project folder. Now type in the following code to get the resolution of your image.

from PIL import Image

with Image.open("Lions.jpg") as img:
    width, height = img.size

print(f"The width of the given image is: {width} and the height is : {height}")
  • Firstly, we have imported Image module from the Pillow Library.
  • Then using the “open” function, we’ve opened our image and assigned it in a variable named img.
  • We have created 2 variables named “width” and “height”. And assigned the values we’ve got from the “size” parameter. Size parameter is a 2-tuple, that contains width, height in pixels.
  • Finally using the f strings, we have printed the image resolution or the width and the height of our image.

Find Image Size Using Python’s OpenCV Library

OpenCV stands for Open Source Computer Vision Library. It is an immense python library that supports image processing, computer vision, and machine learning. It is a versatile library for image and video processing features like facial recognition & detection, photo editing, license plate reading, optical character recognition, and a lot more.

Installing OpenCV

Type in the following command in the command prompt and let the OpenCV library gets installed.

pip install opencv-python  

Using OpenCV

import cv2

img = cv2.imread("Lions.jpg")
width = img.shape[1]
height = img.shape[0]

print(f"The width of the given image is: {width} and the height is : {height}")
  • We have imported OpenCV module by importing its cv2 library.
  • After that, we have loaded our target image using its inbuilt “imread” function and stored it in a variable named ‘img’.
  • Then we have fetched the width and height of our loaded image using one of its attributes called “shape” attribute. And stored the width and height in variables named “width” and “height” respectively.
  • Lastly, we have printed our image dimensions using python’s f strings.

Find Image Size Using Python’s Pygame Library

Pygame is an open-source library that is used to write video games. It is an amalgamation of various computer graphics and sound libraries.

Installing Pygame

Installing pygame follows the same command which is followed to install most of the python libraries. Open your terminal and type in the following command to get started:

pip install pygame

Using Pygame

import pygame

img = pygame.image.load("Lions.jpg")
width = img.get_width()
height = img.get_height()

print(f"The width of the given image is: {width} and the height is : {height}")
  • We have imported “pygame”.
  • With the help of image.load function, load the target image and store in a variable namede “img”.
  • Using get_width() and get_height() functions, we have got the width and and height of our image.
  • Then we have stored the width and height in the respective variables.
  • Lastly, we have printed our image resolution using python’s f strings.

Output

Below is the common output that we have got after running all the three approaches explained above.

The width of the given image is: 1366 and the height is : 768

Make sure to try and explore all the mentioned libraries more. All the libraries mentioned in this article are endless treasures to explore.

Share the article if you’ve read it till here and don’t forget to write your suggestions/ feedback in the comments section down below.

Get in touch with us on our Social Media Handles:

Have a look at our popular posts:


Vaishali Rastogi

Hey There! I am Vaishali Rastogi and I am a tech-researcher. I've been doing writing for a good 4-5 years, so here I am now. Working on my own website to make people digitally aware of the updates in technology.

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *