时间:2021-05-23
from PIL import Imagefrom 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 Imageimport mathimport operatorfrom functools import reducedef 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 resultif __name__ == '__main__': img1 = "./1.png" # 指定图片路径 img2 = "./2.png" result = image_contrast(img1,img2) print(result)如果两张图片完全相等,则返回结果为浮点类型“0.0”,如果不相同则返回结果值越大。
同样用上面两张图片,执行结果为38,还是比较小的:
这样就可以在自动化测试用例中调用该方法来断言执行结果。
关于Pillow库的详细文档:
http://pillow.readthedocs.org/en/latest/index.html
总结
以上所述是小编给大家介绍的用python实现对比两张图片的不同,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例为大家分享了python实现两张图片像素融合的具体代码,供大家参考,具体内容如下通过计算两张图片的颜色直方图特征,利用直方图对图片的颜色进行融合。imp
本文实例为大家分享了python实现两张图片拼接为一张图片并保存的具体代码,供大家参考,具体内容如下这里主要用Python扩展库pillow中Image对象的p
网页中当鼠标放置到热点区,图片颜色自动变换,可以用两张图片切换的方式,实现这一效果,今天我们就来看看MicrosoftOfficeSharePointDesig
本文研究的主要是pythonPIL实现图片合成的相关内容,具体介绍如下,分享实例代码。在项目中需要将两张图片合在一起。遇到两种情况,一种就是两张非透明图片的合成
ps中把两张图融合的步骤: 1、用ps打开两张图片; 2、在图层面板中双击待移动图片人物将背景改为图层,方便移动; 3、使用工具栏中的移动工具将人物图片移