Python Pillow resize image

Resizing Images with Python Pillow

Most digital images are two-dimensional surfaces composed of pixels, with a width and height. The Image module in the Pillow library has a size attribute. This tuple consists of the image’s width and height as its elements. To resize an image, you can call the resize() method of the pillow’s Image class by providing the width and height.

Resize and save the resized image

The program to resize and save the resized image is shown 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 image
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, it will display the original and resized images using standard PNG display tools, as shown below.

Original Image

Python Pillow - Resize Image

Resized Image

Python Pillow - Resize Image

Leave a Reply

Your email address will not be published. Required fields are marked *