How to rotate an image using Python?

Spread the love
How to rotate an image using Python?

Let’s look at how to use Python to rotate an image in this tutorial. The image rotated by a predetermined number of degrees around its center using Image Rotation. An image is transformed geometrically when it rotates. Either forward transformation or inverse transformation can be used.

Here, inverse transformation is used by the Image Processing Library with Pillow. Some pixel values are beyond the image boundaries, or outside the image’s dimensions, if the number of degrees specified for image rotation is not an integer multiple of 90 degrees. The generated image will not display such values.

Technique 1: Making use of the Image Processing Library Pillow

Python3

# import the Python Image 
# processing Library 
from PIL import Image 

# Giving The Original image Directory 
# Specified 
Original_Image = Image.open("./gfgrotate.jpg") 

# Rotate Image By 180 Degree 
rotated_image1 = Original_Image.rotate(180) 

# This is Alternative Syntax To Rotate 
# The Image 
rotated_image2 = Original_Image.transpose(Image.ROTATE_90) 

# This Will Rotate Image By 60 Degree 
rotated_image3 = Original_Image.rotate(60) 

rotated_image1.show() 
rotated_image2.show() 
rotated_image3.show() 

The rotate() method of Python Image Processing Library Pillow Takes the number of degrees as a parameter and rotates the image in Counter Clockwise Direction to the number of degrees specified.

Method 2: Using Open-CV to rotate an image by an angle in Python

This is common that everyone knows that Python Open-CV is a module that will handle real-time applications related to computer vision. Open-CV works with image processing library imutils which deals with images. The imutils.rotate() function iused to rotate an image by an angle in Python.

Python3

import cv2 # importing cv 
import imutils 


# read an image as input using OpenCV 
image = cv2.imread(r".\gfgrotate.jpg") 

Rotated_image = imutils.rotate(image, angle=45) 
Rotated1_image = imutils.rotate(image, angle=90) 

# display the image using OpenCV of 
# angle 45 
cv2.imshow("Rotated", Rotated_image) 

# display the image using OpenCV of 
# angle 90 
cv2.imshow("Rotated", Rotated1_image) 

# This is used for To Keep On Displaying 
# The Image Until Any Key is Pressed 
cv2.waitKey(0) 

Even with this Open-CV, the image rotates counterclockwise to the designated number of degrees.

Are you looking to hone your Python skills or explore the world of programming? Your best resource for learning Python is our Master Python: Complete Beginner to Advanced Course. From basic programming concepts to sophisticated approaches, this course covers all you need to establish a strong foundation. With practical projects, real-world examples, and knowledgeable advice, you’ll develop the self-assurance to take on challenging coding tasks. This training is ideal whether you want to improve your skills or start from fresh. Learn Python, the language of the future, by enrolling today!