3 Ways to Resize Images in Python

Updated: January 7, 2024 By: Guest Contributor Post a comment

Introduction

Resizing images is a common requirement in software development, especially when dealing with media content, web development, or machine learning data preprocessing. Python, being versatile and supported by robust libraries, offers several methods to achieve this. We’ll go through a few popular ways to resize images in Python, touching upon the process, code examples, performance, and their respective pros and cons.

Using PIL/Pillow

The Python Imaging Library (PIL), also known as Pillow in its modern version, is one of the most widely used libraries for image processing tasks in Python. It offers simple syntax for resizing images and is quite capable of handling various file formats.

Steps:

  1. Install Pillow with pip install Pillow.
  2. Import the Image module from PIL.
  3. Open the image using the Image.open() method.
  4. Resize the image using the resize() method, specifying the desired size.
  5. Save or display the image as needed.

Example:

from PIL import Image

# Load the original image
original_image = Image.open('path/to/image.jpg')

# Resize the image
dimension = (100, 100)
resized_image = original_image.resize(dimension)

# Save the resized image
resized_image.save('path/to/resized_image.jpg')

Performance Discussion: PIL/Pillow performs well for general-use cases, particularly for scripts and applications not requiring real-time image processing. It may not be the best fit for high-performance applications or batch processing vast numbers of images.

Using OpenCV

OpenCV is a powerful library focused on computer vision and image processing. It is faster than PIL/Pillow and can be more suitable for image processing in real-time applications.

Steps to follow:

  1. Install OpenCV with pip install opencv-python-headless.
  2. Import the cv2 module.
  3. Read the image using the cv2.imread() function.
  4. Resize the image with the cv2.resize() function.
  5. Save or show the image using cv2.imwrite() or cv2.imshow(), respectively.

Example:

import cv2

# Load the original image
original_image = cv2.imread('path/to/image.jpg')

# Resize the image
dimension = (100, 100)
resized_image = cv2.resize(original_image, dimension)

# Save or display the image
cv2.imwrite('path/to/resized_image.jpg', resized_image)

OpenCV is optimized for performance, and it is a suitable choice for real-time image processing and systems with high processing requirements.

Using scikit-image

scikit-image is a collection of algorithms for image processing that is built on top of SciPy. It is designed to integrate well with sci-fi stacks and is often used in scientific research contexts.

Here’s the process to follow:

  1. Install scikit-image with pip install scikit-image.
  2. Import the io module from skimage to read images and transform module to resize.
  3. Read the image using io.imread().
  4. Resize the image with transform.resize().
  5. Save or display the image using io.imsave() or plt.imshow() respectively.

Example:

from skimage import io, transform
import matplotlib.pyplot as plt

# Load the original image
original_image = io.imread('path/to/image.jpg')

# Resize the image
dimension = (100, 100)
resized_image = transform.resize(original_image, dimension)

# Display the image
plt.imshow(resized_image)
plt.show()

The performance of scikit-image is generally good for most scientific and research applications, though it might not match the optimized performance of OpenCV in terms of raw speed.

Conclusion

There are several ways to resize images in Python, each with its own set of features, advantages, and trade-offs. PIL/Pillow is straightforward and convenient for simple tasks, while OpenCV offers high-performance processing, and scikit-image integrates well within the SciPy ecosystem, particularly in research. The choice of library will often depend on the specific needs of the project, whether those needs are based on ease of use, performance requirements, or the type of image processing tasks at hand. Python’s versatility ensures there is a solution suitable for nearly any image resizing need.