Python| cv2 resize() Method

In this tutorial, we will see how to resize an image in python programming language using open-cv which is exist as cv2 (computer vision) library in python.

You can use resize() method of cv2 library to resize an image. In order to use cv2 library, you need to import cv2 library using import statement.

Resizing an image means to change the configuration of an image like change the height of an image or change the width of an image or change both the height and width of an image.

Now let’s see the syntax and return value of resize() method, then we will move on the examples.

Syntax

Parameters

You can pass 5 parameters to resize() method. Among the 5 parameters, the first two(source and dim) are mandatory; rest(fx, fy and interpolation) are optional.

  1. source: Source/Original/Input image i.e that image n-dimensional array which is wanted to be resized.
  2. dim: Required dimension of an image in the form of tuple as (height, width).
  3. fx: Scale factor along the horizontal axis or X- axis.
  4. fy: Scale factor along the vertical axis or Y- axis.
  5. interpolation: This is the process of estimating unknown values that fall between known values. Some methods of interpolation we will mention here which can be used to find-out the pixel value based on its neighboring pixels.

    1. INTER_LINEAR: A bilinear interpolation. This is by default method which is used by resize() method.
    2. INTER_CUBIC: A bicubic interpolation over 4×4 pixel neighborhood.
    3. INTER_LANCZOS4: A Lanczos interpolation over 8×8 pixel neighborhood.

Return Value

This method returns resized image pixel matrix.

cv2 resize method example

Now, Let’s see the python code :
Example 1: Resize to Half of the original image using dim parameter.

Output:

 

Original image configuration : (251, 335, 3)
Resize image configuration : (125, 167, 3)

Example 2: Resize to Half of the original image using fx,fy parameter.

Output :

Original image configuration : (251, 335, 3)
Resize image configuration : (126, 168, 3)

That’s all about cv2 resize() Method in Python.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *