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.
Syntax:
image.rotate(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:
image.show()
Below is the Implementation:
#importing image from PIL from PIL import Image #open the image by providing the image Path imge = Image.open(r"D:\repeat\matrix.jpg") #Rotating Image by 145 degrees rotate_imge= imge.rotate(145) #Displaying the image rotate_imge.show()
Before Rotation:
After Rotation:
- Python Program for Negative Transformation of an Image Using PIL
- Python ORB Feature Detection
- Python Program to Print Right Angled Triangle Star Pattern
Method #2: Using OpenCV
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) cv2.waitKey(0)
Before Rotation:
After Rotation:
Related Programs:
- python how to find an element in tuple by value
- python program to count pair in an array or list whose product is divisible by k
- python how to find all indexes of an item in a list
- python how to check if an item exists in list
- python how to access characters in string by index
- create an empty numpy array of given length or shape data type in python
- python how to find keys by value in dictionary