image hash python

Solutions on MaxInterview for image hash python by the best coders in the world

showing results for - "image hash python"
Blanche
24 Apr 2017
1from PIL import Image
2
3def image_average_hash(img):
4    hash = []
5    for x in range(16): hash.append([0, 0, 0])
6    for x2 in range(4):
7        for x in range(math.floor(img.size[0] / 4)):
8            for y2 in range(4):
9                for y in range(math.floor(img.size[1] / 4)):
10                    px = img.getpixel((x + x2 * 4, y + y2 * 4))
11                    for z in range(3): hash[x2 * 4 + y2][z] += px[z]
12    for x in range(16): 
13        for y in range(3): hash[x][y] = hash[x][y] / (math.floor(img.size[0] / 4) * math.floor(img.size[1] / 4))
14        hash[x] = hex(math.floor(sum(hash[x]) / 3)); hash[x] = hash[x][hash[x].index("x") + 1:]
15        if len(hash[x]) != 2: hash[x] = "0" + hash[x]
16    return "".join(map(str, hash))
17  
18image = Image.new("RGB", (100, 100), color = "black")
19print(image_average_hash(image))