DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. How to Rotate/Mirror Photos With Python

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.

Mike Driscoll user avatar by
Mike Driscoll
·
Oct. 09, 17 · Tutorial
Like (1)
Save
Tweet
Share
25.99K Views

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

image title

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:

image title

in the example above, we rotated the praying mantis 90 degrees counter-clockwise.

mirroring an image

image title

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:

  • pil.image.flip_left_right
  • pil.image.flip_top_bottom
  • pil.image.transpose

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.

wrapping up

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!

related reading

  • pillow documentation on the image module
  • how to crop an image in python
  • an intro to the python imaging library/pillow
Python (language)

Published at DZone with permission of Mike Driscoll, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Why You Should Automate Code Reviews
  • Streamlining Your Workflow With the Jenkins HTTP Request Plugin: A Guide to Replacing CURL in Scripts
  • 3 Ways That You Can Operate Record Beyond DTO [Video]
  • Agile Scrum and the New Way of Work in 2023

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: