image hash python code example

Example: image hash python

from PIL import Image

def image_average_hash(img):
    hash = []
    for x in range(16): hash.append([0, 0, 0])
    for x2 in range(4):
        for x in range(math.floor(img.size[0] / 4)):
            for y2 in range(4):
                for y in range(math.floor(img.size[1] / 4)):
                    px = img.getpixel((x + x2 * 4, y + y2 * 4))
                    for z in range(3): hash[x2 * 4 + y2][z] += px[z]
    for x in range(16): 
        for y in range(3): hash[x][y] = hash[x][y] / (math.floor(img.size[0] / 4) * math.floor(img.size[1] / 4))
        hash[x] = hex(math.floor(sum(hash[x]) / 3)); hash[x] = hash[x][hash[x].index("x") + 1:]
        if len(hash[x]) != 2: hash[x] = "0" + hash[x]
    return "".join(map(str, hash))
  
image = Image.new("RGB", (100, 100), color = "black")
print(image_average_hash(image))