Rotate An Image Using Python Image Processing Library - Pillow

Overview:

Rotation of Image is one of the simple Image transform operations that can be applied on an Image. By image rotation the image is rotated about its center by specified number of degrees. Image rotation is a specialization of affine transformation.

Image rotation results in some pixel values beyond image boundaries that is pixel values lying outside the dimension of the image. Such values will not be displayed in the output image shown. This happens when the number of degrees specified for Image rotation is not an integer multiple of 90 degrees.

Rotation using Python and Pillow:

The rotate() method of Python Image Processing Library Pillow takes number of degrees as a parameter and rotates the  image in counter clockwise direction to the number of degrees specified.

Alternately, the transpose method can also be used with one of the constants Image.ROTATE_90, Image.ROTATE_180 and Image.ROTATE_270.

Example:

# import the Python Image processing Library

from PIL import Image

 

# Create an Image object from an Image

colorImage  = Image.open("./effil.jpg")

 

# Rotate it by 45 degrees

rotated     = colorImage.rotate(45)

# Rotate it by 90 degrees

transposed  = colorImage.transpose(Image.ROTATE_90)

 

# Display the Original Image

colorImage.show()

 

# Display the Image rotated by 45 degrees

rotated.show()

 

# Display the Image rotated by 90 degrees

transposed.show()

 

Output:

Image before rotation using Python Image Processing Library - Pillow

Image after rotation by 45 degrees using Python Image Processing Library - Pillow

Image after rotation by 90 degrees using Python Image Processing Library - Pillow

 


Copyright 2019 © pythontic.com