Table of Contents
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
1 2 3 |
cv2.resize(source, dim, fx, fy, interpolation) |
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.
-
source:
Source/Original/Input image i.e that image n-dimensional array which is wanted to be resized. -
dim:
Required dimension of an image in the form of tuple as (height, width). -
fx:
Scale factor along the horizontal axis or X- axis. -
fy:
Scale factor along the vertical axis or Y- axis. -
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.- INTER_LINEAR: A bilinear interpolation. This is by default method which is used by resize() method.
- INTER_CUBIC: A bicubic interpolation over 4×4 pixel neighborhood.
- 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# import computer vision library(cv2) in this code import cv2 # main code if __name__ == "__main__" : # mentioning absolute path of the image img_path = "C:\\Users\\user\\Desktop\\flower.jpg" # read/load an image image = cv2.imread(img_path) # find out the configuration of an image # shape attribute of an image gives shape # of an image in form of (width,height,planes) width,height,dimension = image.shape print("Original image configuration :",image.shape) # show the image cv2.imshow("window1",image) # half of the original image required_width, required_height = width // 2, height // 2 # resizing of an image is done resize_img = cv2.resize(image,(required_height, required_width)) print("Resize image configuration :",resize_img.shape) # show the image cv2.imshow("window2",resize_img) |
Output:
Resize image configuration : (125, 167, 3)
Example 2:
Resize to Half of the original image using fx,fy parameter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# import computer vision library(cv2) in this code import cv2 # main code if __name__ == "__main__" : # mentioning absolute path of the image img_path = "C:\\Users\\user\\Desktop\\flower.jpg" # read/load an image image = cv2.imread(img_path) # find out the configuration of an image # shape attribute of an image gives shape # of an image in form of (width,height,planes) width,height,dimension = image.shape print("Original image configuration :",image.shape) # show the image cv2.imshow("window1",image) # resizing of an image is done using scaling factor # fx and fy parameter is set to the 50% resize_img = cv2.resize(image,(0,0),fx=0.5,fy = 0.5) print("Resize image configuration :",resize_img.shape) # show the image cv2.imshow("window2",resize_img) |
Output :
Resize image configuration : (126, 168, 3)
That’s all about cv2 resize() Method in Python.