Rate This Document
Findability
Accuracy
Completeness
Readability

resize

Usage

Scales up or down an image. You can adjust the image width and height by specifying the target size or scaling factor.

Multiple interpolation modes, such as cv2.INTER_LINEAR, cv2.INTER_CUBIC, and cv2.INTER_NEAREST_EXACT, are available to maintain the image quality during scaling.

Interface

1
cv2.resize(src, dsize, fx=0, fy=0, interpolation=cv2.INTER_LINEAR)

Parameters

Parameter

Description

Value Range

Input/Output

src

Input image.

Not null

Input

dsize

Size (width and height) of the target image. If this parameter is specified, the values of fx and fy are invalid. If this parameter is set to (0, 0) or not specified, the values of fx and fy are used.

Tuple (x, y), where x and y are positive integers

Input

fx

Horizontal scaling factor. The default value is 0.

(0, inf)

Input

fy

Vertical scaling factor. The default value is 0.

(0, inf)

Input

interpolation

Interpolation mode. The value can be cv2.INTER_LINEAR (default), cv2.INTER_NEAREST_EXACT, or cv2.INTER_CUBIC.

cv2.INTER_LINEAR (default), cv2.INTER_NEAREST_EXACT, cv2.INTER_CUBIC

Input

Return Value

  • Output image

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import numpy as np
import cv2

# Create a 5x5 image.
src = np.array([[0, 0, 0, 0, 0],
               [0, 1, 1, 1, 0],
               [0, 1, 0, 1, 0],
               [0, 1, 1, 1, 0],
               [0, 0, 0, 0, 0]], dtype=np.uint8)  

dst = cv2.resize(src, dsize=(10, 10), interpolation=cv2.INTER_LINEAR)  

print(dst)

Output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
[[0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 1 1 1 1 1 1 0 0]
 [0 0 1 1 1 1 1 1 0 0]
 [0 0 1 1 0 0 1 1 0 0]
 [0 0 1 1 0 0 1 1 0 0]
 [0 0 1 1 1 1 1 1 0 0]
 [0 0 1 1 1 1 1 1 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]]