Python cv2 resize() Method

Python cv2.resize() method is “used to resize images.” It allows you to resize images while maintaining their aspect ratio or to a specific size.

Resizing an image means changing its dimensions of it. The dimensions can be width, height, or both. Also, the original image’s aspect ratio could be preserved in the resized image.

Interpolation Method for Resizing Options

  1. cv2.INTER_AREA: This option is used when scaling down an image.
  2. cv2.INTER_CUBIC: This option is slow but more efficient.
  3. cv2.INTER_LINEAR: This option is primarily used when zooming is required. This is the default interpolation technique in OpenCV.

Syntax

cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]])

Parameters

Parameter Description
src This parameter is required, and it is the source/input image.
dsize This is the required parameter and the desired size for the output image.
fx This is an optional parameter and scale factor along the horizontal axis.
fy This is the optional scale factor along the vertical axis.
interpolation This is the optional flag that takes one of the following methods.

INTER_NEAREST – It is the nearest-neighbor interpolation

INTER_LINEAR – It is the bilinear interpolation (used by default)

INTER_AREA – It is the resampling using pixel area relation. It may be a preferred function for image decimation, as it gives moire’-free results. But when an image is zoomed, it is similar to the INTER_NEAREST method.

INTER_CUBIC – It is the bicubic interpolation over a 4×4 pixel neighborhood INTER_LANCZOS4 – It is the Lanczos interpolation over an 8×8 pixel neighborhood.

Example 1: How to Use cv2.resize() method in Python

# Importing cv2
import cv2
import matplotlib.pyplot as plt

# Path
path = 'cropped_image.jpg'

# Reading an image in default mode
image = cv2.imread(path)

half = cv2.resize(image, (0, 0), fx=0.1, fy=0.1)
bigger = cv2.resize(image, (1050, 1610))

stretch_near = cv2.resize(image, (780, 540),
 interpolation=cv2.INTER_NEAREST)

Titles = ["Original", "Half", "Bigger", "Interpolation Nearest"]
images = [image, half, bigger, stretch_near]
count = 4

for i in range(count):
  plt.subplot(2, 2, i + 1)
  plt.title(Titles[i])
  plt.imshow(images[i])

plt.show()

Output

Python cv2 resize

Example 2: Upscale – Resize and Preserve Aspect Ratio

import cv2

img = cv2.imread('Krunal.jpg', cv2.IMREAD_UNCHANGED)

print('Original Dimensions : ', img.shape)

scale_percent = 200
width = int(img.shape[1] * scale_percent / 100)
height = int(img.shape[0] * scale_percent / 100)
dim = (width, height)

resized = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)

print('Resized Dimensions : ', resized.shape)

cv2.imshow("Resized image", resized)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output

Upscale – Resize and Preserve Aspect Ratio

Example 3: Resize only the width and do not preserve Aspect Ratio

import cv2

img = cv2.imread('Krunal.jpg', cv2.IMREAD_UNCHANGED)

print('Original Dimensions : ', img.shape)

width = 660
height = img.shape[0]
dim = (width, height)

resized = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)

print('Resized Dimensions : ', resized.shape)

cv2.imshow("Resized image", resized)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output

Resize only the width and do not preserve Aspect Ratio

Example 4: Resize to a specific width and height

import cv2

img = cv2.imread('Krunal.jpg', cv2.IMREAD_UNCHANGED)

print('Original Dimensions : ', img.shape)

width = 660
height = 450
dim = (width, height)

resized = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)

print('Resized Dimensions : ', resized.shape)

cv2.imshow("Resized image", resized)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output

Resize to specific width and height

That’s it.

1 thought on “Python cv2 resize() Method”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.