For the past couple of years, I’ve been writing automated tests for my employer. One of the many types of tests that I do is comparing how an application draws. Does it draw the same way every single time? If not, then we have a serious problem. An easy way to check that it draws the same each time is to take a screenshot and then compare it to future versions of the same drawing when the application gets updated.
The Pillow library provides a handy tool for this sort of thing that is called ImageChops. If you don’t already have Pillow, you should go install it now so you can follow along with this short tutorial.
Comparing Two Images
The first thing we need to do is find two images that are slightly different. You can create your own by using burst mode on your camera and taking a bunch of photos of animals as they move, preferably while using a tripod. Or you can take an existing photo and just add some kind of overlay, such as text. I’m going to go with the latter method. Here is my original photo of Multnomah Falls in Oregon:
And here’s the modified version where I added some text to identify the location of the photo:
Now let’s use ImageChops to find the difference for us!
import Image import ImageChops def compare_images(path_one, path_two, diff_save_location): """ Compares to images and saves a diff image, if there is a difference @param: path_one: The path to the first image @param: path_two: The path to the second image """ image_one = Image.open(path_one) image_two = Image.open(path_two) diff = ImageChops.difference(image_one, image_two) if diff.getbbox(): diff.save(diff_save_location) if __name__ == '__main__': compare_images('/path/to/multnomah_falls.jpg', '/path/to/multnomah_falls_text.jpg', '/path/to/diff.jpg')
Here we have a simple function that we can use to find differences in images. All you need to do is pass it three paths! The first two paths are for the images that we want to compare. The last path is where to save the diff image, if we find a diff. For this example, we should definitely find a diff and we did. Here’s what I got when I ran this code:
The Pillow package has many amazing features for working with images. I enjoy photography so it’s fun to be able to take photos and then use my favorite programming language to help me do neat things with the results. You should give this a try as well and read the Pillow documentation to see what else you can do with this clever package!
- Python Pillow official website
- Pillow documentation
- An Intro to the Python Imaging Library / Pillow
6 thoughts on “How to Create a Diff of an Image in Python”
Why indeed? I don’t know why I did it like that. Normally I don’t. Anyway, I updated the example to use your simpler syntax.
Pingback: Differentiating screenshot images | apmvision
That’s a good example to illustrate the diff between images. Earlier, I was thinking to use the SciPy library for the similar purpose. But the solution given here is more straight forward.
Can we design the diff page like white screen with diff as red
I’m sure you could. You would probably need to play around with the RGB values in the diff image to make it look the way you want though.
Pingback: pythonå›¾åƒå¤„ç†å·¥å…·pillowç»ƒä¹ :æ¯”è¾ƒå›¾åƒå·®å¼‚ - ç®—æ³•ç½‘
Comments are closed.