You are on page 1of 6

Name: Chaitanya Sawant

Roll no: 18
ET-D

CV lab2

Code:
import cv2
import numpy as np

def translateImage(path):
image = cv2.imread(path)
height = image.shape[0]
width = image.shape[1]
print(height, " width is : ", width)
M = np.float32([[1, 0, 100], [0, 1, 50]])
translated_image = cv2.warpAffine(image, M, (height, width))
cv2.imshow("original image", image)
cv2.imshow('Translated Image ', translated_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

# translateImage('.\\flower.jpg')

def rotateImage(path):
image = cv2.imread(path)
height = image.shape[0]
width = image.shape[1]
M = cv2.getRotationMatrix2D((height / 2, width / 2), 90, 1)
rotate = cv2.warpAffine(image, M, (height, width))
cv2.imshow("original image", image)
cv2.imshow('Rotated Image ', rotate)
cv2.waitKey(0)
cv2.destroyAllWindows()

# rotateImage('.\\flower.jpg')

def scaleImage(path):
image = cv2.imread(path)
height = image.shape[0]
width = image.shape[1]
scaledImage = cv2.resize(image, (2 * width, 2 * height),
interpolation=cv2.INTER_CUBIC)
cv2.imshow("original image", image)
cv2.imshow('Scaled Image ', scaledImage)
cv2.waitKey(0)
cv2.destroyAllWindows()

# scaleImage('.\\flower.jpg')

def cropImage(path):
image = cv2.imread(path)
height = image.shape[0]
width = image.shape[1]
# Cropping an image
cropped_image = image[80:280, 150:330]
cv2.imshow('Original Image ', image)
# Display cropped image
cv2.imshow("cropped Image", cropped_image)

cv2.waitKey(0)
cv2.destroyAllWindows()

# cropImage('.\\flower.jpg')

def shearImage(path):
image = cv2.imread(path)
height = image.shape[0]
width = image.shape[1]
M = np.float32([[1, 0, 100], [0, 1, 50],[0, 0, 1]])
translated_img = cv2.warpPerspective(image, M, (height, width))
cv2.imshow("original image",image)
cv2.imshow("Sheared Image ", translated_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

# shearImage('.\\flower.jpg')

def flipImage(path):
image = cv2.imread(path)
filpedImage = cv2.flip(image, 0)
cv2.imshow("original image", image)
# Displaying the image
cv2.imshow("original image", image)
cv2.imshow("Flipped Image ", filpedImage)
cv2.waitKey(0)

flipImage('.\\flower.jpg')
def switchCase(i):
if i == "1":
translateImage(input("enter image path"))
return
if i == "2":
rotateImage(input("enter image path"))
return
if i == "3":
scaleImage(input("enter image path"))
return
if i == "4":
cropImage(input("enter image path"))
return
if i == "5":
shearImage(input("enter image path"))
return
if i == "6":
flipImage(input("enter image path"))
return

print("Invalid choice pls select choice from 1 to 7 only.")

while(True):
print("to translate image press 1")
print("to rotate image press 2")
print("to scale image press 2")
print("to crop image press 2")
print("to flip image press 2")

print("to quit press q ")


i = input()
if i == 'q':
break
switchCase(i)

Output:
1. Translation Image
2. Rotation Image:

3. Scaling image:
4. Cropping Image:

5. Shearing Image:
6. Flipped Image:

You might also like