用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中Collection的使用小技巧
Aug 18 Python
Python3读取Excel数据存入MySQL的方法
May 04 Python
python selenium 对浏览器标签页进行关闭和切换的方法
May 21 Python
python使用turtle库与random库绘制雪花
Jun 22 Python
在mac下查找python包存放路径site-packages的实现方法
Nov 06 Python
python+unittest+requests实现接口自动化的方法
Nov 29 Python
详解django中url路由配置及渲染方式
Feb 25 Python
python3.x+pyqt5实现主窗口状态栏里(嵌入)显示进度条功能
Jul 04 Python
python爬虫 爬取超清壁纸代码实例
Aug 16 Python
python yield关键词案例测试
Oct 15 Python
python dumps和loads区别详解
Feb 04 Python
Python3爬虫中Selenium的用法详解
Jul 10 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+APACHE实现用户论证的方法
2006/10/09 PHP
数据库查询记录php 多行多列显示
2009/08/15 PHP
让PHP支持断点续传的源码
2010/05/16 PHP
PHP版网站缓存加快打开速度的方法分享
2012/06/03 PHP
Yii实现文章列表置顶功能示例
2016/10/18 PHP
laravel框架添加数据,显示数据,返回成功值的方法
2019/10/11 PHP
JavaScript 的继承
2011/10/01 Javascript
javascript简单实现类似QQ头像弹出效果的方法
2015/08/03 Javascript
js阻止浏览器默认行为触发的通用方法(推荐)
2016/05/15 Javascript
js判断所有表单项不为空则提交表单的实现方法
2016/09/09 Javascript
Vue自定义图片懒加载指令v-lazyload详解
2020/12/31 Javascript
webpack构建react多页面应用详解
2017/09/15 Javascript
使用node.js实现微信小程序实时聊天功能
2018/08/13 Javascript
vue生成文件本地打开查看效果的实例
2018/09/06 Javascript
vue中使用codemirror的实例详解
2018/11/01 Javascript
element-ui中el-upload多文件一次性上传的实现
2020/12/02 Javascript
[01:59]游戏“zheng”当时试玩会
2019/08/21 DOTA
python 正则式 概述及常用字符
2009/05/07 Python
Python实现windows下模拟按键和鼠标点击的方法
2015/03/13 Python
Python随机生成数模块random使用实例
2015/04/13 Python
Python实现注册登录系统
2017/08/08 Python
Python将多份excel表格整理成一份表格
2018/01/03 Python
python ---lambda匿名函数介绍
2019/03/13 Python
Django框架登录加上验证码校验实现验证功能示例
2019/05/23 Python
python语言的优势是什么
2020/06/17 Python
python tkinter实现连连看游戏
2020/11/16 Python
英国最大的LED专业零售商:Led Hut
2018/03/16 全球购物
加拿大品牌鞋包连锁店:Little Burgundy
2021/02/28 全球购物
RIP版本1跟版本2的区别
2013/12/30 面试题
函授教育个人学习的自我评价
2013/12/31 职场文书
大学社团活动策划书
2014/01/26 职场文书
小学生打架检讨书
2014/01/26 职场文书
《苏珊的帽子》教学反思
2014/04/07 职场文书
企业群众路线教育实践活动心得体会
2014/11/03 职场文书
2014年残疾人工作总结
2014/12/06 职场文书
十大最强格斗系宝可梦,超梦X仅排第十,第二最重格斗礼仪
2022/03/18 日漫