Image Resize in Python

Fariba Laiq Oct 10, 2023
Image Resize in Python

The imresize() method is used to resize an image in Python, available in the scipy module.

But unfortunately, this method is now deprecated in scipy 1.0.0 and will be completely removed from scipy 1.3.0. So we have to use another way to resize an image in Python.

Resize an Image Using resize() Method of PIL Module in Python

The PIL is an acronym for Python Imaging Library, which contains some modules for image processing. We can resize an image using the resize() method of the Image class available in the PIL module.

First, install the PIL module:

pip install pillow

We will import the Image class from the PIL module and the display() method from the IPython.display module.

We will put an image in our relative path. Then use the display() method to display the original image.

from PIL import Image
from IPython.display import display

print("Original Image")
im = Image.open("img.jpg")
display(im)
resized_im = im.resize((round(im.size[0] * 0.5), round(im.size[1] * 0.5)))
print("Resized Image")
display(resized_im)
resized_im.save("resized.jpg")

Output:

Image Resize in Python

We have resized the image using the resize() method and passed the length and width of the required image as an integer tuple.

Here we have resized the original image by half its length and width. After that, we have displayed and saved the resized image.

Author: Fariba Laiq
Fariba Laiq avatar Fariba Laiq avatar

I am Fariba Laiq from Pakistan. An android app developer, technical content writer, and coding instructor. Writing has always been one of my passions. I love to learn, implement and convey my knowledge to others.

LinkedIn

Related Article - Python Image