Resize an Image using Python Pillow (PIL)

Resize Image In Python Using Pillow

Resizing an image can be tricky, but luckily Python has us covered with Pillow, a fork of Python Imaging Library (PIL). The following file formats are supported by Python Pillow: JPEG, PNG, TIFF, PPM, GIF, and BMP.

Pillow is an open-source Python library for image manipulation and processing. It is designed to improve upon PIL and provide a quick and easy way to develop Python imaging applications.

That’s great, we’re almost there! Let’s just get started learning how to resize images in python using a pillow library.

How to Resize an Image using Pillow (PIL)

To start with, we use the resize method from the PIL library

Syntax: Image.resize(size, resample=0)

Parameters of the method:

  • Size: Should be in pixels, as a tuple(width, height).
  • Resample: Optional. can be one of PIL.Image.BILINEAR (linear interpolation), PIL.Image.NEAREST (use nearest neighbor), PIL.Image.LANCZOS (a high-quality downsampling filter), or PIL.Image.BICUBIC (cubic spline interpolation).
  • Return Value: A resized copy of the input image.

1. Installing Pillow

pip install Pillow

Though it may seem unusual to you, the Pillow library is imported using import PIL.

2. Import the module

We start with importing the Pillow module, using the code:

from PIL import Image

3. Select and open the Image

Now we need to pass the image, which we want to resize in the object of the PIL module.

Here, the ‘img_sample.jpg’ used image sample is stored at the same location of the python code file, if not then you will need to specify the file name with its location too as – ‘/images/sample.jpg’

This step creates an object to our image and loads it to the Pillow library.

# Creating Image Object

img ='img_sample.jpg')

As an optional part, it comes to us to display the image that we just passed to the open method, using the show() method as:

# Display original image

The result of the show method in our code results in the below sample image (original image) –

Original Image
Original Image

4. Resize the image

We use the resize() method to our image object created in the previous step and pass the desired dimension (size) of our image in terms of – width x height. The value of width and height can be anything as per user choice.

res_img = img.resize((400,300))

To see our resized image, we again use the show() method as:

# Display resized image

In order to save our resized image, we use the save() method on the resized image object.

For this purpose, we also need to pass the desired new name to our newly resized image file to the save() method as a parameter.'resized_img.jpg')

The final output image, which is displayed using the show method for the resized image is –

Resized Image
Resized Image

Complete code to resize an image using PIL

from PIL import Image

# Creating Image Object
img ='img_sample.png')

# Display original image

res_img = img.resize((400,300)) #size can be anything of our choice

# Display resized image

# To save the resized image'resized_img.png')

5. Crop Image

If you wish to crop an image then you may do so using the crop() method of the PIL library, which takes 4 arguments for left, top, right, bottom in the same order.

cropped_img = img_sample.crop((left, top, right, bottom))

Replace the loft, top, right, bottom placeholders with the image coordinates here.


That’s it for the tutorial! Hope you have learned well how to resize images in python using the pillow (PIL) library and can easily implement it in your code to resize images.