Rotate an Image using Python

In this tutorial, I am going to rotate an image in Python using OpenCV which is a Python library used for real-time computer vision, and imutils which is also a Python library that can be used for translation, rotation, resizing, skeletonization, etc.

Requirements


    • OpenCV
    • imutils

To get started make sure you have OpenCV and imutils installed in your system. If you don’t have these you have to install them using PIP which is a package manager for Python that can install Python modules or packages.

Using these following commands you can install OpenCV and imutils in your system

pip install opencv-python
pip install imutils

Code

Here in this code I am going to read the image using the cv2.imread() function and Rotate the image using imutils.rotate() function, which will take two parameters the Image and the angle that you want to rotate the image.

#importing modules
import cv2
import imutils

#Reading the Image
img = cv2.imread("image.jpg")

#Rotateing into differnt angles
ro_img = imutils.rotate(img,angle=180)
ro_img2 = imutils.rotate(img,angle=45)
ro_img3 = imutils.rotate(img,angle=90)

#Preview of Original Image
cv2.imshow("original image",img)

#Preview of Rotated Images
cv2.imshow("180 degree",ro_img)
cv2.imshow("45 degree",ro_img2)
cv2.imshow("90 degree",ro_img3)

cv2.waitKey(0)

Output

Original Image:

180° Rotated Image:

45° Rotated Image:

90° Rotated Image:

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top