Python Pillow (PIL) Image Resize with Image.Resize() Function

Introduction

Pillow (PIL) is a popular Python library for image processing tasks. One of its common functions is image.resize() which lets you resize the image easily. In this article, we will understand the syntax and various parameters of the pillow image resize function, along with multiple examples.

Syntax of Pillow Image.resize() 

The syntax of pillow image.resize() function is as follows –

image.resize(size, resample=None, box=None, reducing_gap=None)

  • size: The desired dimensions for the resized image (width, height).
  • resample (optional): The interpolation method to be used during resizing.
  • box (optional): A tuple representing the region of interest within the image to be resized.
  • reducing_gap (optional): This parameter is used to optimize the performance of resize operation

Example of Pillow Image Resize

Import Libraries

To start with we import the Image module from the PIL library. We also make use IPython display module to use it to display images in Google Colab.

In [0]:

from PIL import Image
from IPython.display import display

Read Input Image

Let us now import an input image that will be used in all the below examples. Python display is used to display the image and its size is also printed by using image.resize attribute. As we can see from the output, the input image has a size of 3000×2000.

In [1]:

image = Image.open("/content/bird.jpg")

width, height = image.size
print("Image Size: {} x {}".format(width, height))

display(image)

Out[1]:

Image Size: 3000 x 2000

pillow image resize example

Example 1: Simple Image Resize with Pillow image.resize() Function

This is the simplest example Pillow image resize, we just need to pass the required new size to image.resize() function. Here we passed the new size as (800,600) and after resizing, we can see the new size is indeed 800×600 in the output.

In [2]:

new_size = (800, 600)
resized_image = image.resize(new_size)

width, height = resized_image.size
print("Resized Image: {} x {}".format(width, height))

Out[2]:

Resized Image: 800 x 600

Example 2: Relative Resize of Image with Pillow

Often you would be required to resize the image with a scale factor, instead of passing absolute values. The below example of Pillow image resize shows how this can be done.

In [3]:

scale_factor = 0.25
new_size = (int(image.size[0] * scale_factor), int(image.size[1] * scale_factor))

resized_image = image.resize(new_size)

width, height = resized_image.size
print("Resized Image: {} x {}".format(width, height))

Out[3]:

Resized Image: 750 x 500

Example 3: Resizing Image with Fixed Width

This example shows how you can resize image with PIL library by keeping the width fixed.

In[4]:

new_width = 500
resized_image = image.resize((new_width, int(image.size[1] * (new_width / image.size[0]))))

width, height = resized_image.size
print("Resized Image: {} x {}".format(width, height))

Out[4]:

Resized Image: 500 x 333

 

Example 4: Resizing Image with Fixed Height

In this example, we resize image with PIL library by keeping the height fixed.

In [5]:

new_height = 300
resized_image = image.resize((int(image.size[0] * (new_height / image.size[1])), new_height))

width, height = resized_image.size
print("Resized Image: {} x {}".format(width, height))

Out[5]:

Resized Image: 450 x 300

 

Example 5: Resizing Image with Nearest Neighbor Interpolation

The resample parameter of the Image.resize() function allows you to specify the interpolation method used during the resizing process.

Nearest Neighbor interpolation selects the nearest pixel from the original image and assigns its color to the corresponding pixel in the resized image. It is the fastest but can result in pixelation.

To use Nearest Neighbor interpolation in Pillow image resize, set the resample parameter to Image.NEAREST as shown in the below example.

In [6]:

new_size = (800, 600)
resized_image = image.resize(new_size, resample=Image.NEAREST)

width, height = resized_image.size
print("Resized Image: {} x {}".format(width, height))

Out[6]:

Resized Image: 800 x 600

Example 6: Resizing Image with Bilinear Interpolation

Bilinear interpolation calculates the color value of a pixel in the resized image by taking a weighted average of the surrounding pixels in the original image. This method produces smoother results compared to Nearest Neighbor.

To use Bilinear interpolation in PIL image resize, set the resample parameter to Image.BILINEAR as shown in the below example.

In [7]:

new_size = (800, 600)
resized_image = image.resize(new_size, resample=Image.BILINEAR)

width, height = resized_image.size
print("Resized Image: {} x {}".format(width, height))
Out[7]:
Resized Image: 800 x 600

Example 7: Resizing Image with Bicupic Interpolation

Bicubic interpolation takes a weighted average of a larger number of surrounding pixels in the original image, resulting in even smoother resizing. It is slower than Bilinear but produces higher-quality results.

To use Bicubic interpolation for Pillow image resize, set the resample parameter to Image.BICUBIC as shown below.

In [8]:

new_size = (800, 600)
resized_image = image.resize(new_size, resample=Image.BICUBIC)

width, height = resized_image.size
print("Resized Image: {} x {}".format(width, height))

Out[8]:

Resized Image: 800 x 600

 

Example 8: Optimizing with Reducing Gap Parameter

The reducing_gap parameter is used to optimize the resizing performance of image.resize() function. It is useful for large images and can speed up the resizing process when a smaller value for this parameter is used. For higher values, the image is closer to fair sampling. The default value is None, which means the optimization is disabled.

In [9]:

new_size = (4500, 3500)
resized_image = image.resize(new_size, reducing_gap=2.0)

width, height = resized_image.size
print("Resized Image: {} x {}".format(width, height))
Out[9]:
Image Size: 4500 x 3500

Example 9: Using Box Parameter

The box parameter allows you to specify a region of interest within the image to be resized. It takes a tuple representing the coordinates (left, upper, right, lower) of the bounding box. Only the pixels within this box will be resized using this.

In [10]:

new_size = (450, 350)
bounding_box = (100, 100, 500, 500)
resized_image = image.resize(new_size, box=bounding_box)

width, height = resized_image.size
print("Resized Image: {} x {}".format(width, height))
Out[10]:
Resized Image: 450 x 350

 

Reference: Pillow Documentation

  • MLK

    MLK is a knowledge sharing community platform for machine learning enthusiasts, beginners and experts. Let us create a powerful hub together to Make AI Simple for everyone.

Follow Us

Leave a Reply

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