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

Finding Width and Height of an Image with Python

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

Understanding the image’s width and height is important whether you are resizing an image for your project or preparing images to feed machine learning models. Finding the dimension of the image is not a gigantic task. With the proper library installed, you can easily find it.

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

Image's width and height

Here are three ways:

  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 below image:

Normal Image

Method 1: Using PIL’s image.size

The Pillow is an image-editing library that is used to open, manipulate, or save the library.

Import the Image module from the pillow, open the image using the .open() function, get the dimension using image.size, and print it.

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 like JPG, PNG, or 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 fairly 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.

The 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 already have 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 not installed already 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 do not use this approach as it is a plotting library and does not support all the image formats. However, if you are working with plotting and do not want to install other libraries then you can use it.

Conclusion

My recommendation would be to use the OpenCV approach to find the dimension if you are already working with computer vision tasks. If you are looking for a simple and lightweight approach, use the pillow module.

That’s all!

Post Views: 75
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.

Rotating an Image with Python [ROI, Affine, Perspective Rotations]
Cloning 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