How to Rotate/Mirror Photos With Python
In this post, we take a look at how to quickly and easily rotate and mirror your photos using a simple Python script. Read on for the details.
Join the DZone community and get the full member experience.Join For Free
in our last article , we learned how to crop images with the pillow package. for this article, we will learn how to rotate and mirror our images.
rotating an image
rotating an image with python and pillow is quite simple. let’s take a look at some code:
from pil import image def rotate(image_path, degrees_to_rotate, saved_location): """ rotate the given photo the amount of given degreesk, show it and save it @param image_path: the path to the image to edit @param degrees_to_rotate: the number of degrees to rotate the image @param saved_location: path to save the cropped image """ image_obj = image.open(image_path) rotated_image = image_obj.rotate(degrees_to_rotate) rotated_image.save(saved_location) rotated_image.show() if __name__ == '__main__': image = 'mantis.png' rotate(image, 90, 'rotated_mantis.jpg')
here we just import the image module from pil and create a rotate() function. our custom rotate function takes the following parameters: the image path that we will be rotating, the degrees we want to rotate and where we want to save the result. the actual code is quite straight-forward. all we do is open the image and then call the image object’s rotate() method while passing it the number of degrees to rotate it counter-clockwise. then we save the result and call the image object’s show() method to see the result:
in the example above, we rotated the praying mantis 90 degrees counter-clockwise.
mirroring an image
now let’s try to flip or mirror our mantis image. here’s an example that mirrors the image from left to right:
from pil import image def flip_image(image_path, saved_location): """ flip or mirror the image @param image_path: the path to the image to edit @param saved_location: path to save the cropped image """ image_obj = image.open(image_path) rotated_image = image_obj.transpose(image.flip_left_right) rotated_image.save(saved_location) rotated_image.show() if __name__ == '__main__': image = 'mantis.png' flip_image(image, 'flipped_mantis.jpg')
this code is very similar to the previous example. the meat of this code is that we are using the image object’s transpose() method which takes one of the following constants:
you can also use one of pillow’s rotate constants here too, but we’re focusing just on the mirroring aspect of the transpose() method. try swapping in one of these other constants into the code above to see what happens.
now you know how to use the pillow package to rotate and flip/mirror your images. python makes this sort of thing quite trivial to do. you should give it a try and be sure to check out pillow’s documentation to find out what else you can do!
Published at DZone with permission of Mike Driscoll, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.