Key points:
- dim = (width, height) # first param is width, second is height, contrary to
height = image.shape[0], width = image.shape[1]
-
height = int(shape[0] * scale_percentage)
& width = int(shape[1] * scale_percentage)
Need to make sure width & height are int
, else 'TypeError: integer argument expected, got float'
.
import cv2
img = cv2.imread("C:\\Users\\zzheng\\Pictures\\tiger.png", cv2.IMREAD_UNCHANGED)
shape = img.shape
print("Original shape: ", shape)
scale_percentage = 60 / 100
height = int(shape[0] * scale_percentage)
# need to transfer float to int,
# or "TypeError: integer argument expected, got float"
width = int(shape[1] * scale_percentage)
dim = (width, height)
resized_img = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)
print("New shape: ", resized_img.shape)
cv2.imshow("Resized image", resized_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Original shape: (1025, 1921, 3)
# New shape: (615, 1152, 3)
网友评论