import cv2 # cv2 is the importing for opencv
import numpy as np
#Image rotation
#Defining image rotation function
def image_rotation(image,dimensions,angle,scale):
(height,width) = image.shape[:2]
rotation_matrix = cv2.getRotationMatrix2D((width/2,height/2),angle,scale)
rotated_image = cv2.warpAffine(image,rotation_matrix,(width,height))
return rotated_image
image = cv2.imread('Challenge1_1638957930.png') # Reading image
(height,width) = image.shape[:2] # Shape of image in terms of pixels.
angle = 140 # Rotation angle
scale = 1
rotated_image = image_rotation(image,(610,996),angle,scale) # Calling function
cv2.imwrite('rotated_image.png',rotated_image) # Saving rotated image
#Scaling of image
#Defining image resizing function
def resize_image(image,dimensions,method_number):
methods =
[cv2.INTER_AREA,cv2.INTER_LINEAR,cv2.INTER_NEAREST,cv2.INTER_CUBIC,cv2.INTER_LANCZOS4]
resize_method = methods[method_number]
print(resize_method)
resized_image = cv2.resize(image,dimensions,resize_method)
return resized_image
image = cv2.imread('rotated_image.png')
image.shape[:2]
# scaling dimensions 50% of original dimensions
h = image.shape[0]*50/100
w = image.shape[1]*50/100
dimensions = (h,w)
dimensions
method_number = 0 # for interpolation method
scaled_image = resize_image(image,(305,498),method_number) # calling function
cv2.imwrite('scaled_image.png',scaled_image) # saving scaled_image
#Cropping image
#defining cropping function
def crop_image(image,dimensions):
image_dims = image.shape
h_s,h_e,w_s,w_e = dimensions
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.shape
image = cv2.imread('Challenge1_1638957930.png') # Reading orignal image
crop_dimensions = [109,538,180,849] # crop dimensions
cropped_image = crop_image(image,crop_dimensions) # calling croping function
cv2.imwrite('Cropped_image.png',cropped_image) # saving cropped iamge