In this Python OpenCV lesson we are going to learn How to Scale Image with Python OpenCV, Scaling is the resizing of the image, and in opencv you can use cv2.resize() for scaling or resizing the image. OpenCV provides a function called resize to achieve image scaling. If you don’t specify a size (by using None), then it expects the X and Y scaling factors. in our example, the image will be enlarged by a factor of 1.2, also the size of the image can be specified manually, or you can specify the scaling factor. different interpolation methods are used. preferable interpolation methods are cv2.INTER_AREA for shrinking and cv2.INTER_CUBIC (slow) & cv2.INTER_LINEAR for zooming. By default, the interpolation method cv2.INTER_LINEAR is used for all resizing purposes.
This is the complete code for this lesson.
img = cv2.imread('lena.tif')
img_scaled = cv2.resize(img,None,fx=1.2, fy=1.2,
interpolation = cv2.INTER_LINEAR)
cv2.imshow('Scaling Linear Interpolation', img_scaled)
interpolation = cv2.INTER_CUBIC)
cv2.imshow('Scaling Cubic Interpolation', img_scaled)
img_scaled = cv2.resize(img,(450, 400),
interpolation = cv2.INTER_AREA)
cv2.imshow('Scaling Skewed Size', img_scaled)
You can see that we have used cv2.resize() method for image resizing, you need to give some parameters like image source, dsize, factor x and factor y also interpolation.
If you run the code you will see that we have 3 different sizes of the image.