Python Pillow - Resizing an Image


Advertisements

Most of the digital image is a two-dimensional plane of pixels and it has a width and height. The Image module from pillow library has an attribute size. This tuple consists of width and height of the image as its elements. To resize an image, you call the resize() method of pillow’s image class by giving width and height.

Resize and save the resized image

The program for resizing and saving the resized image is given below −

#Import required Image library
from PIL import Image

#Create an Image Object from an Image
im = Image.open("images/cat.jpg")

#Display actual image
im.show()

#Make the new image half the width and half the height of the original image
resized_im = im.resize((round(im.size[0]*0.5), round(im.size[1]*0.5)))

#Display the resized imaged
resized_im.show()

#Save the cropped image
resized_im.save('resizedBeach1.jpg')

Output

If you save the above program as Example.py and execute, it displays the original and resized images using standard PNG display utility, as follows −

Original Image

Original Image

Resized Image

resized_image.jpg
Advertisements