Challenge

Project 1

Solve the following problem statements.

1.Script a function to perform image rotation by deriving the rotation matrix for a 140 degree rotation angle.
2.Now on the rotated image perform a scaling operation which reduces the size of the image by 50% of the original size.
3.Take the original image and perform cropping operation manually which focuses on the subject of the input image.

Install OpenCV library and import the libraries required

!pip install opencv-python

import cv2 as computervision
import numpy as numericalpython

Import the image to the notebook

image=computervision.imread(r"image.png")
assert not isinstance(image,type(None)), 'image not found'

Rotation matrix for a 140 degree rotation angle

height,width=image.shape[:2]
center=(width/2,height/2)
rotated_matrix=computervision.getRotationMatrix2D(center=center,angle=140,scale=1)
rotated_image=computervision.warpAffine(src=image,M=rotated_matrix,dsize=(width,height))
computervision.imwrite(r"rotated_image.jpg",rotated_image)

Reduce 50% of the size of the rotated image

scale_percent = 50
resized_width = int(rotated_image.shape[1] * scale_percent / 100)
resized_height = int(rotated_image.shape[0] * scale_percent / 100)
dim = (resized_width, resized_height)
resized_image = computervision.resize(rotated_image, dim, interpolation = computervision.INTER_AREA)
computervision.imwrite(r"resized_image.jpg",resized_image)

Cropping the image to focus the subject

image.shape
rowstart=104
rowend=537
colstart=178
colend=854
if(rowstart<0 or rowstart>rowend or rowend>image.shape[0] or colstart<0 or colstart>colend or colend>image.shape[1]):
print("invalid cropping dimension")
cropped_image = image[rowstart:rowend, colstart:colend]
computervision.imwrite(r"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.