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')

执行结果:

第二种方法:

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,还是比较小的:

这样就可以在自动化测试用例中调用该方法来断言执行结果。

关于Pillow库的详细文档:

总结

以上所述是小编给大家介绍的用python实现对比两张图片的不同,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

python图像对比_用python实现对比两张图片的不同相关推荐

  1. python图像检测_用Python实现通过哈希算法检测图片重复的教程

    Iconfinder 是一个图标搜索引擎,为设计师.开发者和其他创意工作者提供精美图标,目前托管超过 34 万枚图标,是全球最大的付费图标库.用户也可以在 Iconfinder 的交易板块上传出售原创 ...

  2. python图像边缘检测_使用python获取图像中形状的轮廓(x,y)坐标

    我需要使用python获得下面图像的轮廓坐标(x,y)的矩阵.使用python获取图像中形状的轮廓(x,y)坐标 我尝试用OpenCV的精明探测器和发现的轮廓,但我得到了很多的轮廓,我不知道如何让一个 ...

  3. python 时间序列预测_使用Python进行动手时间序列预测

    python 时间序列预测 Time series analysis is the endeavor of extracting meaningful summary and statistical ...

  4. python 概率分布模型_使用python的概率模型进行公司估值

    python 概率分布模型 Note from Towards Data Science's editors: While we allow independent authors to publis ...

  5. python调整图像大小_使用Python调整图像大小

    作者|Nicholas Ballard 编译|VK 来源|Towards Data Science 可以说,每一个"使用计算机的人"都需要在某个时间点调整图像的大小.MacOS的预 ...

  6. python图像清晰度_图像的模糊检测方法

    关键字:blur detection Function:图像的清晰度检测英文表达为 image blue detection; 以此为关键字可以找到很多有关清晰度检测的demo和算法. 图像的清晰度检 ...

  7. python分类信息_用Python分析了 7 万款 App后,我们发现...

    原标题:用Python分析了 7 万款 App后,我们发现... 本文中使用 Scrapy 爬取了豌豆荚全网 70,000+ App ,并进行探索性分析. 写在前面:若对数据抓取部分不感兴趣,可以直接 ...

  8. python 免费空间_用python做大数据

    不学Python迟早会被淘汰?Python真有这么好的前景? 最近几年Python编程语言在国内引起不小的轰动,有超越Java之势,本来在美国这个编程语言就是最火的,应用的非常非常的广泛,而Pytho ...

  9. 使用python预测基金_使用python先知3 1创建预测

    使用python预测基金 This tutorial was created to democratize data science for business users (i.e., minimiz ...

最新文章

  1. 如何加入LSGO软件技术团队?
  2. 【邮箱使用】Yahoo邮箱POP3、SMTP开通以及设置方法
  3. 解决ubuntu打开windows中txt文件出现乱码现象
  4. Flume1.7.0的安装与使用
  5. s6-8 TCP 拥塞控制
  6. linux之ps命令详解
  7. chdir、getcwd、mkdir、rmdir函数
  8. 下载丨云和恩墨技术通讯:OCR无法正常读取导致节点宕机
  9. Linux查找历史命令
  10. php is_subclass_of,PHP is_subclass_of函数的一个BUG和解决方法
  11. c++游戏编程 源代码
  12. google hack 语法(渗透测试google黑客语法)
  13. 明光杂感之四:足球与情境觉知(上)
  14. 如何利用后端给的url,前端下载图片、音频、视频等文件
  15. 在 WPF 中使用 Prism DryIOC 加载应用程序时导航到默认视图
  16. foxit 福昕阅读器 点击书签跳转时,保持 页面 缩放比例
  17. HQL语句的基本语法和使用
  18. 如何搭建一个自己的安全测试实验室WebGoat?
  19. 【敏捷研发系列】前端DevOps流水线实践
  20. 【案例30】WebSphere诡异宕机

热门文章

  1. socket websocket
  2. 该Tiled地图制作拿到项目~~这是偷懒,为了直接复制后写来
  3. 做订购系统必须要明白的几点
  4. java中单例模式的3种实现
  5. csu 1008 - Horcrux
  6. [NHibernate] NHibernate对象关系映射工具了解
  7. leetcode 154. 寻找旋转排序数组中的最小值 II(二分查找)
  8. spring—拦截器和异常
  9. spring—事务控制
  10. leetcode329. 矩阵中的最长递增路径(dfs)