How to Scale Image with Python and OpenCV

In this Python OpenCV article we are going to learn How to Scale Image with Python and OpenCV, Scaling an image is common task in computer vision or OpenCV and image processing. you can use scaling image for resizing an image to smaller or larger size, which can be useful for different applications such as object detection, image recognition and many more. in this article we want to learn how to scale an image using OpenCV and Python.

 

 

First of all we need to install Python OpenCV

 

 

OK after installation we need to import required libraries, we need to import cv2 from OpenCV.

 

 

After that we need to load our image that we want to scale. we can use cv2.imread() function to read an image from disk. this function takes file path as the input and returns NumPy array that represents the image.

 

 

 

After we have loaded the image, now we can resize our image using cv2.resize() function, this function takes input image, desired output size, and interpolation method as the input arguments.

In the above code we have define scaling percentage, which is set to 60%. after that we calculates new width and height of the image based on the scaling percentage, and finally we use cv2.resize() function to resize the input image to the desired output size.

 

 

 

And the last step is to display resized image using cv2.imshow() function. we pass title of the window and image array as the input arguments.

In the above code we have used cv2.imshow() function to display resized image in new window. after that we have used wait for key press using cv2.waitKey() function. and finally we have used cv2.destroyAllWindows() function to close all the windows.

 

 

This is the complete code for this article 

 

 

 

Run the complete code and this will be the result

How to Scale Image with Python and OpenCV
How to Scale Image with Python and OpenCV

 

 

In this article we have learned how to scale an image using OpenCV and Python. we learned how to load an image, resize it using the cv2.resize() function and display the resized image using cv2.imshow() function. scaling an image is useful technique that can be applied to different computer vision and image processing applications.

 

 

Learn More on OpenCV

 

Leave a Comment