未加星标

How to Create a Diff of an Image in Python

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二04 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

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:


How to Create a Diff of an Image in Python

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


How to Create a Diff of an Image in Python

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:


How to Create a Diff of an Image in Python
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 python Pillow official website Pillow documentation An Intro to the Python Imaging Library / Pillow

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

主题: Python
分页:12
转载请注明
本文标题:How to Create a Diff of an Image in Python
本站链接:http://www.codesec.net/view/481746.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(29)