-
Notifications
You must be signed in to change notification settings - Fork 0
/
dhash.py
37 lines (30 loc) · 987 Bytes
/
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
from PIL import Image, ImageOps
import sys
import imagehash
img_name = 'img.jpg'
if len(sys.argv) > 1:
img_name = sys.argv[1]
img = Image.open(img_name)
# is there alpha channel?
if img.mode in ('RGBA', 'LA') or (img.mode == 'P' and 'transparency' in img.info):
if img.mode == 'P':
img = img.convert('RGBA')
if img.mode == 'LA':
img = img.convert('RGBA')
# average grayscale in the image
grayscale = img.convert('L')
avg_gray = int(sum(grayscale.getdata()) / len(grayscale.getdata()))
data = img.load()
# set white or black for transparent pixels
for i in range(img.size[0]):
for j in range(img.size[1]):
if data[i, j][3] < 32:
data[i, j] = (255 - avg_gray, 255 - avg_gray, 255 - avg_gray, 255)
else:
data[i, j] = (data[i, j][0], data[i, j][1], data[i, j][2], 255)
img = img.convert('RGB')
# img.save("new_" + img_name)
else:
img = img.convert('RGB')
# img.save("new_" + img_name)
print(imagehash.dhash(img))