Challenge

Project 1

1) Image is read as a color image using cv2.imread function

2) To rotate the image first we get the Rotation matrix using cv2.getRotationMatrix2D by passing the center of image, angle of rotation and scaling factor

Angle of Rotation is given as 140, hence it will be rotated 140 degress in anti-clockwise direction

To rotate the image in Clockwise direction, angle should be passed as -140

3) Rotated image can now be obtained using cv2.warpAffine function by passing the original image, rotation matrix and the size

4) Image resizing to the desired width and height can be obtained by cv2.resize function

5) To reduce the size by 50%, we reduce the width and height by 50%

6) The resized image can now be cropped by using slicing operation

7) Height and Width dimension to be cropped are used in the slicing operation

Scaling Operation Calculation

Height and width of the rotated image is reduced to 50% by using the below calculation. The result is converted to integer (As integer is required for cv2.resize function)

width = int((rotated_image.shape[1])*(50/100))

height = int((rotated_image.shape[0])*(50/100))

Note: The resizing of the image can be directly done to 50% along with Rotation by passing scaling factor as 0.5 in cv2.getRotationMatrix2D

#!/usr/bin/env python
# coding: utf-8

# In[1]:


import cv2


# In[2]:


import numpy as np


# In[3]:


# Read a image
# [flag = (1:Color image), (0: Greyscale image), (-1: Unchanged imaged :: Useful for transparency in png)]
def read_image(image_path,flag):
    image=cv2.imread(image_path,flag)
    return image


# In[4]:


# Display a image
def dis_image(window_name,read_image):
    cv2.imshow(window_name,read_image)
    cv2.waitKey(0)


# In[5]:


# Save a image
def save_image(image_name,image):
    cv2.imwrite(image_name,image)


# In[6]:


image = read_image('Challenge.png',1)   #To read the image as color image


# In[7]:


def image_rotation(image,angle,scale): 
    h = image.shape[0]         #Height of image
    w = image.shape[1]         #Width of image
    center = (w/2,h/2)
    rotation_matrix = cv2.getRotationMatrix2D(center,angle,scale)   # To get the rotation matrix
    rotated_image=cv2.warpAffine(image,rotation_matrix,(w,h))       # To obtain the rotated image
    return rotated_image      


# In[8]:


rotated_image=image_rotation(image,140,1)  #Rotating the image to an angle of 140 degree anti clockwise with scaling factor as 1


# In[9]:


dis_image('Rotated image',rotated_image)
save_image('Rotated_image.png',rotated_image)


# In[10]:


# Resize an image
def resize_image(image,dimensions,method_number):
    methods=[cv2.INTER_AREA,cv2.INTER_LINEAR,cv2.INTER_CUBIC]
    resize_method = methods[method_number]
    resized_image = cv2.resize(image,dimensions,interpolation = resize_method)
    return resized_image


# In[11]:


width = int((rotated_image.shape[1])*(50/100))     #Reducing the width and height to 50%
height = int((rotated_image.shape[0])*(50/100))
resized_image = resize_image(rotated_image,(width,height),0)  #INTER_AREA interpolation is used as it is best for shrinking images


# In[12]:


dis_image('Scaled image',resized_image)
save_image('Scaled image.png',resized_image)


# In[13]:


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])   # To clip the height_dims values if it is less the 0 or greater the height of image
    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


# In[14]:


crop_dimension = (15,250,90,410)           #Height and Width dimension to focus on subject of image
cropped_image = crop_image(resized_image,crop_dimension)


# In[15]:


dis_image('Cropped image',cropped_image)
save_image('Cropped image.png',cropped_image)


# In[ ]:




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.

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.