import cv2
import numpy as np
# Read image
def read_image(image_path):
image=cv2.imread(image_path)
return image
# Display Image
def dis_image(window_name,read_image):
cv2.imshow(window_name,read_image)
cv2.waitKey(0)
# Save image
def save_img(image_name,image):
cv2.imwrite(image_name,image)
# Rotation
def rotate(image,angle):
center = (image.shape[1]//2,image.shape[0]//2)
rotation_matrix = cv2.getRotationMatrix2D(center,angle,1)# creating rotation matrix keeping scale factor as 1
rotated_image = cv2.warpAffine(image,rotation_matrix,(image.shape[1],image.shape[0]))
return rotated_image
# Scale
def scaling_image(image,percentage,method_number):
methods = [cv2.INTER_AREA,cv2.INTER_LINEAR,cv2.INTER_NEAREST,cv2.INTER_CUBIC,cv2.INTER_LANCZOS4]
resize_method = methods[method_number]
hsize=int((1-(percentage/100))*(image.shape[0]))# size decreased by 50 %
vsize=int((1-(percentage/100))*(image.shape[1]))# size decreased by 50 %
dimensions=(hsize,vsize)
resized_image = cv2.resize(image,dimensions,interpolation=resize_method)
return resized_image
# Crop
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
#Part-1 and 2
angle=140
rotated_image = rotate(image,angle)# rotating image by angle=140
percentage = 50 # scaling percentage
scaleimg = scaling_image(rotated_image,percentage,0)# sclaing image with cv2.INTER_AREA method
dis_image("rotated image",scaleimg)
save_img("rotated_image.jpg",rotated_image)# Deliverable-1 save rotated image in drive with name rotated_image.jpg
save_img("raotatedandScaled_img.jpg",scaleimg)# Deliverable-2 save rotated and scaled image in drive with name rotatedandScaled_img.jpg
# Part-3
dimensions= (100,800,100,900)
c_img= crop_image(image,dimensions)
dis_image("crop image",c_img)
save_img("crop_img.jpg",c_img)# Deliverable-3 save crop image in drive with name crop_img.jpg
# Part-4
## Necessary calculations for the scaling operations which fix the scaling percentage
### hsize=int((1-(percentage/100))*(image.shape[0]))
### vsize=int((1-(percentage/100))*(image.shape[1]))