-
Notifications
You must be signed in to change notification settings - Fork 10
/
color_dHash192.py
executable file
·48 lines (35 loc) · 1.21 KB
/
color_dHash192.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
'''
Script for computing the 192-bit Difference Hash of color RGB Images.
Usage: python color_dHash192.py <image path>
'''
from __future__ import print_function
import sys
from PIL import Image
import numpy as np
import imagehash ## https://github.com/JohannesBuchner/imagehash
imagepath = sys.argv[1]
def color_dhash(imagepath):
im = Image.open(imagepath)
npim = np.asarray(im)
imr = Image.fromarray(npim[:,:,0])
img = Image.fromarray(npim[:,:,1])
imb = Image.fromarray(npim[:,:,2])
hashr = bin(int(str(imagehash.dhash(imr)), 16))[2:]
hashg = bin(int(str(imagehash.dhash(img)), 16))[2:]
hashb = bin(int(str(imagehash.dhash(imb)), 16))[2:]
gapr = 64 - len(hashr)
gapg = 64 - len(hashg)
gapb = 64 - len(hashb)
hashrf = ''.join(['0' for i in range(gapr)])
hashgf = ''.join(['0' for i in range(gapg)])
hashbf = ''.join(['0' for i in range(gapb)])
hashrf += hashr
hashgf += hashg
hashbf += hashb
im.close()
return str(hex(int(hashrf + hashgf + hashbf, 2)).split('0x')[1].split('L')[0])
if __name__ == '__main__':
if len(imagepath) != 0:
print(color_dhash(imagepath), imagepath)
else:
print('Bad path.')