Open In App

Python | Working with the Image Data Type in pillow

Last Updated : 26 Jul, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
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
Python3
# 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. Python3
# 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
Python3
# 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
Python3
# 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
Python3
# 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)

Next Article
Practice Tags :

Similar Reads