Python calculates the similarity of pictures

Python calculates the similarity of pictures


PIL is needed here, if PIL is not installed, you need to pip install pillow first

Calculation method

1. Scale the picture to 10×10 (the scaling ratio varies with the size of the picture)
2. Read the gray- scaled pixels of each point
3. Calculate the average pixel value of each row
4. Generate a feature sequence. The pixel at each point is compared with the average value of the pixel in the row. If it is greater than the average value of the pixel, the feature sequence is +'1', otherwise +'0'. The final feature sequence is a string composed of 1 and 0 (for example: 11001101101111001).
5. Comparing the feature sequences of two pictures, the more the same number of bits, the higher the picture similarity. We can define the similarity as: similarity = the same number of bits in the feature sequence ➗ feature sequence length

Complete code

from PIL import Image

def hash_img(img):#计算图片的特征序列
    a=[]#存储图片的像素
    hash_img=''#特征序列
    width,height=10,10#图片缩放大小
    img=img.resize((width,height))#图片缩放为width×height
    for y in range(img.height):
        b=[]
        for x in range(img.width):
            pos=x,y
            color_array = img.getpixel(pos)#获得像素
            color=sum(color_array)/3#灰度化
            b.append(int(color))
        a.append(b)
    for y in range(img.height):
        avg=sum(a[y])/len(a[y])#计算每一行的像素平均值
        for x in range(img.width):
            if a[y][x]>=avg:#生成特征序列,如果此点像素大于平均值则为1,反之为0
                hash_img+='1'
            else:
                hash_img+='0'
                
    return hash_img
    
def similar(img1,img2):#求相似度
    hash1=hash_img(img1)#计算img1的特征序列
    hash2=hash_img(img2)#计算img2的特征序列
    differnce=0
    for i in range(len(hash1)):
        differnce+=abs(int(hash1[i])-int(hash2[i]))
    similar=1-(differnce/len(hash1))
    return similar

img1=Image.open('1.png')
img2=Image.open('2.png')
print('%.1f%%' % (similar(img1,img2) * 100))

test

1.png
1.png
2.png
2.png
3.png
3.png
4.png
4.png

img1=Image.open('1.png')
img2=Image.open('2.png')
print('%.1f%%' % (similar(img1,img2) * 100))
img3=Image.open('3.png')
img4=Image.open('4.png')
print('%.1f%%' % (similar(img3,img4) * 100))

Output result:

61.0%
89.0%

The similarity between 1.png and
2.png is 61% 3. The similarity between png and 4.png is 89%

The above code is feasible in python 3.7, if there is any error, please correct me.

Guess you like

Origin blog.csdn.net/nejssd/article/details/90291116