Python rotate image – Rotate an Image by an Angle in Python

Python rotate image: Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.

In this tutorial, we’ll look at how to rotate a picture by an angle in Python.

When we say that a picture has been rotated, we indicate that the image has been rotated to a specific degree by its centre.

Rotate an Image by an Angle in Python

Below are the ways to Rotate an Image by an Angle in Python.

Method #1: Using Python Image Library(PIL)

PIL (Python Image Library) is a module that includes built-in functions for manipulating and working with images as input to the functions.

PIL has a built-in image.rotate(angle) method to rotate an image by an angle.



To load an image or give an image to the rotate() method, use the following code :

Image. open ( r” Path of the image ” )

To display the image, we’ll need to use the code below:

Below is the Implementation:

#importing image from PIL
from PIL import Image 
#open the image by providing the image Path
imge ="D:\repeat\matrix.jpg") 
#Rotating Image by 145 degrees
rotate_imge= imge.rotate(145)
#Displaying the image

Before Rotation:

After Rotation:

Method #2: Using OpenCV


OpenCV is a large open-source library for computer vision, machine learning, and image processing, and it currently plays a significant part in real-time operation, which is critical in today’s systems. It can process photos and movies to recognize items, faces, and even human handwriting. Python can process the OpenCV array structure for analysis when combined with other libraries such as NumPy. We employ vector space and execute mathematical operations on these features to identify visual patterns and their various features.

It has a variety of built-in capabilities for dealing with user-supplied photos.

To alter and work with images, OpenCV works brilliantly with another image processing library called imutils.

In Python, the imutils.rotate() function is used to rotate an image by an angle.

imutils.rotate(image, angle=angle)

Syntax  For Reading Image using OpenCV

cv2.imread(r”Path of the image”)

Syntax  to display the image using OpenCV

cv2.imshow(“output message”,imagevariable)

Below is the implementation:

#importing opencv and imutils 
import cv2
import imutils

#open the image by providing the image Path
imge = cv2.imread(r"D:\repeat\BTechGeeks (100).png")
 #Rotating Image by 145 degrees
rotate_imge = imutils.rotate(imge, angle=145)
#Displaying the image
cv2.imshow("Rotated Image", rotate_imge)

Before Rotation:

After Rotation:

Related Programs: