Image Resizing using OpenCV | Python
Last Updated :
11 Aug, 2025
Resizing an image means changing its width and height, shrinking it for faster loading or enlarging it for better visibility. This is a common task in image processing and machine learning for various reasons:
- Reduce training time in neural networks by decreasing input image size, which lowers number of input nodes.
- Fit images to display or processing requirements sometimes images must meet specific size criteria.
- Zoom in or out on an image for better visualization.
Function Used
OpenCV provides a function called cv2.resize() to resize images easily. It supports different interpolation methods, which affect the quality and speed of resizing.
Syntax
cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]])
Parameters:
- src: Source/input image.
- dsize: Desired size (width, height). Order is width first, then height.
- dst(Optional): Output image, rarely used explicitly.
- fx(optional): Scale factor along the horizontal axis.
- fy(optional): Scale factor along the vertical axis.
- interpolation(optional): Interpolation method to use.
Note: Use either dsize or fx/fy for scaling:
dsize: when you know exact width & height.
fx/fy: when you want to scale by a factor.
Don’t set both together (unless dsize=None)
Interpolation Methods
Interpolation is the method used to decide pixel colors when an image is resized. Below are some methods:
Method | When to Use | Description |
---|
cv2.INTER_AREA | Shrinking an image | Best for downsampling, minimizes distortion. |
cv2.INTER_LINEAR | Zooming or general resizing | Default method, balances speed and quality. |
cv2.INTER_CUBIC | Zooming (high-quality) | Slower but better quality for enlarging images. |
cv2.INTER_NEAREST | Fast resizing | Fast but lower quality, can produce blocky results. |
Example
This code demonstrates different ways to resize an image in OpenCV. It loads an image, then resizes it:
- to 10% of its original size using INTER_AREA,
- to fixed dimensions (1050×1610) using INTER_CUBIC,
- to another fixed size (780×540) using INTER_LINEAR.
Finally, it displays all resized versions in a 2×2 grid using Matplotlib.
Python
import cv2
import matplotlib.pyplot as plt
image = cv2.imread(r"grapes.jpg", 1) # Load the image
# Resize to 10% of original size with INTER_AREA
half = cv2.resize(image, (0, 0), fx=0.1, fy=0.1, interpolation=cv2.INTER_AREA)
# Resize to fixed dimensions with INTER_CUBIC
bigger = cv2.resize(image, (1050, 1610), interpolation=cv2.INTER_CUBIC)
# Resize to specific size with INTER_LINEAR
stretch_near = cv2.resize(image, (780, 540), interpolation=cv2.INTER_LINEAR)
Titles = ["Original", "Resized 10%", "Resized to 1050x1610", "Resized 780x540"]
images = [image, half, bigger, stretch_near]
count = 4
# Plot all images in a 2x2 grid
for i in range(count):
plt.subplot(2, 2, i + 1) # Select subplot position
plt.title(Titles[i]) # Set title for current image
plt.imshow(cv2.cvtColor(images[i], cv2.COLOR_BGR2RGB)) # Convert BGR to RGB for display
plt.axis('off') # Hide axis ticks
plt.tight_layout()
plt.show()
Output
Output representing different way to resize image
Image Resizing using OpenCV in Python
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice