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 Create a Diff of an Image in Python

How to Create a Diff of an Image in Python

This article is a quick, hands-on example of comparing two images using Python and the Pillow library.

Mike Driscoll user avatar by
Mike Driscoll
·
Oct. 13, 16 · Tutorial
Like (4)
Save
Tweet
Share
5.62K Views

Join the DZone community and get the full member experience.

Join For Free

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() is none: # there is no difference between the images return else: 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!

Diff 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

  • Better Performance and Security by Monitoring Logs, Metrics, and More
  • Distributed SQL: An Alternative to Database Sharding
  • How To Use Terraform to Provision an AWS EC2 Instance
  • Understanding gRPC Concepts, Use Cases, and Best Practices

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: