Python | Working with the Image Data Type in pillow

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)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.