Necessary calculations for the scaling operations which fixes the scaling percentage.
scale_factor = 0.5
scaling_matrix = np.array([[scale_factor, 0, 0], [0, scale_factor, 0]], dtype='float32')
import cv2
import numpy as np
def rotate_and_scale_image(image,angle):
# Get the dimensions of the image
rows, cols = image.shape[:2] # first two elements of the tuple will be assigned to rows and columns
# Calculate the rotation matrix along the centre
rotation_matrix = cv2.getRotationMatrix2D((cols/2,rows/2), angle, 1)
# Rotate the image along centre
rotated_image = cv2.warpAffine(image, rotation_matrix, (cols, rows))
# display = cv2.imshow('Rotated_image',rotated_image)
# cv2.waitKey(0)
return rotated_image
def scale_image(image,scale):
rows, cols = image.shape[:2]
#Calculate the scaling matrix
scaling_matrix = np.array([[scale, 0, 0], [0, scale, 0]], dtype='float32')
# Scale the image
result = cv2.warpAffine(image, scaling_matrix, (int(cols * scale), int(rows * scale)))
return result
def crop_image(image,dimentions):
image_dims = image.shape
h_s , h_e ,w_s , w_e = dimentions
width_dims = [w_s , w_e]
height_dims = [h_s , h_e]
np.clip(height_dims, a_min = 0, a_max = image_dims[0])
np.clip(width_dims, a_min = 0, a_max = image_dims[1])
crop_image = image[height_dims[0]:height_dims[1],width_dims[0]:width_dims[1]]
return crop_image
image = cv2.imread('car.jpeg')
rotated = rotate_and_scale_image(image,int(140))
cv2.imwrite("Rotated Image.jpg", rotated )
image_1 = cv2.imread('Rotated Image.jpg')
scaled = scale_image(image_1,int(0.5))
cv2.imwrite("Scaled Image.jpg",scaled)