def get_img_hash(image, hash_size = 8):
    """ Grayscale and shrink the image in one step """

    image = image.resize((hash_size + 1, hash_size), Image.ANTIALIAS, )

    pixels = list(image.getdata())
    #print('get_img_hash: pixels=', pixels)

    # Compare adjacent pixels.
    difference = []
    for row in range(hash_size):
        for col in range(hash_size):
            pixel_left = image.getpixel((col, row))
            pixel_right = image.getpixel((col + 1, row))
            difference.append(pixel_left > pixel_right)

    # Convert the binary array to a hexadecimal string.
    decimal_value = 0
    hex_string = []
    for index, value in enumerate(difference):
        if value:
            decimal_value += 2**(index % 8)
        if (index % 8) == 7:
            hex_string.append(hex(decimal_value)[2:].rjust(2, '0'))
            decimal_value = 0

    return ''.join(hex_string)
def get_img_hash ( image , hash_size = 8 ) : image = image . resize ( ( hash_size + 1 , hash_size ) , Image . ANTIALIAS , ) pixels = list ( image . getdata ( ) ) difference = [ ] for row in range ( hash_size ) : for col in range ( hash_size ) : pixel_left = image . getpixel ( ( col , row ) ) pixel_right = image . getpixel ( ( col + 1 , row ) ) difference . append ( pixel_left > pixel_right ) decimal_value = 0 hex_string = [ ] for index , value in enumerate ( difference ) : if value : decimal_value += 2 ** ( index % 8 ) if ( index % 8 ) == 7 : hex_string . append ( hex ( decimal_value ) [ 2 : ] . rjust ( 2 , '0' ) ) decimal_value = 0 return '' . join ( hex_string )
Grayscale and shrink the image in one step