ImageHash is a Python library that provides tools for generating perceptual hash values for images. These hashes can be used to compare images based on their visual content, making it useful for finding similar or duplicate images. It supports various hashing algorithms like aHash, pHash, dHash, wHash, colorhash, and crop-resistant hashing. The current version is 4.3.2, and it receives updates periodically to add features, improve performance, and address bugs. [1, 3, 8]
pip install Pillow imagehashVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to load an image using Pillow, generate an average perceptual hash using `imagehash.average_hash`, and compare it to another hash by calculating their Hamming distance. A smaller difference typically indicates greater visual similarity. [1, 3, 8, 14]
To convert hashes from the old encoding to the new format, use the `imagehash.old_hex_to_hash` function.
If you relied on the previous (buggy) vertical difference calculation, use `imagehash.dhash_vertical` which retains the old behavior. Otherwise, `dhash` now works as intended.
Convert NumPy arrays to PIL Image objects before passing them to ImageHash functions using `Image.fromarray(numpy_array)`.
Always verify the `requests` response's status code (`resp.status_code`) and content type before attempting to open it as an image. You might also need to read `resp.content` into a `BytesIO` object for `Image.open()`.
Experiment with different threshold values on your specific dataset to find what best defines 'similar' for your application. There's no one-size-fits-all value.
For robust matching against such transformations, consider preprocessing images to normalize them (e.g., consistent sizing, minor rotations corrected) or use more advanced techniques like crop-resistant hashing if available and suitable for your specific deformation types.