用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 相关文章推荐
如何搜索查找并解决Django相关的问题
Jun 30 Python
python+Django+apache的配置方法详解
Jun 01 Python
Python 编码Basic Auth使用方法简单实例
May 25 Python
利用Python代码实现数据可视化的5种方法详解
Mar 25 Python
Python简单计算给定某一年的某一天是星期几示例
Jun 27 Python
python 梯度法求解函数极值的实例
Jul 10 Python
python获取Pandas列名的几种方法
Aug 07 Python
python TK库简单应用(实时显示子进程输出)
Oct 29 Python
python打印n位数“水仙花数”(实例代码)
Dec 25 Python
Django 返回json数据的实现示例
Mar 05 Python
pyqt5中动画的使用详解
Apr 01 Python
jupyter notebook 多行输出实例
Apr 09 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+javascript实现二级级联菜单的制作
2008/05/06 PHP
php正则过滤html标签、空格、换行符的代码(附说明)
2010/10/25 PHP
thinkPHP框架实现的短信接口验证码功能示例
2018/06/20 PHP
关于js中alert弹出窗口文本换行问题简单详细说明
2012/12/11 Javascript
jquery操作cookie插件分享
2014/01/14 Javascript
json字符串之间的相互转换示例代码
2014/08/21 Javascript
js获得当前系统日期时间的方法
2015/05/06 Javascript
Bootstrap组件(一)之菜单
2016/05/11 Javascript
PHP捕捉异常中断的方法
2016/10/24 Javascript
js canvas实现擦除效果示例代码
2017/04/26 Javascript
基于BootStrap实现简洁注册界面
2017/07/20 Javascript
jQuery实现checkbox即点即改批量删除及中间遇到的坑
2017/11/11 jQuery
javascript实现文件拖拽事件
2018/03/29 Javascript
Vue.js 时间转换代码及时间戳转时间字符串
2018/10/16 Javascript
js实现固定区域内的不重叠随机圆
2019/10/24 Javascript
小程序中设置缓存过期的实现方法
2020/01/14 Javascript
将图片文件嵌入到wxpython代码中的实现方法
2014/08/11 Python
Python使用Beautiful Soup包编写爬虫时的一些关键点
2016/01/20 Python
为什么str(float)在Python 3中比Python 2返回更多的数字
2018/10/16 Python
python事件驱动event实现详解
2018/11/21 Python
Python中生成一个指定长度的随机字符串实现示例
2019/11/06 Python
使用pandas的box_plot去除异常值
2019/12/10 Python
详解python UDP 编程
2020/08/24 Python
python3实现语音转文字(语音识别)和文字转语音(语音合成)
2020/10/14 Python
使用python爬取抖音app视频的实例代码
2020/12/01 Python
Python中lru_cache的使用和实现详解
2021/01/25 Python
关于canvas绘制模糊问题的解决方法
2019/09/24 HTML / CSS
杭州信雅达系统.NET工程师面试试题
2015/02/08 面试题
物流仓管员岗位职责
2013/12/04 职场文书
时尚休闲吧创业计划书
2014/01/25 职场文书
大学生实习鉴定评语
2014/04/25 职场文书
2014年教育实习工作总结
2014/11/22 职场文书
失职检讨书大全
2015/01/26 职场文书
如何解决.cuda()加载用时很长的问题
2021/05/24 Python
Python中super().__init__()测试以及理解
2021/12/06 Python
JS前端使用canvas实现扩展物体类和事件派发
2022/08/05 Javascript