Sunday, November 17, 2024
Google search engine
HomeLanguagesPython | Working with the Image Data Type in pillow

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




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

RELATED ARTICLES

Most Popular

Recent Comments