import cv2 # cv2 is the importing for Opencv
# Opencv is a library of Python bindings designed to solve computer vision problems
import numpy as np # np is an alias for numpy
# general-purpose array-processing package.
# It provides a high-performance multidimensional array object and tools for working with these arrays
# Method to read an image
def read_image(image_path):
image = cv2.imread(image_path)
# cv2.imread(PATH) : method loads an image from the specified file.
# Parameters:
# 1) PATH - A string representing the path of the image to be read.
return image
# Method to display an image
def display_image(window_name, read_image):
cv2.imshow(window_name, read_image)
# cv2.imshow(window_name, image) : method is used to display an image in a window.
# Parameters:
# 1) window_name - A string representing the name of the window in which the image is to be displayed;
# 2) image - It is the image that is to be displayed.
cv2.waitKey(0)
# Method to perform Image Rotation
def rotate_image(image, dimensions, angle):
rotation_matrix = cv2.getRotationMatrix2D(center = dimensions, angle = angle, scale = 1)
# cv2.getRotationMatrix2D(center, angle, scale) : function is used to make the transformation matrix M which will be used for rotating a image
# Parameters:
# 1) center - Center of rotation
# 2) angle - Angle of Rotation. Angle is positive for anti-clockwise and negative for clockwise.
# 3) scale - scaling factor which scales the image
rotated_image = cv2.warpAffine(image, rotation_matrix, (image.shape[1],image.shape[0]))
return rotated_image
# Method to perform Image Scaling
def scaling_image(image, dimensions, scale):
scaled_matrix = cv2.getRotationMatrix2D(center = dimensions, angle = 0, scale = scale)
scaled_image = cv2.warpAffine(image, scaled_matrix, (image.shape[1],image.shape[0]))
# dst = cv2.warpAffine(src, M, dsize)
# Parameters:
# 1) src - input image.
# 2) M - transformation matrix.
# 3) dsize - size of the output image.
# 4) dst - output image that has the size dsize and the same type as src.
return scaled_image
# Method to perform Image cropping
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])
# numpy.clip(a,a_min,a_max) : numpy method which clip(limit) the values in an array.
# Parameters:
# 1) a - Array containing elements to clip.
# 2) a_min - Minimum value
# 3) a_max - Maximum value
crop_image = image[height_dims[0]:height_dims[1], width_dims[0]:width_dims[1]]
return crop_image
# To save image
def save_image(image_name, image):
cv2.imwrite(image_name, image)
# cv2.imwrite(filename, image) : method is used to save an image to any storage device.
# Parameters:
# 1) filename - A string representing the file name. The filename must include image format like .jpg, .png, etc.
# 2) image - It is the image that is to be saved.
# ************************************************************************************ #
# Main function
# Flow of the code structure
# 1. Before doing "Image Manipulation" we should read the input image from the specified path by using the "read_image()" method.
# 2. Then the image can be displayed by using the "display_image()" method.
# 3. "rotate_image()" method is to perform image rotation by deriving the rotation matrix[cv2.getRotationMatrix2D()] for a 140-degree rotation angle.
# 4. "scaling_image()" method is to perform a scaling operation that reduces the size of the image by 50% of the original size on the rotated image
# 5. "crop_image()" method is to perform the cropping operation manually which focuses on the subject of the input image on the original image
# NOTE: PFA of the resultant image(in jpg) after transformation operations(Rotation, scaling, Cropping)
# and jupyter notebook file "Project1.ipynb"
# Reading an image
image = read_image('Challenge1.png')
# To display original image
display_image('Original Image', image)
save_image('C1_Original_image.jpg', image)
# To rotate an image by an angle of 140 degree
image_dims = image.shape
rotate_dim = (image_dims[1]/2, image_dims[0]/2)
rotate_angle = 140
rotated_image = rotate_image(image, rotate_dim, rotate_angle)
display_image('Rotated Image', rotated_image)
save_image('C1_Rotated_image.jpg', rotated_image)
# Performing scaling operation on the rotated image which reduces the size of the image by 50% of the original image
image_dims = image.shape
scale_dim = (image_dims[1]/2, image_dims[0]/2)
scale_value = 0.5
scaled_image = scaling_image(rotated_image, scale_dim, scale_value)
display_image('Scaled Image', scaled_image)
save_image('C1_Scaled_image.jpg', scaled_image)
# Performing cropping operation on the original image which focuses on the subject of the input image
crop_dim = [105, 538, 180, 850]
cropped_image = crop_image(image, crop_dim)
display_image('Cropped Image', cropped_image)
save_image('C1_Cropped_image.jpg', cropped_image)