python判断两张照片一样

def test_one():
	"""
	opencv的subtract方法
	"""
    file1 = r"./1.bmp"
    file2 = r"./2.bmp"

    image1 = cv2.imread(file1)
    image2 = cv2.imread(file2)


    # image1 = cv2.imdecode(np.fromfile(file1, dtype=np.uint8), -1)   # 在路径中有中文时,opencv不识别中文
    # image2 = cv2.imdecode(np.fromfile(file2, dtype=np.uint8), -1)

    difference = cv2.subtract(image1, image2)
    print(difference)
    result = not np.any(difference)  # if difference is all zeros it will return False

    if result is True:
        print("两张图片一样")
    else:
        cv2.imwrite("result.jpg", difference)
        print("两张图片不一样")
from PIL import Image
import math
import operator
from functools import reduce

def test_two(img1, img2):
	"""
	使用PIL的Image,然后使用数学方法计算
	"""
    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))
    # result是一个double类型的数据,该值越小,则说明两者内容越一致,根据实际情况,确定是否一致,如果极为严格,则result为0时,两者一致
    if result < 500:
        print('两张图片一样')
    else:
        print('两张图片不一样')

from PIL import Image
def test_three(image1_path, image2_path):
	"""
	使用PIL的Image,比较所有的像素,该方法要求图片大小一致
	"""
    # 打开图片
    image1 = Image.open(image1_path)
    image2 = Image.open(image2_path)

    # 获取图片的宽度和高度
    width, height = image1.size

    # 获取图片的像素值
    pixels1 = image1.load()
    pixels2 = image2.load()

    # 比较所有像素值是否相同
    for x in range(width):
        for y in range(height):
            if pixels1[x, y] != pixels2[x, y]:
                return False

    # 所有像素值都相同,返回True
    return True
  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值