-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathdHash.py
61 lines (42 loc) · 1.23 KB
/
dHash.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
from PIL import Image
from PIL import ImageFilter
from PIL import ImageOps
#This module can classfy the image by dHash
#
#author MashiMaroLjc
#version 2016-2-16
def getCode(img,size):
result = []
# print("x==",size[0])
# print("y==",size[1]-1)
x_size = size[0]-1#width
y_size = size[1] #high
for x in range(0,x_size):
for y in range(0,y_size):
now_value = img.getpixel((x,y))
next_value = img.getpixel((x+1,y))
if next_value < now_value:
result.append(1)
else:
result.append(0)
return result
def compCode(code1,code2):
num = 0
for index in range(0,len(code1)):
if code1[index] != code2[index]:
num+=1
return num
def classfiy_dHash(image1,image2,size=(9,8)):
''' '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 to another image by the dHash.
It's 9 * 8 when it default.
The function will return the hamming code,less is correct.
'''
image1 = image1.resize(size).convert('L')
code1 = getCode(image1, size)
image2 = image2.resize(size).convert('L')
code2 = getCode(image2, size)
assert len(code1) == len(code2),"error"
return compCode(code1, code2)
__all__=[classfiy_dHash]