用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爬虫之抓取百度贴吧代码分享
Nov 06 Python
kNN算法python实现和简单数字识别的方法
Nov 18 Python
一键搞定python连接mysql驱动有关问题(windows版本)
Apr 23 Python
Python中的左斜杠、右斜杠(正斜杠和反斜杠)
Aug 30 Python
Python基于time模块求程序运行时间的方法
Sep 18 Python
python生成密码字典的方法
Jul 06 Python
Python编程在flask中模拟进行Restful的CRUD操作
Dec 28 Python
Golang GBK转UTF-8的例子
Aug 26 Python
win10下python2和python3共存问题解决方法
Dec 23 Python
Django中modelform组件实例用法总结
Feb 10 Python
python字典和json.dumps()的遇到的坑分析
Mar 11 Python
TensorFlow-gpu和opencv安装详细教程
Jun 30 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
php 计算两个时间戳相隔的时间的函数(小时)
2009/12/18 PHP
php生成不重复随机数、数组的4种方法分享
2015/03/30 PHP
PHP实现即时输出、实时输出内容方法
2015/05/27 PHP
php实现模拟post请求用法实例
2015/07/11 PHP
php的mail函数发送UTF-8编码中文邮件时标题乱码的解决办法
2015/10/20 PHP
PHP实现一个轻量级容器的方法
2019/01/28 PHP
javascript与asp.net(c#)互相调用方法
2009/12/13 Javascript
浅析js中的浮点型运算问题
2014/01/06 Javascript
jquery实现多屏多图焦点图切换特效的方法
2015/05/04 Javascript
js微信分享实现代码
2020/10/11 Javascript
详解Angular5 服务端渲染实战
2018/01/04 Javascript
JavaScript实现的简单加密解密操作示例
2018/06/01 Javascript
jquery实现购物车基本功能
2019/10/25 jQuery
原生js实现贪食蛇小游戏的思路详解
2019/11/26 Javascript
JS绘图Flot如何实现动态可刷新曲线图
2020/10/16 Javascript
Python 返回汉字的汉语拼音
2009/02/27 Python
Python实现抓取百度搜索结果页的网站标题信息
2015/01/22 Python
Python字符串切片操作知识详解
2016/03/28 Python
Python中pandas dataframe删除一行或一列:drop函数详解
2018/07/03 Python
windows下pycharm安装、创建文件、配置默认模板
2018/07/31 Python
使用python opencv对目录下图片进行去重的方法
2019/01/12 Python
Django给admin添加Action的步骤详解
2019/05/01 Python
使用python实现离散时间傅里叶变换的方法
2019/09/02 Python
python 定义类时,实现内部方法的互相调用
2019/12/25 Python
django-xadmin根据当前登录用户动态设置表单字段默认值方式
2020/03/13 Python
pyqt5 textEdit、lineEdit操作的示例代码
2020/08/12 Python
css3实现二维码扫描特效的示例
2020/10/29 HTML / CSS
HTML5中5个简单实用的API
2014/04/28 HTML / CSS
千禧酒店及度假村官方网站:Millennium Hotels and Resorts
2019/05/10 全球购物
银行会计财务工作个人的自我评价
2013/10/29 职场文书
总经理司机岗位职责
2014/02/06 职场文书
宾馆总经理岗位职责
2014/02/14 职场文书
安全生产责任书
2014/03/12 职场文书
纪念九一八事变演讲稿1000字
2014/09/14 职场文书
2016最新离婚协议书范本及程序
2016/03/18 职场文书
Java实现注册登录跳转
2022/06/16 Java/Android