Rotate Image 45, 90, 180, 270 degrees - Pillow - Examples
Python Pillow - Rotate Image
To rotate an image by an angle with Python Pillow, you can use rotate() method on the Image object. rotate() method rotates the image in counter clockwise direction.
In this tutorial, we shall learn how to rotate an image, using PIL Python library, with the help of example programs.
Syntax of PIL Image.rotate()
The syntax of rotate() method is as shown in the following code block.
Image.rotate(angle, resample=0, expand=0, center=None, translate=None, fillcolor=None)
where
- angle – In degrees counter clockwise.
- resample – An optional resampling filter. This can be one of PIL.Image.NEAREST (use nearest neighbour), PIL.Image.BILINEAR (linear interpolation in a 2x2 environment), or PIL.Image.BICUBIC (cubic spline interpolation in a 4x4 environment). If omitted, or if the image has mode “1” or “P”, it is set PIL.Image.NEAREST. See Filters.
- expand – Optional expansion flag. If true, expands the output image to make it large enough to hold the entire rotated image. If false or omitted, make the output image the same size as the input image. Note that the expand flag assumes rotation around the center and no translation.
- center – Optional center of rotation (a 2-tuple). Origin is the upper left corner. Default is the center of the image.
- translate – An optional post-rotate translation (a 2-tuple).
- fillcolor – An optional color for area outside the rotated image.
Examples
1: Rotate given image by 45 degrees
In the following example, we will rotate the image by 45 degrees in counter clockwise direction.
Python Program
from PIL import Image
#read the image
im = Image.open("sample-image.png")
#rotate image
angle = 45
out = im.rotate(angle)
out.save('rotate-output.png')
Input Image - sample-image.png
Output Image - rotate-image.png
The size of the original image is preserved. You can make the size of the output image adjust to the rotation.
2. Rotate image and adjust the output size
In the following example, we will adjust the size of output image to the rotation, by using the parameter expand=True.
from PIL import Image
#read the image
im = Image.open("sample-image.png")
#rotate image
angle = 45
out = im.rotate(angle, expand=True)
out.save('rotate-output.png')
Output Image
3. Rotate image by 90 degrees
You can rotate an image by 90 degrees in counter clockwise direction by providing the angle=90. We also give expand=True, so that the rotated image adjusts to the size of output.
from PIL import Image
#read the image
im = Image.open("sample-image.png")
#rotate image by 90 degrees
angle = 90
out = im.rotate(angle, expand=True)
out.save('rotate-output.png')
4. Rotate image by 180 degrees
In this Python Pillow example, we will rotate the image by 180 degrees.
from PIL import Image
#read the image
im = Image.open("sample-image.png")
#rotate image by 180 degrees
angle = 180
out = im.rotate(angle, expand=True)
out.save('rotate-output.png')
Output Image
Summary
Concluding this tutorial of Python Examples, we learned how to rotate an image using Python PIL library.