Pillow实现图片对比

来源:互联网 发布:java web记录日志 编辑:程序博客网 时间:2024/04/28 12:48
# coding=utf-8
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 = "./img1.jpg"  # 指定图片路径
    img2 = "./img2.jpg"
    result = image_contrast(img1,img2)
    print(result)

 

  如果两张图片完全相等,则返回结果为浮点类型“0.0”,如果不相同则返回结果值越大。

0 0