Skip to content
Related Articles

Related Articles

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


Output :



(400, 533)

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


Output :

400
533

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

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :