Scale an image with OpenCV
Python OpenCV - Scale Image
To scale an image with OpenCV in Python, you can use cv2.resize() function.
In this tutorial, you will learn how to scale an image to a specific percentage, with examples.
Syntax of resize()
The syntax of resize() function is
cv2.resize(src, dsize
For complete details on resize() function, please refer Python OpenCV - cv2.resize().
Steps to scale an image
- Read an image into an array using cv2.imread() function.
- Define a scaling percent.
- Scale the image (2D array) using cv2.resize() function.
- Save the scaled image using cv2.imwrite() function.
Examples
In the following examples, we take the below image, and scale it to different scale percentages. The original width and height of the image are 640 and 427 respectively.
test_image_house.jpg
1. Scale image to 50 percent
In the following program, we scale the image test_image_house.jpg to 50 percent. This means the resulting image shall have a width of 50% of the original image width, and same with the height. We will save the scaled image to scaled_img.jpg.
Since we are scaling down the image from 100 percent to 50 percent, this is called Image Down-scaling.
Python Program
import cv2
# Load the image
img = cv2.imread('test_image_house.jpg')
# Define the scaling factor
scale_percent = 50 # 50% scaling
# Calculate the new image dimensions
new_width = int(img.shape[1] * scale_percent / 100)
new_height = int(img.shape[0] * scale_percent / 100)
dim = (new_width, new_height)
# Resize the image
scaled_img = cv2.resize(img, dim)
# Save cropped image
cv2.imwrite('scaled_img.jpg', scaled_img)
scaled_img.jpg
When you downscale an image, the resolution of the image is reduced. This results in a smaller image with fewer pixels. This can lead to a loss of detail and sharpness in the image, as well as a decrease in the amount of information contained in the image.
Downscaling an image can be useful in certain cases, such as when you need to reduce the file size of an image for web or mobile applications. It can also be used to speed up certain image processing algorithms that may be computationally expensive on large images.
2. Scale image to 200 percent
In the following program, we scale the image to 200% of the original image size.
Since we are scaling up the image from 100 percent to 200 percent, this is called Image Up-scaling.
Python Program
import cv2
# Load the image
img = cv2.imread('test_image_house.jpg')
# Define the scaling factor
scale_percent = 200
# Calculate the new image dimensions
new_width = int(img.shape[1] * scale_percent / 100)
new_height = int(img.shape[0] * scale_percent / 100)
dim = (new_width, new_height)
# Resize the image
scaled_img = cv2.resize(img, dim)
# Save cropped image
cv2.imwrite('scaled_img.jpg', scaled_img)
scaled_img.jpg
The size of this scaled image is 1280 pixels wide and 854 pixels height.
When you upscale an image, the resolution of the image is increased. This results in a larger image with more pixels. This can lead to a blurred image, because of the interpolation of the extra pixels.
Summary
In this Python OpenCV Tutorial, we scaled an image lower or higher using cv2.resize() function, with the help of examples.