close
The Wayback Machine - https://web.archive.org/web/20210204165053/https://www.geeksforgeeks.org/image-resizing-using-opencv-python/amp/

Image Resizing using OpenCV | Python

Image resizing refers to the scaling of images. Scaling comes 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 more is 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 images. Many times we need to resize the image i.e. either shirk it or scale up to meet the size requirements. OpenCV provides us several interpolation methods for resizing an image.

Choice of Interpolation Method for Resizing –

Below is the code for resizing.

filter_none

edit
close

play_arrow

link
brightness_4
code

import cv2
import numpy as np
import matplotlib.pyplot as plt 
% matplotlib qt
# This is a magic command to display in an external window
  
image = cv2.imread("C://gfg//tomatoes.jpg", 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_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()
chevron_right

Output:



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

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

Article Tags :