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
- cv2.INTER_AREA: This option is used when scaling down an image.
- cv2.INTER_CUBIC: This option is slow but more efficient.
- 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
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
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
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
That’s it.

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.
Thanks for the very good examples!