失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > python比较两张图片是否一样_python实战===用python对比两张图片的不同

python比较两张图片是否一样_python实战===用python对比两张图片的不同

时间:2020-05-30 08:49:36

相关推荐

python比较两张图片是否一样_python实战===用python对比两张图片的不同

PIL importImagefrom PIL importImageChopsdefcompare_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() isNone:#图片间没有任何不同则直接退出

print("【+】We are the same!")else:

diff.save(diff_save_location)exceptValueError 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对比两张图片的不同》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。