How to rotate an image in Pillow Python

In this article, you’ll see how to rotate an image in Pillow Python. To rotate an image you have to specify the angle of rotation.

# Import the PIL library
from PIL import Image

# Read the image from location
img = Image.open("C:\\AiHints\\cats.jpg")

# Rotate the image 45 degree
rot_img = img.rotate(45)

# Show the Rotated Image
rot_img.show()

# Save the Rotated Image
rot_img.save("C:\\AiHints\\Rotated_Image.jpg") 

Output:

Rotated_Image

Rotate an Image with expand parameter

# Import the PIL library
from PIL import Image

# Read the image from location
img = Image.open("C:\\AiHints\\cats.jpg")

# Rotate the image 45 degree
rot_img = img.rotate(45, expand=True)

# Show the Rotated Image
rot_img.show()

# Save the Rotated Image
rot_img.save("C:\\AiHints\\Rotated_Expand.jpg") 

Output:

Rotated_Expand

Recommendation: Computer Vision: Models, Learning, and Inference Book

Leave a Comment

Your email address will not be published.