Python | Working with the Pillow Image Data Type

Python Methods and Functions

Image we'll be working on:

The size () method — helps to get the image dimensions.

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

# Image module import

from PIL import Image

 
# open image

catIm = Image. open ( 'D: / cat.jpg' )

  
# Display image dimensions

print (catIm.size)

Output:

 (400, 533) 

Getting height and width separately — this helps us get height and width images.

# import Image module

from PIL import Image

 
# Open the image

catIm = Image. open ( 'D: / cat.jpg' )

 
# Create two different variables
# The first will contain the width and
# the second will contain the height

width, height = catIm.size

 
# Display height and width

print (height)

print (width)

Output:

 400 533 

The filename () method — helps us get the name of the image file.

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

# import Image module

from PIL import Image

  
# Open the image

catIm = Image. open ( ' D: / cat.jpg' )

 
# print file name

print (catIm.filename)

Output:

 D: /cat.jpg 

format () method — helps us get the image format.

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

# import image

from PIL import Image

 
# open image

catIm = Image. open ( 'D: / cat. jpg' )

 
# print image format

print (catIm. format )

Output:

 JPEG 

The format_description () method — helps to get a description of the image format.

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

# import image

from PIL import Image

 
# open image

catIm = Image. open ( 'D: / cat.jpg' )

  
# print image format description

print (catIm.format_description)

Output:

 JPEG (ISO 10918)