How to Create a Diff of an Image in Python

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:

multnomah_falls

And here’s the modified version where I added some text to identify the location of the photo:

multnomah_falls_text

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:

diff


Wrapping Up

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!


Related Reading

6 thoughts on “How to Create a Diff of an Image in Python”

  1. Pingback: Differentiating screenshot images | apmvision

  2. 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.

  3. Pingback: python图像处理工具pillow练习:比较图像差异 - 算法网

Comments are closed.