forked from MashiMaroLjc/Learn-to-identify-similar-images
-
Notifications
You must be signed in to change notification settings - Fork 8
/
aHash.py
62 lines (46 loc) · 1.54 KB
/
aHash.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from PIL import Image
from PIL import ImageFilter
from PIL import ImageOps
#This module can classfy the image by Average Hash Method
#The Hash Method is too strict,so this moudel suitable for finding image by Thumbnail
#
#author MashiMaroLjc
#version 2016-2-17
def getCode(img,size):
pixel = []
for x in range(0,size[0]):
for y in range(0,size[1]):
pixel_value = img.getpixel((x,y))
pixel.append(pixel_value)
avg = sum(pixel)/len(pixel)
cp = []
for px in pixel:
if px > avg:
cp.append(1)
else:
cp.append(0)
return cp
def compCode(code1,code2):
num = 0
for index in range(0,len(code1)):
if code1[index] != code2[index]:
num+=1
return num
def classfiy_aHash(image1,image2,size=(8,8),exact=25):
''' 'image1' and 'image2' is a Image Object.
You can build it by 'Image.open(path)'.
'Size' is parameter what the image will resize to it and then image will be compared by the algorithm.
It's 8 * 8 when it default.
'exact' is parameter for limiting the Hamming code between 'image1' and 'image2',it's 25 when it default.
The result become strict when the exact become less.
This function return the true when the 'image1' and 'image2' are similar.
'''
image1 = image1.resize(size).convert('L').filter(ImageFilter.BLUR)
image1 = ImageOps.equalize(image1)
code1 = getCode(image1, size)
image2 = image2.resize(size).convert('L').filter(ImageFilter.BLUR)
image2 = ImageOps.equalize(image2)
code2 = getCode(image2, size)
assert len(code1) == len(code2),"error"
return compCode(code1, code2)<=exact
__all__=[classfiy_aHash]