用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过滤函数filter()使用自定义函数过滤序列实例
Aug 26 Python
详细讲解用Python发送SMTP邮件的教程
Apr 29 Python
python列表操作之extend和append的区别实例分析
Jul 28 Python
Python向日志输出中添加上下文信息
May 24 Python
python进阶之多线程对同一个全局变量的处理方法
Nov 09 Python
利用rest framework搭建Django API过程解析
Aug 31 Python
dpn网络的pytorch实现方式
Jan 14 Python
Python 之 Json序列化嵌套类方式
Feb 27 Python
详解pyinstaller生成exe的闪退问题解决方案
Jun 19 Python
python 实现一个图形界面的汇率计算器
Nov 09 Python
Python实现灰色关联分析与结果可视化的详细代码
Mar 25 Python
Python经常使用的一些内置函数
Apr 11 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
js常见表单应用技巧
2008/01/09 Javascript
jquery表单验证使用插件formValidator
2012/11/10 Javascript
jQuery隔行变色与普通JS写法的对比
2013/04/21 Javascript
js浮点数保留两位小数点示例代码(四舍五入)
2013/12/26 Javascript
网页右下角弹出窗体实现代码
2014/06/05 Javascript
html+js+highcharts绘制圆饼图表的简单实例
2016/08/04 Javascript
vuex实现简易计数器
2016/10/27 Javascript
JS实现太极旋转思路分析
2016/12/09 Javascript
Javascript中的 “&” 和 “|” 详解
2017/02/02 Javascript
vue引入swiper插件的使用实例
2017/07/19 Javascript
Angular4集成ng2-file-upload的上传组件
2018/03/14 Javascript
Vue实现的父组件向子组件传值功能示例
2019/01/19 Javascript
Vue自定义全局Toast和Loading的实例详解
2019/04/18 Javascript
vue学习笔记之Vue中css动画原理简单示例
2020/02/29 Javascript
八种Vue组件间通讯方式合集(推荐)
2020/08/18 Javascript
nuxt 服务器渲染动态设置 title和seo关键字的操作
2020/11/05 Javascript
[04:54]DOTA2-DPC中国联赛1月31日Recap集锦
2021/03/11 DOTA
python中pygame模块用法实例
2014/10/09 Python
ipython和python区别详解
2019/06/26 Python
在Django admin中编辑ManyToManyField的实现方法
2019/08/09 Python
Python:slice与indices的用法
2019/11/25 Python
Python 生成一个从0到n个数字的列表4种方法小结
2019/11/28 Python
澳大利亚现代波西米亚风格女装网站:Bohemian Traders
2018/04/16 全球购物
什么是URL
2015/12/13 面试题
介绍一下木马病毒的种类
2015/07/26 面试题
自荐书模板
2013/12/15 职场文书
家长对孩子的评语
2014/04/18 职场文书
工厂搬迁方案
2014/05/11 职场文书
幼儿发展评估方案
2014/06/11 职场文书
商务经理岗位职责
2014/08/03 职场文书
见习报告怎么写
2014/10/31 职场文书
驳回起诉裁定书
2015/05/19 职场文书
催款函范本大全
2015/06/24 职场文书
2016元旦晚会主持词开场白和结束语
2015/12/04 职场文书
六年级作文之家庭作文
2019/12/12 职场文书
Pandas-DataFrame知识点汇总
2022/03/16 Python