AppDividend
Latest Code Tutorials

Python cv2 resize: How to Resize Image in Python

0

Image processing is one of the most performed tasks in this digital world. Image rotate, resize, and adding different filters are frequent operations that we perform regularly. Image resizing refers to the scaling of images. You can either scale up or scale down the image. Scaling comes very handy in machine learning applications.

In this example, we will see how to resize Image in Python using the OpenCV library. 

First, we import the cv2 module and then use the cv2.resize() method to scale the images.

Python cv2 resize

To resize images in Python using OpenCV, use cv2.resize() method. OpenCV provides us number of interpolation methods to resize the image.

Resizing the image means changing the dimensions of it. The dimensions can be a width, height, or both. Also, the aspect ratio of the original image could be preserved in the resized image. To resize an image, OpenCV provides cv2.resize() function.

Interpolation Method for Resizing Options

  1. cv2.INTER_AREA: This option is used when we need need to scale 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

ParameterDescription
srcThis parameter is required, and it is the source/input image.
dsizeThis is the required parameter, and it is the desired size for the output image.
fxThis is an optional parameter and scale factor along the horizontal axis.
fyThis is the optional scale factor along the vertical axis.
interpolationThis 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 4×4 pixel neighborhood INTER_LANCZOS4 – It is the Lanczos interpolation over 8×8 pixel neighborhood.

Example

# 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

In this example, we have imported cv2 and matplotlib libraries.

In the next step, we have defined an image path and reading the image using the imread() method.

Then we are resizing images with four different options using the cv2.resize() method.

  1. Normal Image
  2. Half Image
  3. Bigger Image
  4. Stretch_near Image

And then we are displaying image one by one using for loop.

We are using a matplotlib library to plot the four images with its title.

Downscale with resize()

In the following code, scale_percent value holds the percentage by which the image has to be scaled. Providing a value <100 downscales the image provided.

We will use this scale_percent value along with the original image’s dimensions to calculate the width and height of the output image.

We will provide a 20% scale_percent. So it will resize to 20% of the original image size.

# Importing cv2
import cv2

# Path
path = 'cropped_image.jpg'

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

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

scale_percent = 20  # percent of original size
width = int(image.shape[1] * scale_percent / 100)
height = int(image.shape[0] * scale_percent / 100)
dim = (width, height)
# resize image
resized = cv2.resize(image, dim, interpolation=cv2.INTER_AREA)

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

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

Output

 

How to Resize Image in Python

Upscale the image with Python cv2 resize()

In the following code, scale_percent value holds the percentage by which the image has to be scaled. Providing a value >100 upscales the image provided.

# Importing cv2
import cv2
# Path
path = 'cropped_image.jpg'

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

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

scale_percent = 120  # percent of original size
width = int(image.shape[1] * scale_percent / 100)
height = int(image.shape[0] * scale_percent / 100)
dim = (width, height)
# resize image
resized = cv2.resize(image, dim, interpolation=cv2.INTER_AREA)

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

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

Output

 

cv2 resize

That is it for the Python cv2 resize() method.

Leave A Reply

Your email address will not be published.

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