DZone
Web Dev Zone
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
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > How to Crop a Photo With Python

How to Crop a Photo With Python

Looking to help automate photo cropping? Look no further than this article, then, as we take a look at how to accomplish just that using Python!

Mike Driscoll user avatar by
Mike Driscoll
·
Oct. 04, 17 · Web Dev Zone · Tutorial
Like (2)
Save
Tweet
38.20K Views

Join the DZone community and get the full member experience.

Join For Free

if you like taking photos, then you will probably also find yourself cropping your photos from time to time. i will crop photos to get rid of background noise or to just focus more on the subject i was trying to capture. i also like to take high-resolution photos of insects or other small creatures and then crop it down to make it seem like i was even closer to the insect than i really was.

now, most people will use a photo editing application to crop their image, such as photoshop elements. i use these kinds of tools too, but you can also use the python programming language to do the cropping for you. one good example where you might want to use python is if you have thousands of scanned images of the same type, then it makes more sense to just write a script to do the cropping for you.

the most popular package for image manipulation in python is the pillow package, a “friendly fork of the python imaging library (pil).” you can install pillow using pip:

pip install pillow

now that we have pillow installed, we just need a photo. here’s one of a grasshopper i took:

image title

let’s write some code to try to crop the picture down to just the grasshopper’s head:

from pil import image

def crop(image_path, coords, saved_location):
    """
    @param image_path: the path to the image to edit
    @param coords: a tuple of x/y coordinates (x1, y1, x2, y2)
    @param saved_location: path to save the cropped image
    """
    image_obj = image.open(image_path)
    cropped_image = image_obj.crop(coords)
    cropped_image.save(saved_location)
    cropped_image.show()


if __name__ == '__main__':
    image = 'grasshopper.jpg'
    crop(image, (161, 166, 706, 1050), 'cropped.jpg')

the first thing we do in this code is to import the image sub-module from pil. then we create a crop() function that takes 3 parameters:

  • image_path – the file path to the file you want to crop.
  • coords – a 4-element tuple that contains the beginning and end coordinates to crop the image to.
  • saved_location – the file path to save the cropped file to.

after we open the initial image, we get an object back that we can call crop() on. the crop method takes the coordinates we passed in and crops the image down appropriately and returns a second image object. we then call this second image object’s save() method and tell it to save it to the specified location.

when you run the code, it will show the cropped image as well as save it:

image title

this is pretty close to what i wanted. you can experiment a bit with the x/y coordinates in the code above to try cropping the image in various ways to see how that works.


wrapping up

this code should probably have a check in it that prevents the user from overwriting the original image. any good photo editor will not overwrite the original photo as that is really annoying and usually a bad thing. but i will leave that to the reader to figure it out.

anyway, the pillow project is a really powerful and helpful package for working with images in python. give it a try and see what fun things you can accomplish!


related reading

  • 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

  • An Introduction to Graph Data
  • Use Lambda Function URL To Write a Serverless App Backed by DynamoDB
  • How to Handle Early Startup Technical Debt (Or Just Avoid it Entirely)
  • 3 Predictions About How Technology Businesses Will Change In 10 Years

Comments

Web Dev Partner Resources

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • 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:

DZone.com is powered by 

AnswerHub logo