You are in my Digital Garden.
Perceptual Hashing
Last modified: Sep 11th, 2026
A perceptual hash is a fingerprint of a multimedia file derived from various features from its content. Unlike cryptographic hash functions which rely on the avalanche effect of small changes in input leading to drastic changes in the output, perceptual hashes are “close” to one another if the features are similar.
This document focuses on image hashing.
Algorithms
-
pHash library
- GPLv3 licensed
- Seems to contain a variety of algorithms
- Average Hash or aHash
- DifferenceHash or dHash
- pHash - DCT-based algorithm, one of several used by the pHash library above
- Wavelet hash
-
PDQ by Facebook
- This is probably the best on this page
- They also made a large video hash (VPDQ) based on this
Comparisons
- Evaluating Perceptual Image Hashes at OkCupid - no one clear winner, worth reading
- aHash vs dHash vs pHash - pHash is most accurate, dHash wins for a balance between accuracy and speed
- Testing different image hash functions - pHash wins but it’s unclear why to me
Software
- PDQ infrastructure from Meta
- Library: python-threatexchange
- Service: Hasher Matcher Actioner (HMA)
- Vector/similarity search library: faiss from Meta
- This is used to efficiently query for similar image hashes
- Since regular databases can’t do searches by Hamming distance
- Spotify also has a library for this called Annoy, although it is immutable, see comparison
- The best option might be pgvector, a generic Postgres extension for vector databases that supports Hamming distance