-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompareImages.py
40 lines (32 loc) · 1.17 KB
/
CompareImages.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
import cv2
# Функция вычисления хэша
def CalcImageHash(FileName):
image = cv2.imread(FileName) # Прочитаем картинку
resized = cv2.resize(image, (8, 8), interpolation=cv2.INTER_AREA) # Уменьшим картинку
gray_image = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY) # Переведем в черно-белый формат
avg = gray_image.mean() # Среднее значение пикселя
ret, threshold_image = cv2.threshold(gray_image, avg, 255, 0) # Бинаризация по порогу
# Рассчитаем хэш
_hash = ""
for x in range(8):
for y in range(8):
val = threshold_image[x, y]
if val == 255:
_hash = _hash + "1"
else:
_hash = _hash + "0"
return _hash
def CompareHash(hash1, hash2):
l = len(hash1)
i = 0
count = 0
while i < l:
if hash1[i] != hash2[i]:
count = count + 1
i = i + 1
return count
def get_hash(file1, file2):
hash1 = CalcImageHash(file1)
hash2 = CalcImageHash(file2)
result = CompareHash(hash1, hash2)
return result