Rotate Images with PIL/Pillow

# Import Pillow:
from PIL import Image

# Load the original image:
img = Image.open("flowers.jpg")

original image

Counterclockwise Rotation

img2 = img.rotate(45)
img2.save("img2.jpg")

45 degrees counterclockwise

img3 = img.rotate(90)
img3.save("img3.jpg")

90 degrees counterclockwise

Clockwise Rotation

img4 = img.rotate(-45)
img4.save("img4.jpg")

45 degrees clockwise

Disable cropping of the output image

img5 = img.rotate(45, expand=True)
img5.save("img5.jpg")

disable cropping

Apply a resampling filter

# Nearest neighbor (default):
img6 = img.rotate(45, resample=Image.NEAREST)
img6.save("img6.jpg")

nearest neighbot

# Linear interpolation:
img7 = img.rotate(45, resample=Image.BILINEAR)
img7.save("img7.jpg")

linear interpolation

# Cubic spline interpolation:
img8 = img.rotate(45, resample=Image.BICUBIC)
img8.save("img8.jpg")

cubic spline interpolation

About this Entry


Comments