Image Resizing with Python
Use the package manager pip to install cv2 and numpy.
pip install cv2
pip install numpy
import cv2
import numpy as np
Here we are using 'cv2.imread()' method to read samples.
img = cv2.imread("cat.png")
Here we are using a bit better method to resize an image, preserving the aspect-ratio rather than using normal resizing.
1.Set the scale-percentage i.e percentage upto which, we want to downscale.
2.Build new width of the resized image i.e (Original image width * scale_percent).
3.Build new height of the resized image i.e (Original image height * scale_percent).
4.Storing height and width.
In the next snippet we are using interpolation to finally resizing it.
scale_percent = 0.60
width = int(img.shape[1]*scale_percent)
height = int(img.shape[0]*scale_percent)
dim = (width,height)
As we are demonstrating Down-scaling or shrinking the scale, we will be using cv2.INTER_AREA method of Interpolation
resized = cv2.resize(img,dim,interpolation = cv2.INTER_AREA)
cv2.imshow('ORIGINAL',img)
cv2.imshow('RESIZED',resized)
cv2.waitKey(0)
cv2.destroyAllWindows()