GET HEIGHT AND WIDTH OF AN IMAGE IN PYTHON

In this tutorial, we will discuss how to get the height and width of an image by using a Python program. It is very easy to find the width and height of an image.

  • Prerequisite: Install pillow library, to install run the following command:
pip install pillow
  • Import the Image class from the PIL (Python Imaging Library) module to access the image.
from PIL import Image
  • PIL.Image.open(): Opens and identifies the given image file. This is a lazy operation; this function identifies the file, but the file remains open, and actual image data is not read from the file until you try to process the data (or call the load() method).
image=Image.open("https://www.codespeedy.com/wp-content/themes/CodeSpeedy-March-2019/img/logo/CodeSpeedy-Logo.png")
  • Get the width and height of an image and assign them to variables.
  width=image.width
  height=image.height
  • Print the above variables to see the width and height of an image.
print("Width:",width)
print("Height:",height)
  • Final code will be like this:
from PIL import Image
image=Image.open("https://www.codespeedy.com/wp-content/themes/CodeSpeedy-March-2019/img/logo/CodeSpeedy-Logo.png")
width=image.width
height=image.height
print("Width: ",width)
print("Height: ",height)
  • Final Output:
  • Width: 194
  • Height: 169

 

 

 

 

Leave a Comment

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

Scroll to Top