Rotate an image in Python – Multiple ways
Let’s look at how to rotate an image using Python in this tutorial. By using image rotation, the picture is rotated by a predetermined number of degrees with respect to its center. A geometric transformation is an image’s rotation. Either forward transformation or inverse transformation may be used to accomplish the task.
How to rotate an image using Python?
We occasionally encounter situations when we must rotate a picture at a specific angle. In this article, we’ll go through alternative approaches to rotate a picture by a specific angle in Python, including utilizing the open-CV library and the Python image library.
The image we will use for our code is:
Rotate Image Using Python Image Library (PIL)
Python Image Library(PIL) is a module that contains in-fabricated capabilities in python to control and work with pictures as a contribution to the capabilities. The capability utilizes a Backwards change. In the event that the Quantity Of Degrees that we have Determined for Picture Revolution isn’t a Number Numerous of 90 Degrees, then, at that point, some Pixel Values Past Picture Limits, i.e., Pixel values lying outside the Element of the picture. Such Qualities are not shown in the resulting picture.
To turn a picture by a point with a python pad, you can utilize the rotate() technique on the Picture object. The rotate() strategy is utilized to turn the picture in a counter-clockwise bearing.
Python Code will be:
# import the Library from PIL import Image actual_image = Image.open("1664721752902.jpg") # Rotate the Image By 180 Degree rotate_image = actual_image.rotate(180) rotate_image.show()
The output will be:
Rotate image Using Open-CV, rotate by an angle
Everyone is aware that the Python Open-CV module can handle real-time computer vision applications, which is common knowledge. Imutils, an image processing package used by Open-CV, deals with images. In Python, you may rotate an image by a specified angle using the imutils.rotate() method.
Python Code will be:
import cv2 # importing cv import imutils actual_image = cv2.imread(r"1664721752902.jpg") Rotated_image = imutils.rotate(actual_image, angle=180) cv2.imshow("Rotated Image", Rotated_image) # The Image Until Any Key is Pressed cv2.waitKey(0)
The output will be:
Also read: Convert PIL image to NumPy array in Python
Leave a Reply