For scaling to 50% of the original image
First we take the height, width and multiply it by scaling factor.
height = height*0.5
width = width*0.5
Then we take the scaled image and resize it to half of the image.
#Importing Libraries
import cv2
import numpy as np
#Reading the image
image = cv2.imread("Ch1.png")
#Image rotation func
def image_rotation(image,angle):
height,width = image.shape[:2]
center = (width/2,height/2)
rot_matrix = cv2.getRotationMatrix2D(center,angle,scale=1) #rotation matrix for 140
#degree rotation angle
rotated_image = cv2.warpAffine(image,rot_matrix,(width,height))
return rotated_image
#Func for resizing it to half of the image
def scaling(image,scale):
resized_height = int(image.shape[0] * scale / 100)
resized_width = int(image.shape[1] * scale / 100)
dimensions = (resized_width, resized_height)
resized_image = cv2.resize(image, dimensions, interpolation = cv2.INTER_AREA)
return resized_image
#ROtation Angle = 140
angle=140
rotated_image = image_rotation(image,angle)
cv2.imwrite("rotated_img.jpg",rotated_image)
#50% scale percent
scale=50
resized_img = scaling(rotated_image,scale)
cv2.imwrite("resized_image.jpg",resized_img)
#Func for cropping the image
def crop_image(image, dimensions):
image_dims = image.shape
h_s,h_e,w_s,w_e = dimensions
height_dims = [h_s,h_e]
width_dims = [w_s,w_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_img = image[height_dims[0]:height_dims[1],width_dims[0]:width_dims[1]]
return crop_img
dimensions = [100,540,165,852]
crop = crop_image(image,dimensions)
#Saving the result
cv2.imwrite("cropped_image.jpg",crop)