Appending hashes to a list for reference

167 Views Asked by At

I'm using the imagehash library in Python and i'm trying to compare one image to a set of images to determine if it's similar to any of them.

To avoid having to fetch the hashes every time I run the program I generated each hash and appended it to a list (one time operation) however when running the program to actually compare, it appears that the Hash is it's own type of value and thus can't be easily subtracted/added (Mainly because generating the hashes and adding to a list gives me a HUGE list of True/Falses instead of a clean alphanumeric value like 003c7e7e7e7e0000

Kindly ignore syntax errors as I have edited the actual code for posting this question

Basically, can I convert a string to an imagehash? If not, any recommendations?

Below is a simplified version of the code i'm using

Part 1 (Fetching the hashes)

import imagehash
import os
from PIL import Image

def hashit(a):
    return((imagehash.average_hash(Image.open(a))))

directory=r"C:\Users\65903\Desktop\Images"
hashes=[]
for filename in os.listdir(directory):
    f = os.path.join(directory, filename)
    # checking if it is a file
    if os.path.isfile(f):
        has=hashit(f)
        hashes.append(has)

The above generates a ~40 line list of hashes of True/Falses (which isn't really practical to have in my case)

Part 2 (Comparing with a list of hashes)

from PIL import Image
import imagehash
import os
import time

def hashit(a):
    return((imagehash.average_hash(Image.open(a))))

def ifinlist(p):
    hashes=['003c7e7e7e7e0000','f8f8d80878f8f801','fad20000ff7f7f0f','f0000028e0f0f7ff','f8f7e8089d3208dd']
    for i in hashes:
        if abs(i-p)<25:
            return("Similar image found")

print(ifinlist(hashit(r"c/users/filenamestufftestidk")))

The above gives an error that imagehash and string cannot be subtracted (which I know to be true)

1

There are 1 best solutions below

1
On

The hash value returned by the average_hash method can be converted to a hex value using the str builtin. E.g. str(hashit(img)).

To reverse this (for Part 2) you can use the function imagehash.hex_to_hash.

from PIL import Image
import imagehash
import os
import time

def hashit(a):
    return((imagehash.average_hash(Image.open(a))))

def ifinlist(p):
    hashes=['003c7e7e7e7e0000','f8f8d80878f8f801','fad20000ff7f7f0f','f0000028e0f0f7ff','f8f7e8089d3208dd']
    for i in hashes:
        if abs(imagehash.hex_to_hash(i) - p) < 25:
            return("Similar image found")

print(ifinlist(hashit(r"c/users/filenamestufftestidk")))