JohannesBuchner / imagehash

A Python Perceptual Image Hashing Module
BSD 2-Clause "Simplified" License
3.28k stars 331 forks source link

imagehash.hex_to_hash() does not return original imagehash object. #154

Closed GitTimmerd closed 2 years ago

GitTimmerd commented 2 years ago

Hi,

I started with an imagehash h = array([[False, False, False], [ True, True, False], [False, False, False], [False, False, False], [False, False, False], [False, False, False], [False, True, False], [False, False, False], [False, False, False], [False, False, False], [False, False, False], [False, False, False], [False, True, False], [False, False, False]])

When I convert this to a string and back I get: hsh = imagehash.hex_to_hash(str(h)) This gives a warning as well, and returns the following: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray. hash_array = numpy.array([[bool(int(d)) for d in row] for row in bit_rows]) array([list([True, True, False, False, False, False]), list([False, False, False, False, False, False]), list([False, False, False, False, True, False]), list([False, False, False, False, False, False]), list([False, False, False, False, False, False]), list([False, False, False, False, True, False]), list([False, False, False])], dtype=object)

Comparing them gives: TypeError: ('ImageHashes must be of the same shape.', (7,), (14, 3)) I am using imagehash 4.2.1.

Does anyone know how to get the original imagehash back after you have converted this to string? Could the hex_to_hash function be adjusted so that this is the case?

JohannesBuchner commented 2 years ago

str(h)

Don't just use numpy.__str__, you have to use the one implemented in ImageHash. It makes a hex string using _binary_array_to_hex(h).

Have a look here https://github.com/JohannesBuchner/imagehash/blob/master/imagehash.py#L92

GitTimmerd commented 2 years ago

Thanks, this works for the regular hashes ! However, I still get the issue when I try to perform these modifications on a colorhash. Do you know what kind of solution is there to that?

JohannesBuchner commented 2 years ago

colorhashes are different, because they are not a image. you have to use hex_to_flathash to restore IIRC.

JohannesBuchner commented 2 years ago

This is now documented in the README.