- PIL Home
- Python Image Library Intro
- Python Image Library Setup
- Python Image Library Basics
- Python Image Library Hierarchy
- Python Image Library Modules
- Python ImageColor Module
- Python ImageDraw Module
- Python ImageStat Module
- Python ImageTK Module
- Python ImageFile Module
- Python ImageFilter Module
- Python ImageGrab Module
- Python ImageMath Module
- Python - Loading Image
- Python - Rotating Image
- Python - Blurring Image
- Python - Converting Image
- Python - Cropping Image
- Python - Image Thumbnails
- Python - Text on Images
- Python - Multiple Image Copies
- Python - Saving Image As PDF
- Python - Saving Screenshot
- Python - Resizing Image
- Python - Adding Watermark
- Python Image Library Resources
- Python - Quick Guide
- Python - Useful Resources
- Python - Discussion
Python - Rotating Image
Description
Rotating,turning,reversing or flipping the Image is a feature that allows you to turn the image either in clockwise or anticlockwise direction.We can use one among the softwares like MS Paint,photoshop,Picasa or any online image editor to rotate the image.We are rotate the images with 90,180,270 degrees either in clockwise or anticlockwise direction.
We can also rotate the images with Python combing with Python Image Library.Other than 90,180,270 degrees we have the flexibility of rotating as per our need.
For rotating the image we need to import Image module.
Let us take the below image and will try to rotate the image.
First import the Image module and load the image with open method in Image.Make sure to give the complete path of the image where it has been placed.The image name can only be given if the image resides in the same program directory.
Note : Make sure that you have the image named scenary_rotate.jpg in the program directory.
# Import Pillow:
from PIL import Image
# Load the original image:
img = Image.open("scenary_rotate.jpg")
from PIL import Image
# Load the original image:
img = Image.open("scenary_rotate.jpg")
Apply the rotate method in Image module to rotate the image in counterclockwise direction as below.
img2 = img.rotate(45)
Save the blurred file with save method from Image module.
img2.save("scenary_45.jpg")
Syntax
Following is the syntax of functions been used.
# Import Pillow:
from PIL import Image
# Load the original image:
object = Image.open(image)
object = object.rotate(angle in degrees) object.save(image)
from PIL import Image
# Load the original image:
object = Image.open(image)
object = object.rotate(angle in degrees) object.save(image)
Parameters
For the rotate method we need to pass the integer value based on the requirement for rotating the image.Note:If image is in same directory where your python program resides, you can give the image name itself. If the image resides in some other directory, you are supposed to define the complete path of the image.
Return value
The new image which has been rotated will be saved to the local directory with the save method.Examples
The following examples shows how the image can be rotated in clockwise and anticlockwise directions.
from PIL import Image
# Load the original image:
img = Image.open("scenary_rotate.jpg")
img2 = img.rotate(270)
img2.save("scenary_45.jpg")
# Load the original image:
img = Image.open("scenary_rotate.jpg")
img2 = img.rotate(270)
img2.save("scenary_45.jpg")
Before
After
Now rotating the image in 90 degrees.
from PIL import Image
# Load the original image:
img = Image.open("scenary_90.jpg")
img2 = img.rotate(90)
img2.save("scenary_90.jpg")
# Load the original image:
img = Image.open("scenary_90.jpg")
img2 = img.rotate(90)
img2.save("scenary_90.jpg")
Rotating the image in clockwise direction
from PIL import Image
# Load the original image:
img = Image.open("scenary_90.jpg")
img2 = img.rotate(-45)
img2.save("scenary_clockwise_45.jpg")
# Load the original image:
img = Image.open("scenary_90.jpg")
img2 = img.rotate(-45)
img2.save("scenary_clockwise_45.jpg")
In the above image, during rotating the image, the image gets cropped automatically if the image doesn't find in the frame.For this we can pass the optional argument expand=True as below.
from PIL import Image
# Load the original image:
img = Image.open("scenary_90.jpg")
img2 = img.rotate(45, expand=True)
img2.save("scenary_disable_cropping.jpg")
# Load the original image:
img = Image.open("scenary_90.jpg")
img2 = img.rotate(45, expand=True)
img2.save("scenary_disable_cropping.jpg")
Advertisements