Search code examples
pythonarraysnumpyimagephash

How to convert an imagehash to a numpy array?


I use python / imagehash.phash to search for photos similar to a given one, i.e. photos so that the hamming distance is zero. Now I also would like to detect photos that are flipped or rotated copies - the hamming distance is > 0. Instead of flipping or rotating the photo and then calculating an new pHash I would like to derive the pHash of a rotated / flipped photo directly from the original pHash.

My idea is to convert the hash into an numpy array, use np.flip etc and convert the array back into a pHash.

Questions:

  1. How can I convert the hash into an numpy array and back?
  2. is there a better way to search for rotated / flipped images given the hash of the original photo?

Solution

  • From reading the source code, I found that you can convert between an imagehash.ImageHash object and a binary array using .hash and the imagehash.ImageHash() constructor.

    from PIL import Image
    import imagehash
    
    img = Image.open("house.jpg")
    image_hash_obj = imagehash.phash(img)
    print(image_hash_obj)
    hash_array = image_hash_obj.hash
    print(hash_array)
    hash_array[0, 0] = False
    image_hash_obj2 = imagehash.ImageHash(hash_array)
    print(image_hash_obj2)
    

    Output:

    fde1921f6c20b34c
    [[ True  True  True  True  True  True False  True]
     [ True  True  True False False False False  True]
     [ True False False  True False False  True False]
     [False False False  True  True  True  True  True]
     [False  True  True False  True  True False False]
     [False False  True False False False False False]
     [ True False  True  True False False  True  True]
     [False  True False False  True  True False False]]
    7de1921f6c20b34c