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!
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:
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:
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.
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!
- an intro to the python imaging library/pillow
Published at DZone with permission of Mike Driscoll, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.