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
1 |
pip install opencv-python |
OK after installation we need to import required libraries, we need to import cv2 from OpenCV.
1 |
import cv2 |
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.
1 |
img = cv2.imread('image.jpg') |
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.
1 2 3 4 5 6 |
scale_percent = 60 # percent of original size width = int(img.shape[1] * scale_percent / 100) height = int(img.shape[0] * scale_percent / 100) dim = (width, height) resized = cv2.resize(img, dim, interpolation = cv2.INTER_AREA) |
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.
1 2 3 |
cv2.imshow("Resized image", resized) cv2.waitKey(0) cv2.destroyAllWindows() |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import cv2 # Load the image img = cv2.imread('lena.tif') # Set scaling percentage scale_percent = 60 # percent of original size # Calculate new width and height based on the scaling percentage width = int(img.shape[1] * scale_percent / 100) height = int(img.shape[0] * scale_percent / 100) dim = (width, height) # Resize image resized = cv2.resize(img, dim, interpolation = cv2.INTER_AREA) # Display resized image cv2.imshow("Resized image", resized) # Wait for key press and then close all windows cv2.waitKey(0) cv2.destroyAllWindows() |
Run the complete code and this will be the result
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