用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中实现从目录中过滤出指定文件类型的文件
Feb 02 Python
Python发送以整个文件夹的内容为附件的邮件的教程
May 06 Python
python实现的守护进程(Daemon)用法实例
Jun 02 Python
Python编程实现蚁群算法详解
Nov 13 Python
详解Python最长公共子串和最长公共子序列的实现
Jul 07 Python
Python判断对象是否相等及eq函数的讲解
Feb 25 Python
在django view中给form传入参数的例子
Jul 19 Python
python处理大日志文件
Jul 23 Python
Python实现投影法分割图像示例(一)
Jan 17 Python
python调用百度API实现人脸识别
Nov 17 Python
神经网络训练采用gpu设置的方式
Mar 03 Python
Python 避免字典和元组的多重嵌套问题
Jul 15 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.ini中文版(1)
2006/10/09 PHP
isset和empty的区别
2007/01/15 PHP
记录PHP错误日志 display_errors与log_errors的区别
2012/10/09 PHP
PHP header()函数常用方法总结
2014/04/11 PHP
php支付宝在线支付接口开发教程
2016/09/19 PHP
php 如何禁用eval() 函数实例详解
2016/12/01 PHP
IE 缓存策略的BUG的解决方法
2007/07/21 Javascript
一个简单的js动画效果代码
2010/07/20 Javascript
JS.findElementById()使用介绍
2013/09/21 Javascript
js实现跨域的几种方法汇总(图片ping、JSONP和CORS)
2015/10/25 Javascript
js实现网页收藏功能
2015/12/17 Javascript
JS使用正则截取两个字符串之间的字符串实现方法详解
2017/01/06 Javascript
微信小程序 PHP后端form表单提交实例详解
2017/01/12 Javascript
获取url中用&隔开的参数实例(分享)
2017/05/28 Javascript
JavaScript实现移动端轮播效果
2017/06/06 Javascript
Angularjs在360兼容模式下取数据缓存问题的解决办法
2017/06/22 Javascript
微信小程序实现弹出菜单功能
2018/06/12 Javascript
在Angular中使用JWT认证方法示例
2018/09/10 Javascript
微信小程序实现顶部导航特效
2019/01/28 Javascript
基于vue实现滚动条滚动到指定位置对应位置数字进行tween特效
2019/04/18 Javascript
jQuery控制input只能输入数字和两位小数的方法
2019/05/16 jQuery
vue.js 2.0实现简单分页效果
2019/07/29 Javascript
JavaScript页面加载事件实例讲解
2019/09/01 Javascript
python 将print输出的内容保存到txt文件中
2018/07/17 Python
Python的numpy库下的几个小函数的用法(小结)
2019/07/12 Python
用Pelican搭建一个极简静态博客系统过程解析
2019/08/22 Python
Python爬虫库BeautifulSoup获取对象(标签)名,属性,内容,注释
2020/01/25 Python
利用CSS3的3D效果制作正方体
2020/03/10 HTML / CSS
调用HTML5的Canvas API绘制图形的快速入门指南
2016/06/17 HTML / CSS
工程采购员岗位职责
2014/03/09 职场文书
党员群众路线自我剖析材料
2014/10/06 职场文书
2014年公司工作总结
2014/11/22 职场文书
导游词之襄阳古城
2019/09/27 职场文书
常用的MongoDB查询语句的示例代码
2021/07/25 MongoDB
Python3.8官网文档之类的基础语法阅读
2021/09/04 Python
SQL Server2019数据库备份与还原脚本,数据库可批量备份
2021/11/20 SQL Server