Rotate an image
To rotate an image, the rotate() method can be used. This method accepts a single parameter – the angle in degrees (counter-clockwise):
new_image = Image.open('handsome.jpg') rotated_image = new_image.rotate(45) rotated_image.show()
To rotate the image in 90 degree steps, you can also use the transpose() method:
transposed_image = new_image.transpose(Image.ROTATE_90) transposed_image.show()
The other two valid values are ROTATE_180 and ROTATE_270.
The transpose() method can also be used to flip an image around its horizontal or vertical axis. To flip an image around its vertical axis, the FLIP_LEFT_RIGHT value is used. To flip around its horizontal axis, the FLIP_TOP_BOTTOM is used:
flipped_image = new_image.transpose(Image.FLIP_LEFT_RIGHT) flipped_image.show()