Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Find Width and Height of an Image with Python

  • 28 Oct, 2024
  • Com 0
Finding Width and Height of an Image with Python

The dimension of an image is defined by its width and height and typically represented as (w × h).

Image's width and height

Here are three ways to find a height and width of an image in Python:

  1. Using Pillow’s image.size 
  2. Using cv2’s image.shape
  3. Using matplotlib’s image.shape

For this practical, I will be using the image below:

Normal Image

Method 1: Using PIL’s image.size

The Pillow is an image-editing library used to open, manipulate, and save images.

Import the Image module from Pillow, open the image using the .open() function, and retrieve the dimensions using the image.size attribute, and print them.

Decision Tree Diagram

Using the Pillow Library to get image height and widthInstall the pillow library if not installed using the below command:

pip install pillow

Code example

from PIL import Image

# Opening the input image file
image = Image.open('krunal.png')

# Getting width and height of the image
width, height = image.size

# Printing the dimensions (width and height)
print(f"The image width is {width} pixels.")
print(f"The image height is {height} pixels.")

Output

The image width is 558 pixels.

The image height is 558 pixels.

The pillow is a lightweight approach to finding the image dimensions. It can handle various image formats, including JPG, PNG, and BMP.

However, it does not support computer vision functions. For that, use the opencv module.

Method 2: Using cv2’s image.shape

The cv2 module provides a .shape attribute to extract the width (shape[1]) and height (shape[0]) from the shape. After getting it, print it, and if any error occurs, then display it to the user.

Decision Tree Diagram

Decision Tree diagram using cv2's image.shape

The above diagram is pretty simple.

We defined each process in a step of the diagram and finally ended the process.

To work with opencv in Python, we need to install it using the command below:

pip install opencv-python

Code example

import cv2

# Reading the input image file
image = cv2.imread('krunal.png')

# Extracting image dimensions (width and height)
dimensions = image.shape
width = image.shape[1]
height = image.shape[0]


# Displaying the dimensions
print(f"The image width is {width} pixels.")
print(f"The image height is {height} pixels.")

Output

The image width is 558 pixels.

The image height is 558 pixels.

OpenCV is a highly optimized and efficient approach that can be used for image and video processing. However, it can be overwhelming for simple tasks like this.

Only use this approach when you are doing computer vision tasks because, in that case, you have already installed opencv on your machine.

Method 3: Using matplotlib’s image.shape

Matplotlib library is the least likable approach you can use because it is a charting library and not an imaging library, but it does provide a function to edit the image.

Decision Tree Diagram

Decision Tree of Using matplotlib's image.shape

Use mpimg.imread() function to load the input image, use the image.shape to get the height and width of the image, and print it to the console so that the user can see it.

To work with the matplotlib library in Python, we need to install it if it is not already installed using the command below:

pip install matplotlib

Code example

import matplotlib.image as mpimg

# Reading the image file using mpimg
image = mpimg.imread('krunal.png')

# Getting dimensions (height and width)
height = image.shape[0]
width = image.shape[1]

# Displaying the dimensions
print(f"The image width is {width} pixels.")
print(f"The image height is {height} pixels.")

Output

The image width is 558 pixels.

The image height is 558 pixels.

I suggest you refrain from using this approach, as it is a plotting library that does not support all image formats. However, if you are working with plotting and do not want to install other libraries, then you can use it.

That’s all!

Post Views: 96
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Rotate an Image with Python
How to Clone an Image with Python

Leave a Reply Cancel reply

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

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend