Pillow Image Rotate

In this chapter, you will learn how to rotate an image and how to flip it using Python Pillow library.

Rotating Images

You can rotate images by calling the Pillow’s rotate() method on an object of the Image class. The angle of rotation(in degrees) in the counter-clockwise direction is passed to rotate() as an argument.

The size of the rotated image is the same as the original image. Hence, rotation can crop the image from some parts.

Note– In Pillow, when you rotate an image, it will always be in counter-clockwise direction. To rotate an image in clockwise direction, you have to pass the angle of rotation as a negative value.

Syntax:

image_object.rotate(angle_in_degrees)

Example

# Rotate the image 45 degrees counter-clockwise and store it in img_rot
img_rot = img.rotate(45)

print(img.size)
# Outputs- (3264, 2448)
print(img_rot.size)
# Outputs- (3264, 2448)

img_rot.show()

The output of this will be-

Using Pillow to rotate an Image by 45 degrees counter-clockwise

Note– Notice that the size of the rotated image is the same as the original image and how it crops the corners of the image.

You can remove this cropping by setting the expand argument to True when calling the rotate() method. This will increase the size of the rotated image to accommodate the entire rotated image.

Example

# Rotate the image 45 degrees counter-clockwise and store it in img_rot_expanded
img_rot_expanded = img.rotate(45, expand=True)

print(img.size)
# Outputs- (3264, 2448)
print(img_rot_expanded.size)
# Outputs- (4040, 4040)

img_rot.show()

The output of this will be-

Using Pillow to rotate an Image by 45 degrees counter-clockwise without being cropped

Example

# Rotate the image 90 degrees clockwise and store it in img_rot_2
img_rot_2 = img.rotate(-90)

print(img.size)
# Outputs- (3264, 2448)
print(img_rot_2.size)
# Outputs- (3264, 2448)

img_rot_2.show()

The output of this will be-

Using Pillow to rotate an Image by 90 degrees clockwise

Transposing Images

You can transpose images by calling the Pillow’s transpose() method on an object of the Image class. transpose() accepts one of the several Image attributes as an argument.

The table below lists a few of the Image Attributes that can be passed to transpose() along with the effect it produces.

Image AttributeEffect
Image.FLIP_LEFT_RIGHTFlip the image about the vertical axis.
Image.FLIP_TOP_BOTTOMFlip the image about the horizontal axis
Image.ROTATE_90Rotate the image 90° in the anti-clockwise direction.
Image.ROTATE_180Rotate the image 180° in the anti-clockwise direction.
Image.ROTATE_270Rotate the image 270° in the anti-clockwise direction.

Example

In this example, we will be using transpose() method to flip the image horizontally(about the vertical axis).

# Transpose the Image by flipping it about the vertical axis and store it as img_tr_hor
img_tr_hor = img.transpose(Image.FLIP_LEFT_RIGHT)

img_tr_hor.show()

The output of this will be-

An image flipped horizontally

The figure below compares images when various Image attributes are passed to transpose().

Variants of the images with different values of transpose.

Note– Using transpose(Image.ROTATE_90) is the same as using rotate(90, expand = True). Similarly, transpose(Image.ROTATE_180) and transpose(Image.ROTATE_270) is the same as rotate(180, expand = True) and rotate(270, expand = True) respectively.