You can rotate an image by an angle using OpenCV Python by following the given steps. I highly recommend you get the “Computer Vision: Models, Learning, and Inference Book” to learn Computer Vision.

Step 1
Import the required libraries. If OpenCV is not installed in your system then first install it using This Method.
import cv2 #cv2 is used for OpenCV library import numpy as np
Step 2
Now Read the Image. The image should be in the current working directory. Otherwise, mention the location of the image.
img =cv2.imread('cat.jpg')
Step 3
First, calculate the center of the image then rotate the image 30 degree anticlockwise from center.
img_center = (np.array(img.shape[1::-1]) / 2) matrix = cv2.getRotationMatrix2D(img_center, 30 , 1.0)
Step 4
Now apply the above matrix to the image and use the method cv2.warpAffine.
rotated_img = cv2.warpAffine(img, matrix, img.shape[1::-1], flags=cv2.INTER_LINEAR)
Step 5
Now display both images.
cv2.imshow('Cat',img) cv2.imshow('Rotated Image',rotated_img) cv2.waitKey(0) cv2.destroyAllWindows()

