The Wayback Machine - https://web.archive.org/web/20250126161703/https://www.geeksforgeeks.org/image-resizing-using-opencv-python/
Open In App

Image Resizing using OpenCV | Python

Last Updated : 09 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Image resizing refers to the scaling of images. Scaling comes in handy in many image processing as well as machine learning applications. It helps in reducing the number of pixels from an image and that has several advantages e.g. It can reduce the time of training of a neural network as the more the number of pixels in an image more is the number of input nodes that in turn increases the complexity of the model.
It also helps in zooming in on images. Many times we need to resize the image i.e. either shrink it or scale it up to meet the size requirements. OpenCV provides us several interpolation methods for resizing an image.

Choice of Interpolation Method for Resizing:

  • cv2.INTER_AREA: This is used when we need to shrink an image.
  • cv2.INTER_CUBIC: This is slow but more efficient.
  • cv2.INTER_LINEAR: This is primarily used when zooming is required. This is the default interpolation technique in OpenCV.

Syntax: cv2.resize(source, dsize, dest, fx, fy, interpolation)

Parameters:

  • source: Input Image array (Single-channel, 8-bit or floating-point) 
  • dsize: Size of the output array
  • dest: Output array (Similar to the dimensions and type of Input image array) [optional]
  • fx: Scale factor along the horizontal axis  [optional]
  • fy: Scale factor along the vertical axis  [optional]
  • interpolation: One of the above interpolation methods  [optional]

Below is the code for resizing: 

Python
import cv2
import matplotlib.pyplot as plt

image = cv2.imread(r"D:\sims\eb\sim21\EB-ML-06-10-2022-Test-Output-15\PERFORATION\Overkill\Fail\Blister 1 2022-03-12 12-59-43.859 T0 M0 G0 3 PERFORATION Mono.bmp", 1)
# Loading the image

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_LINEAR)


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: 


Note: One thing to keep in mind while using the cv2.resize() function is that the tuple passed for determining the size of the new image ((1050, 1610) in this case) follows the order (width, height) unlike as expected (height, width).

Image Resizing using OpenCV | Python – FAQs

How to resize images using OpenCV?

To resize an image using OpenCV, you use the cv2.resize() function. This function takes an image and resizes it to the specified dimensions.

import cv2

# Load the image
image = cv2.imread('image.jpg')

# Resize the image
resized_image = cv2.resize(image, (width, height))

What are the parameters for resizing images in OpenCV?

The cv2.resize() function has several parameters:

  • src: The source image to resize.
  • dsize: The desired size of the output image as a tuple (width, height).
  • fx and fy: Scale factors along the x and y axis (optional, used instead of dsize).
  • interpolation: The interpolation method to use (optional). Common options include:
    • cv2.INTER_NEAREST
    • cv2.INTER_LINEAR (default)
    • cv2.INTER_CUBIC
    • cv2.INTER_LANCZOS4

How to maintain aspect ratio when resizing images in Python?

To maintain the aspect ratio, you need to calculate the new dimensions based on the desired size while preserving the original aspect ratio.

import cv2

# Load the image
image = cv2.imread('image.jpg')

# Get the original dimensions
(h, w) = image.shape[:2]

# Desired width
new_width = 800

# Calculate the aspect ratio
aspect_ratio = h / w
new_height = int(new_width * aspect_ratio)

# Resize the image
resized_image = cv2.resize(image, (new_width, new_height))

What are the common algorithms for image resizing in OpenCV?

OpenCV provides several interpolation algorithms for resizing:

  • cv2.INTER_NEAREST: Nearest-neighbor interpolation.
  • cv2.INTER_LINEAR: Bilinear interpolation (default).
  • cv2.INTER_CUBIC: Bicubic interpolation.
  • cv2.INTER_LANCZOS4: Lanczos interpolation over 8×8 neighborhood.

How does image resizing affect image quality in Python?

Resizing can affect image quality in several ways:

  • Loss of Detail: Reducing the size can cause loss of detail and make the image appear blurry.
  • Artifact Introduction: Enlarging an image might introduce artifacts or make it appear pixelated.
  • Interpolation Artifacts: The choice of interpolation method can affect the smoothness and sharpness of the resized image.

Choosing an appropriate interpolation method and being mindful of the resizing ratio can help mitigate quality loss.



Next Article
Practice Tags :

Similar Reads

three90RightbarBannerImg