用python实现对比两张图片的不同


Posted in Python onFebruary 05, 2018
from PIL import Image
from PIL import ImageChops 
def compare_images(path_one, path_two, diff_save_location):
  """
  比较图片,如果有不同则生成展示不同的图片
  @参数一: path_one: 第一张图片的路径
  @参数二: path_two: 第二张图片的路径
  @参数三: diff_save_location: 不同图的保存路径
  """
  image_one = Image.open(path_one)
  image_two = Image.open(path_two)
  try: 
    diff = ImageChops.difference(image_one, image_two)
    if diff.getbbox() is None:
    # 图片间没有任何不同则直接退出
      print("【+】We are the same!")
    else:
      diff.save(diff_save_location)
  except ValueError as e:
    text = ("表示图片大小和box对应的宽度不一致,参考API说明:Pastes another image into this image."
        "The box argument is either a 2-tuple giving the upper left corner, a 4-tuple defining the left, upper, "
        "right, and lower pixel coordinate, or None (same as (0, 0)). If a 4-tuple is given, the size of the pasted "
        "image must match the size of the region.使用2纬的box避免上述问题")
    print("【{0}】{1}".format(e,text))
if __name__ == '__main__':
  compare_images('1.png',
          '2.png',
          '我们不一样.png')

执行结果:

用python实现对比两张图片的不同

用python实现对比两张图片的不同

用python实现对比两张图片的不同

用python实现对比两张图片的不同

第二种方法:

from PIL import Image
import math
import operator
from functools import reduce
def image_contrast(img1, img2):
  image1 = Image.open(img1)
  image2 = Image.open(img2)
  h1 = image1.histogram()
  h2 = image2.histogram()
  result = math.sqrt(reduce(operator.add, list(map(lambda a,b: (a-b)**2, h1, h2)))/len(h1) )
  return result
if __name__ == '__main__':
  img1 = "./1.png" # 指定图片路径
  img2 = "./2.png"
  result = image_contrast(img1,img2)
  print(result)

如果两张图片完全相等,则返回结果为浮点类型“0.0”,如果不相同则返回结果值越大。

同样用上面两张图片,执行结果为38,还是比较小的:

用python实现对比两张图片的不同

这样就可以在自动化测试用例中调用该方法来断言执行结果。

关于Pillow库的详细文档:

http://pillow.readthedocs.org/en/latest/index.html

总结

以上所述是小编给大家介绍的用python实现对比两张图片的不同,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Python 相关文章推荐
Python Mysql数据库操作 Perl操作Mysql数据库
Jan 12 Python
python 排列组合之itertools
Mar 20 Python
Python统计列表中的重复项出现的次数的方法
Aug 18 Python
Python同时向控制台和文件输出日志logging的方法
May 26 Python
Python编码爬坑指南(必看)
Jun 10 Python
Python中的连接符(+、+=)示例详解
Jan 13 Python
Python爬虫工程师面试问题总结
Mar 22 Python
在Python中给Nan值更改为0的方法
Oct 30 Python
python中几种自动微分库解析
Aug 29 Python
图解python全局变量与局部变量相关知识
Nov 02 Python
Jupyter Notebook远程登录及密码设置操作
Apr 10 Python
Python保存并浏览用户的历史记录
Apr 29 Python
使用pygame模块编写贪吃蛇的实例讲解
Feb 05 #Python
Python安装模块的常见问题及解决方法
Feb 05 #Python
Python实现的用户登录系统功能示例
Feb 05 #Python
python中numpy的矩阵、多维数组的用法
Feb 05 #Python
NumPy 如何生成多维数组的方法
Feb 05 #Python
python生成器,可迭代对象,迭代器区别和联系
Feb 04 #Python
python实现mysql的读写分离及负载均衡
Feb 04 #Python
You might like
基于mysql的bbs设计(一)
2006/10/09 PHP
PHP生成网页快照 不用COM不用扩展.
2010/02/11 PHP
php中将时间差转换为字符串提示的实现代码
2011/08/08 PHP
PHP中使用smarty生成静态文件的例子
2014/04/24 PHP
PHP下的Oracle客户端扩展(OCI8)安装教程
2014/09/10 PHP
浅谈htmlentities 、htmlspecialchars、addslashes的使用方法
2016/12/09 PHP
JavaScript 判断用户输入的邮箱及手机格式是否正确
2013/12/08 Javascript
node.js中的favicon.ico请求问题处理
2014/12/15 Javascript
使用jQuery在对象中缓存选择器的简单方法
2015/06/30 Javascript
微信小程序开发之视频播放器 Video 弹幕 弹幕颜色自定义实例
2016/12/08 Javascript
微信小程序 跳转传递数据的实例
2017/07/06 Javascript
微信小程序点击控件修改样式实例详解
2017/07/07 Javascript
jQuery读取本地的json文件(实例讲解)
2017/10/31 jQuery
浅谈webpack 自动刷新与解析
2018/04/09 Javascript
微信小程序左滑动显示菜单功能的实现
2018/06/14 Javascript
微信小程序swiper实现滑动放大缩小效果
2018/11/15 Javascript
vscode 开发Vue项目的方法步骤
2018/11/25 Javascript
js如何获取图片url的Blob值并预览示例代码
2019/03/07 Javascript
JS实现手风琴特效
2020/11/08 Javascript
[03:06]3分钟带你回顾DOTA2完美盛典&完美大师赛
2017/12/06 DOTA
Python中 Lambda表达式全面解析
2016/11/28 Python
Python程序退出方式小结
2017/12/09 Python
anaconda如何查看并管理python环境
2019/07/05 Python
django 连接数据库 sqlite的例子
2019/08/14 Python
Python 如何提高元组的可读性
2019/08/26 Python
Python3 使用selenium插件爬取苏宁商家联系电话
2019/12/23 Python
pytorch 实现L2和L1正则化regularization的操作
2021/03/03 Python
CSS3 伪类选择器 nth-child()说明
2010/07/10 HTML / CSS
播音主持女孩的自我评价分享
2013/11/20 职场文书
医学生自荐信
2013/12/03 职场文书
聘任书的写作格式及范文
2014/03/29 职场文书
献爱心活动总结
2014/05/07 职场文书
知识改变命运演讲稿
2014/05/21 职场文书
公司委托书格式范本
2014/09/16 职场文书
2015年12.4全国法制宣传日活动总结
2015/03/24 职场文书
mysql 获取相邻数据项
2022/05/11 MySQL