To resize an image in Python, you can use cv2.resize() function of OpenCV library cv2.
Resizing, by default, does only change the width and height of the image. The aspect ratio can be preserved or not, based on the requirement. Aspect Ratio can be preserved by calculating width or height for given target height or width respectively.
In this tutorial, we shall learn how to resize image in Python using OpenCV library.
Syntax of cv2 resize() function
Following is the syntax of cv2.resize() function.
cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]])
srcis the source, original or input image in the form of numpy array
dsizeis the desired size of the output image, given as tuple
fxis the scaling factor along X-axis or Horizontal axis
fyis the scaling factor along Y-axis or Vertical axis
interpolationcould be one of the following values.
Based on the interpolation technique selected, respective algorithm is used. You can think interpolation as a method that decides which pixel gets which value based on its neighboring pixels and the scale at which the image is being resized.
How could you resize an image?
You can resize an image in three ways.
- Preserve the Aspect Ration and increase or decrease the width and height of the image. Just to make things clear, Aspect Ratio is the ratio of image width to image height.
- Scale the image only along X-axis or Horizontal axis. Meaning, change width, keeping height same as that of original image.
- Scale the image only along Y-axis or Vertical axis. Meaning, change height, keeping width same as that of original image.
Consider the following image. We will use this image as input or source image in our ongoing example programs.
Example 1: Resize Image – cv2.resize()
In the following example, we are going to see how we can resize the above image using
cv2.resize() while preserving the aspect ratio. We will resize the image to 50% of its actual shape, i.e., we will reduce its height to 50% of its original and width to 50% of its original.
import cv2 src = cv2.imread('D:/cv2-resize-image-original.png', cv2.IMREAD_UNCHANGED) #percent by which the image is resized scale_percent = 50 #calculate the 50 percent of original dimensions width = int(src.shape * scale_percent / 100) height = int(src.shape * scale_percent / 100) # dsize dsize = (width, height) # resize image output = cv2.resize(src, dsize) cv2.imwrite('D:/cv2-resize-image-50.png',output)
What have we done in the above Python program?
cv2.imread()reads the given file in
cv2.IMREAD_UNCHANGEDwith transparency channel (if any) and returns a numpy array with pixel values.
scale_percentis set to 50. We are going to scale the image to 50% of its original dimensions, both width and height.
- src.shape gives the width of the source image.
int(src.shape * scale_percent / 100)calculates 50% of the original width. Similarly height is also calculated.
- Then we are setting the desired size
dsizewith the newly computed
cv2.resizeresizes the image
srcto the size
dsizeand returns numpy array.
cv2.imwrite, we are writing the output of
cv2.resizeto a local image file.
Example 2: cv2 Resize Image Horizontally
In the following example, we will scale the image only along x-axis or Horizontal axis. And we keep the height of the image unchanged.
In the dsize, we will keep the height same as that of original image but change the width.
import cv2 src = cv2.imread('D:/cv2-resize-image-original.png', cv2.IMREAD_UNCHANGED) # set a new width in pixels new_width = 300 # dsize dsize = (new_width, src.shape) # resize image output = cv2.resize(src, dsize, interpolation = cv2.INTER_AREA) cv2.imwrite('D:/cv2-resize-image-width.png',output)
cv2 Resize Image Vertically
In the following example, we will scale the image only along y-axis or Vertical axis. Width of the output image remains unchanged from that of the source image.
In the dsize, we will keep the width same as that of original image but change the height.
import cv2 src = cv2.imread('D:/cv2-resize-image-original.png', cv2.IMREAD_UNCHANGED) # set a new height in pixels new_height = 200 # dsize dsize = (src.shape, new_height) # resize image output = cv2.resize(src, dsize, interpolation = cv2.INTER_AREA) cv2.imwrite('D:/cv2-resize-image-height.png',output)
In this tutorial of Python Examples, we have learned how to use OpenCV cv2.resize() function, to resize an image along width, height or both by preserving the aspect ratio or not preserving the aspect ratio.
- Python OpenCV – Image Filtering using Convolution
- Python OpenCV cv2 Find Contours in Image
- Python OpenCV – Write Text on Image – putText()
- Python OpenCV Read Image – cv2 imread()
- Python OpenCV – cv2 Image Blur
- Python OpenCV cv2.imwrite() – Save Image
- Python Program to Add or Blend Two Images using OpenCV
- Python Convert Image to Black and White (Binary)