Image Scaling with Pillow in Python

Pillow – Image Scaling

Image scaling is the process of resizing an image to a new size. Scaling can be done to make the image larger (also called upscaling) or smaller (also called downscaling) than its original size.

When scaling an image, the pixels of resulting image are created by interpolating the color values of original pixels.

To scale an image in Python with Pillow library, you can use Image.resize() method.

In this tutorial, you will learn how to use Image.resize() to upscale or downscale an image.

Steps to scale image

The steps to overlay an image over another image with Pillow are

  1. Import Image module from Pillow library.
  2. Read the input image using Image.open() function.
  3. Define target image size. This is a tuple: (width, height).
  4. Scale the image using Image.resize() function. The scaled image is returned by the function.
  5. Save the scaled to required location using Image.save() function.

Examples

In the following examples, we take the following image whose original dimensions are: width = 640 pixels, and height = 427 pixels.

Image Scaling with Pillow in Python

We go through different scenarios of scaling an image.

1. Scale image to specific width and height

In the following example, we scale the given image to a specific width of 200 pixels and a height of 250 pixels.

Python Program

from PIL import Image

# Read the input image
img = Image.open("test_image_house.jpg")

# Target image size
size = (200, 250)

# Scale the image
scaled_img = img.resize(size)

# Save the scaled image
scaled_img.save('scaled_image.jpg')
Copy

Resulting image

Scale image to specific width and height with pillow in python

The aspect ratio of the original image is not preserved, since we have not considered the width:height of the given original image.

2. Down-scale the image to 50%

In this example, we down scale the given image to 50% of its original size. Since the size of original image is (640, 427), the size of the resulting image must be (320, 213).

Python Program

from PIL import Image

# Read the input image
img = Image.open("test_image_house.jpg")

# Target image size
scale = 0.5
size = (int(scale * img.size[0]), int(scale * img.size[1]))

# Scale the image
scaled_img = img.resize(size)

# Save the scaled image
scaled_img.save('scaled_image.jpg')
Copy

Resulting image

Down-scale the image with pillow in python

The aspect ratio is preserved because we decreased the width and height by same percentage.

3. Up-scale the image to 140%

In this example, we up scale the given image to 140% of its original size. Since the size of original image is (640, 427), the size of the resulting image must be (896, 597).

Python Program

from PIL import Image

# Read the input image
img = Image.open("test_image_house.jpg")

# Target image size
scale = 1.40 #140%
size = (int(scale * img.size[0]), int(scale * img.size[1]))

# Scale the image
scaled_img = img.resize(size)

# Save the scaled image
scaled_img.save('scaled_image.jpg')
Copy

Resulting image

Up-scale the image with pillow in python

Since the image is upscaled, you might observe blurring along the edges in the image. [The image might be fit to the website template, not exceeding a specific width. But when you run the program in your local machine, you would notice the difference in the size.]

Where is Image Scaling used?

Image scaling is a common technique used in various applications, such as image processing, computer vision, and computer graphics.

It can be used to prepare images for display on different devices with varying screen sizes and resolutions, as well as for data compression and storage purposes.

Summary

In this Python Pillow Tutorial, we learned how to scale an image to a specific width, height, or scale the image by a a specific scaling factor, using PIL.Image.resize() function, with the help of examples.

Code copied to clipboard successfully 👍