Skip to content
Related Articles

Related Articles

Improve Article

Python | Working with the Image Data Type in pillow

  • Difficulty Level : Basic
  • Last Updated : 26 Jul, 2019

In this article, we will look into some attributes of an Image object that will give information about the image and the file it was loaded from. For this, we will need to import image module from pillow.

Image we will be working on :

size() method – It helps to get the dimensions of an image.

IMG = Image.open(Image_path)
croppedIm = IMG.size




# import Image module
from PIL import Image
  
# open the image
catIm = Image.open('D:/cat.jpg')
  
# Display the dimensions of the image
print(catIm.size)

Output :



(400, 533)

Getting height and width separately – It helps us to get the height and width of the image.




# import Image module
from PIL import Image
  
# Open the image
catIm = Image.open('D:/cat.jpg')
  
# Create two different variables 
# The first one will contain width and 
# the second one will contain height
width, height = catIm.size
  
# Display height and width
print(height)
print(width)

Output :

400
533

 
filename() method – It helps us to get the filename of the image.

IMG = Image.open(Image_path)
croppedIm = IMG.filename




# import the Image module
from PIL import Image
  
# Open the image
catIm = Image.open('D:/cat.jpg')
  
# print the filename
print(catIm.filename)

Output :

D:/cat.jpg

 
format() method – It helps us to get the format the image is in.

IMG = Image.open(Image_path)
croppedIm = IMG.format




# import the image
from PIL import Image
  
# open the image
catIm = Image.open('D:/cat.jpg')
  
# print the format of the image
print(catIm.format)

Output :

JPEG

 
format_description() method – It helps us to get the format description of the image.

IMG = Image.open(Image_path)
croppedIm = IMG.format_description




# import the image
from PIL import Image
  
# open the image
catIm = Image.open('D:/cat.jpg')
  
# print the format description of the image
print(catIm.format_description)

Output :

JPEG (ISO 10918)

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :