python图片相似度比较

面向百度编程,收集的图片比较方法,可用于游戏辅助

#图片相似度比较

#方法零
from PIL import Image

#计算图片hash值
def get_hash(img):
    img = img.resize((16, 16), Image.ANTIALIAS).convert('L')  # 抗锯齿 灰度
    avg = sum(list(img.getdata())) / 256  # 计算像素平均值
    s = ''.join(map(lambda i: '0' if i < avg else '1', img.getdata()))  # 每个像素进行比对,大于avg为1,反之为0
    return ''.join(map(lambda j: '%x' % int(s[j:j+4], 2), range(0, 256, 4)))

#比较图片hash值
def hamming(hash1, hash2, n=20):
    b = False
    assert len(hash1) == len(hash2)
    if sum(ch1 != ch2 for ch1, ch2 in zip(hash1, hash2)) < n:
        b = True
    return b


for i in range(1,10):
    for j in range(1,10):
        im1=Image.open(r'F:\code\python\python\辅助\\'+str(i)+'.png')
        im2=Image.open(r'F:\code\python\python\辅助\\'+str(j)+'.png')
        c=hamming(get_hash(im1), get_hash(im2),5)
        print(str(i)+'-'+str(j)+'-'+str(c))
    print()


'''
#方法一:
import imagehash
from PIL import Image

def diffimg(im1,im2):
    hash_size=20
    hash1 = imagehash.average_hash(im1, hash_size=hash_size)
    hash2 = imagehash.average_hash(im2, hash_size=hash_size)
    a = (1 - (hash1 - hash2) / len(hash1.hash) ** 2)

    return a

for i in range(1,10):
    for j in range(1,10):
        im1=Image.open(r'F:\code\python\python\辅助\\'+str(i)+'.png')
        im2=Image.open(r'F:\code\python\python\辅助\\'+str(j)+'.png')
        c=diffimg(im1, im2)
        print(str(i)+'-'+str(j)+'-'+str(c))
    print()

'''



'''
#方法二:
from PIL import Image
import math
import operator
from functools import reduce

#把图像对象转换为直方图数据,存在list h1、h2 中
def diff(image1,image3):
    h1=image1.histogram()
    h2=image3.histogram()
    result = math.sqrt(reduce(operator.add,  list(map(lambda a,b: (a-b)**2, h1, h2)))/len(h1) )
    return result<10
    #sqrt:计算平方根,reduce函数:前一次调用的结果和sequence的下一个元素传递给operator.add
    #operator.add(x,y)对应表达式:x+y
    #这个函数是方差的数学公式:S^2= ∑(X-Y) ^2 / (n-1)

    #print(result)
    #result的值越大,说明两者的差别越大;如果result=0,则说明两张图一模一样

for i in range(1,10):
    for j in range(1,10):
        im1=Image.open(r'F:\code\python\python\辅助\\'+str(i)+'.png')
        im2=Image.open(r'F:\code\python\python\辅助\\'+str(j)+'.png')
        c=diff(im1, im2)
        print(str(i)+'-'+str(j)+'-'+str(c))
    print()

'''




'''
#方法三:
from PIL import ImageGrab, Image
import numpy as np
import operator

# 汉明距离判断两个图标是否一样
def isMatch(im1, im2):

    # 缩小图标,转成灰度
    image1 = im1.resize((20, 20), Image.ANTIALIAS).convert("L")
    image2 = im2.resize((20, 20), Image.ANTIALIAS).convert("L")

    # 将灰度图标转成01串,即系二进制数据
    pixels1 = list(image1.getdata())
    pixels2 = list(image2.getdata())

    avg1 = sum(pixels1) / len(pixels1)
    avg2 = sum(pixels2) / len(pixels2)
    hash1 = "".join(map(lambda p: "1" if p > avg1 else "0", pixels1))
    hash2 = "".join(map(lambda p: "1" if p > avg2 else "0", pixels2))

    # 统计两个01串不同数字的个数
    match = sum(map(operator.ne, hash1, hash2))
    # 阀值设为10
    return match < 10
for i in range(1,10):
    for j in range(1,10):
        im1=Image.open(r'F:\code\python\python\辅助\\'+str(i)+'.png')
        im2=Image.open(r'F:\code\python\python\辅助\\'+str(j)+'.png')
        c=isMatch(im1, im2)
        print(str(i)+'-'+str(j)+'-'+str(c))
    print()

'''

你可能感兴趣的:(Python,图片比较)