Challenge

Project 1

#Import libraries
import cv2
import numpy as np

%matplotlib auto

#Image read function
def read_image(image_path):
    image = cv2.imread(image_path)
    return image

#Image display function
def display_image(window_name, image):
    image = cv2.imshow(window_name, image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

#Image path from where image to be read
image_path = 'Challenge1.png'

#Read image
image = read_image(image_path)

#Display image
display_image('Read Image', image)

#Rotation matrix
def rotation_image(image, dimensions, angle, scale):
    height, width = dimensions[:2]
    rotation_matrix = cv2.getRotationMatrix2D((width/2, height/2), angle, scale)
    rotated_image = cv2.warpAffine(image, rotation_matrix, (width, height))
    return rotated_image

#Identifying dimensions of the image and passing information to function
dimensions = image.shape
angle = 140
scale = 1
rotated_image = rotation_image(image, dimensions, angle, scale)

#Display Image
display_image('Rotated Image', rotated_image)

#Write image to the disk
image = cv2.imwrite('rotated_image.jpg', rotated_image)

#Scaling Image to 50%
dimensions = image.shape
angle = 140
scale_percentage = 50
scale = scale_percentage/100
scaled_image = rotation_image(image, dimensions, angle, scale)

#Display Image
display_image('Scaled Image', scaled_image)

#Write image to disk
image = cv2.imwrite('scaled_image.jpg', scaled_image)


#Crop Image

#Read Original Image
image = read_image(image_path)

#Crop Image function
def crop_image(image, dimensions):
    height, width = image.shape[:2]
    h_s, h_e, w_s, w_e = dimensions
    height_dimensions = [h_s, h_e]
    width_dimensions = [w_s, w_e]
    print(np.clip(height_dimensions, a_min=0, a_max=height))
    print(np.clip(width_dimensions, a_min=0, a_max=width))
    crop_image = image[height_dimensions[0]:height_dimensions[1], width_dimensions[0]:width_dimensions[1]]
    return crop_image

#Dimensions for cropped image
dimensions = (100,550,170,860)
cropped_image = crop_image(image,dimensions)

#Display Image
display_image('Cropped Image', cropped_image)

#Write Image to Disk
cv2.imwrite('cropped_image.jpg', cropped_image)
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.