How to rotate image in Python pillow
Pillow is a fork for the Python Imaging Library, PIL, that provides a variety of image-processing features to manipulate images and apply different filters to them. It is a powerful library that is easy to integrate with other Python libraries and frameworks. By using the pillow library, we can perform various operations on an image as follows:
Image manipulation, like rotating and flipping
Filter effects like blurring
Image enhancement, like adjusting brightness and contrast
Image overlay, like adding text on an image
In this answer, we shall apply left and right rotation on an image and observe how the image changes.
Required imports
We will use the PIL library to manipulate the image and then use matplotlib to display the results once the code is executed successfully.
import matplotlib.pyplot as pltfrom PIL import Imageimport requestsfrom io import BytesIO
matplotlib: To create visualizations and plots in the Python library.PIL: Computer vision library to apply operations on images.Image: To manipulate the images.
requests: To send the requests over the HTTP server to the website.BytesIO: To handle the binary data as an in-memory system.
Apply right rotate
A right rotation refers to turning the original image along the axis toward the East. This happens when the position of each pixel is transformed to achieve a right-rotated matrix. The color value of each original image pixel is shifted to a new position, changing the image's appearance.
Expected output
When we rotate the image by
When we rotate the image by
Let's right a code for the right image rotation to obtain the expected output.
Example code
In this example, we fetch an online image through its link address, apply 45-degree and 90-degree right rotation, and display it in a plot.
import matplotlib.pyplot as plt
from PIL import Image
import requests
from io import BytesIO
image_url = "https://images.pexels.com/photos/7644274/pexels-photo-7644274.jpeg?auto=compress&cs=tinysrgb&w=1600"
#fetch image
response = requests.get(image_url)
image_data = BytesIO(response.content)
original_image = Image.open(image_data)
#Apply right rotate
rotate_image45 = original_image.rotate(45)
rotate_image90 = original_image.rotate(90)
plt.figure(figsize=(10, 5))
plt.subplots_adjust(wspace=0.4, hspace=0.1)
plt.subplot(1, 3, 1)
plt.imshow(original_image)
plt.axis('off')
plt.title('Original Image')
plt.subplot(1, 3, 2)
plt.imshow(rotate_image45)
plt.axis('off')
plt.title('45 degree Right Rotate')
plt.subplot(1, 3, 3)
plt.imshow(rotate_image90)
plt.axis('off')
plt.title('90 degree Right Rotate')
plt.show()
Code explanation
Lines 1–4: Import the required libraries and modules.
Line 6: Store the link of the image that is to be used on
image_url. We can also use a local image file and give its exact name.Lines 9–11: Use
requestto fetch the image from the server and open it using theImage.open()method and pass the converted image to it.Lines 14–15: Use the
rotate()method ofImageclass and pass the angle of rotation as a parameter.
Note: We use positive angle values to obtain a right rotated image.
Lines 17–18: Specify the figure size that is to appear in the plot and assign the grid size.
Lines 21–24: Display the image using
imshow()and pass the original image to it, turn off the x-axis labels, and specify the title in thetitle().Lines 26–29: Display the image using
imshow()and pass the 45-degree rotation imagerotate_image45to it, turn off the x-axis labels, and specify the title in thetitle().Lines 31–34: Display the image using
imshow()and pass the 90-degree rotation imagerotate_image45to it, turn off the x-axis labels, and specify the title in thetitle().Line 30: Show the resultant plot.
Apply left rotate
A left rotation refers to turning the original image along the axis toward the West. This happens when the position of each pixel is transformed to achieve a left-rotated matrix. The color value of each original image pixel is shifted to a new position, changing the image's appearance.
Expected output
When we rotate the image by
When we rotate the image by
Example code
In this example, we fetch an online image through its link address, apply negative 45-degree and negative 90-degree left rotation, and display it in a plot.
import matplotlib.pyplot as plt
from PIL import Image
import requests
from io import BytesIO
image_url = "https://images.pexels.com/photos/7644274/pexels-photo-7644274.jpeg?auto=compress&cs=tinysrgb&w=1600"
#fetch image
response = requests.get(image_url)
image_data = BytesIO(response.content)
original_image = Image.open(image_data)
#Apply right rotate
rotate_image45 = original_image.rotate(-45)
rotate_image90 = original_image.rotate(-90)
plt.figure(figsize=(10, 5))
plt.subplots_adjust(wspace=0.4, hspace=0.1)
plt.subplot(1, 3, 1)
plt.imshow(original_image)
plt.axis('off')
plt.title('Original Image')
plt.subplot(1, 3, 2)
plt.imshow(rotate_image45)
plt.axis('off')
plt.title('45 degree left Rotate')
plt.subplot(1, 3, 3)
plt.imshow(rotate_image90)
plt.axis('off')
plt.title('90 degree left Rotate')
plt.show()
Code explanation
Lines 1–4: Import the required libraries and modules.
Line 6: Store the link of the image that is to be used on
image_url. We can also use a local image file and give its exact name.Lines 9–11: Use
requestto fetch the image from the server and open it using theImage.open()method and pass the converted image to it.Lines 14–15: Use the
rotate()method ofImageclass and pass the angle of rotation as a parameter.
Note: We use negative angle values to obtain a left rotated image.
Lines 17–18: Specify the figure size that is to appear in the plot and assign the grid size.
Lines 21–24: Display the image using
imshow()and pass the original image to it, turn off the x-axis labels, and specify the title in thetitle().Lines 26–29: Display the image using
imshow()and pass the negative 45-degree rotation imagerotate_image45to it, turn off the x-axis labels, and specify the title in thetitle().Lines 31–34: Display the image using
imshow()and pass the negative 90-degree rotation imagerotate_image45to it, turn off the x-axis labels, and specify the title in thetitle().Line 30: Show the resultant plot.
Real-life application
There are a lot of real-life scenarios where there is a need to rotate an image. Let's take a look at a few of the scenarios where image rotation is useful.
Common query
plt.subplot(1, 3, 1)
Why do we use 3 as a parameter here?
Free Resources