python比较两张图片是否一样_python 比较两张图片的区分

方法一

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)

转载 https://www.cnblogs.com/botoo/p/8416315.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值