Last modified: Jan 15, 2025 By Alexander Williams

Python OpenCV cv2.resize() Guide

Resizing images is a common task in image processing. Python's OpenCV library provides the cv2.resize() function to resize images easily. This guide will help you understand how to use it effectively.

What is cv2.resize()?

The cv2.resize() function is used to resize an image. It allows you to change the dimensions of an image while maintaining its aspect ratio or not. This is useful for various applications like machine learning, computer vision, and more.

Syntax of cv2.resize()

The syntax for cv2.resize() is straightforward. Here’s how it looks:


cv2.resize(src, dsize, fx, fy, interpolation)

Parameters:

  • src: The source image you want to resize.
  • dsize: The desired size of the output image.
  • fx: Scale factor along the horizontal axis.
  • fy: Scale factor along the vertical axis.
  • interpolation: The interpolation method to use.

Example: Resizing an Image

Let’s look at a simple example to resize an image using cv2.resize(). We’ll use the cv2.imread() function to load an image and then resize it.


import cv2

# Load an image
image = cv2.imread('example.jpg')

# Resize the image to 300x300 pixels
resized_image = cv2.resize(image, (300, 300))

# Display the resized image
cv2.imshow('Resized Image', resized_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

In this example, the image is resized to 300x300 pixels. The cv2.imshow() function is used to display the resized image.

Using Scale Factors

You can also resize an image using scale factors. This method is useful when you want to maintain the aspect ratio of the image.


import cv2

# Load an image
image = cv2.imread('example.jpg')

# Resize the image by scaling it by 0.5 in both dimensions
resized_image = cv2.resize(image, None, fx=0.5, fy=0.5)

# Display the resized image
cv2.imshow('Resized Image', resized_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Here, the image is scaled down by 50% in both width and height. The None value for dsize indicates that the size is determined by the scale factors.

Interpolation Methods

Interpolation is the method used to estimate pixel values when resizing. OpenCV provides several interpolation methods. The most common ones are:

  • cv2.INTER_LINEAR: Bilinear interpolation (default).
  • cv2.INTER_NEAREST: Nearest-neighbor interpolation.
  • cv2.INTER_AREA: Resampling using pixel area relation.
  • cv2.INTER_CUBIC: Bicubic interpolation.

Here’s an example using different interpolation methods:


import cv2

# Load an image
image = cv2.imread('example.jpg')

# Resize using different interpolation methods
resized_linear = cv2.resize(image, (300, 300), interpolation=cv2.INTER_LINEAR)
resized_nearest = cv2.resize(image, (300, 300), interpolation=cv2.INTER_NEAREST)

# Display the resized images
cv2.imshow('Linear Interpolation', resized_linear)
cv2.imshow('Nearest Interpolation', resized_nearest)
cv2.waitKey(0)
cv2.destroyAllWindows()

This example shows the difference between bilinear and nearest-neighbor interpolation.

Conclusion

Resizing images is a fundamental task in image processing. The cv2.resize() function in OpenCV makes it easy to resize images while maintaining control over the output dimensions and quality. Whether you’re working on machine learning models or computer vision projects, mastering cv2.resize() is essential.

For more information on related topics, check out our guides on Python OpenCV cv2.imread() and Python OpenCV cv2.imshow().