OpenCV cv2 Resize Image - 3 Python Examples


OpenCV cv2.resize()

To resize an image in Python, you can use cv2.resize() function of OpenCV library cv2.

Resizing, by default, does only change the width and height of the image. The aspect ratio can be preserved or not, based on the requirement. Aspect Ratio can be preserved by calculating width or height for given target height or width respectively.

https://youtu.be/TSnSFCBGpQA?si=Qs8e5320MayJS7Ow

In this tutorial, we shall learn how to resize image in Python using OpenCV library.

Syntax of cv2 resize() function

Following is the syntax of cv2.resize() function.

cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]])

where

  • src is the source, original or input image in the form of numpy array
  • dsize is the desired size of the output image, given as tuple
  • fx is the scaling factor along X-axis or Horizontal axis
  • fy is the scaling factor along Y-axis or Vertical axis
  • interpolation could be one of the following values.
    • INTER_NEAREST
    • INTER_LINEAR
    • INTER_AREA
    • INTER_CUBIC
    • INTER_LANCZOS4

Based on the interpolation technique selected, respective algorithm is used. You can think interpolation as a method that decides which pixel gets which value based on its neighboring pixels and the scale at which the image is being resized.

How could you resize an image?

You can resize an image in three ways.

  1. Preserve the Aspect Ration and increase or decrease the width and height of the image. Just to make things clear, Aspect Ratio is the ratio of image width to image height.
  2. Scale the image only along X-axis or Horizontal axis. Meaning, change width, keeping height same as that of original image.
  3. Scale the image only along Y-axis or Vertical axis. Meaning, change height, keeping width same as that of original image.

Source Image

Consider the following image. We will use this image as input or source image in our ongoing example programs.

Python OpenCV cv2 resize image - Original Image
Input Image

Examples

1. Resize Image using cv2.resize()

In the following example, we are going to see how we can resize the above image using cv2.resize() while preserving the aspect ratio. We will resize the image to 50% of its actual shape, i.e., we will reduce its height to 50% of its original and width to 50% of its original.

Python Program

import cv2
 
src = cv2.imread('D:/cv2-resize-image-original.png', cv2.IMREAD_UNCHANGED)

#percent by which the image is resized
scale_percent = 50

#calculate the 50 percent of original dimensions
width = int(src.shape[1] * scale_percent / 100)
height = int(src.shape[0] * scale_percent / 100)

# dsize
dsize = (width, height)

# resize image
output = cv2.resize(src, dsize)

cv2.imwrite('D:/cv2-resize-image-50.png',output) 

What have we done in the above Python program?

  1. cv2.imread() reads the given file in cv2.IMREAD_UNCHANGED with transparency channel (if any) and returns a numpy array with pixel values.
  2. scale_percent is set to 50. We are going to scale the image to 50% of its original dimensions, both width and height.
  3. src.shape[1] gives the width of the source image. int(src.shape[1] * scale_percent / 100) calculates 50% of the original width. Similarly height is also calculated.
  4. Then we are setting the desired size dsize with the newly computed width and height.
  5. cv2.resize resizes the image src to the size dsize and returns numpy array.
  6. Using cv2.imwrite, we are writing the output of cv2.resize to a local image file.

Output Image

Python OpenCV cv2 resize image to 50% of Original size
cv2.resize() preserving aspect ratio

2. cv2 Resize image only horizontally

In the following example, we will scale the image only along x-axis or Horizontal axis. And we keep the height of the image unchanged.

In the dsize, we will keep the height same as that of original image but change the width.

Python Program

import cv2
 
src = cv2.imread('D:/cv2-resize-image-original.png', cv2.IMREAD_UNCHANGED)

# set a new width in pixels
new_width = 300

# dsize
dsize = (new_width, src.shape[0])

# resize image
output = cv2.resize(src, dsize, interpolation = cv2.INTER_AREA)

cv2.imwrite('D:/cv2-resize-image-width.png',output) 

Output Image

Python OpenCV cv2 resize image along width or horizontal axis
cv2.resize() along width or horizontal axis

3. cv2 - Resize image only vertically

In the following example, we will scale the image only along y-axis or Vertical axis. Width of the output image remains unchanged from that of the source image.

In the dsize, we will keep the width same as that of original image but change the height.

Python Program

import cv2
 
src = cv2.imread('D:/cv2-resize-image-original.png', cv2.IMREAD_UNCHANGED)

# set a new height in pixels
new_height = 200

# dsize
dsize = (src.shape[1], new_height)

# resize image
output = cv2.resize(src, dsize, interpolation = cv2.INTER_AREA)

cv2.imwrite('D:/cv2-resize-image-height.png',output) 

Output Image

Python OpenCV cv2 resize image along height or Vertical axis
cv2.resize() along height or vertical axis

Summary

In this Python OpenCV Tutorial, we have learned how to use OpenCV cv2.resize() function, to resize an image along width, height or both by preserving the aspect ratio or not preserving the aspect ratio.