Perceptual Image Hash
A cryptographic hash tells you whether two files are byte-identical, which is almost never the question you have about images — re-saving a JPEG changes every byte while changing nothing you can see. A perceptual hash summarises what the picture looks like, so two versions of the same photograph at different sizes and quality settings produce nearly the same value.
Drop images here
Drop two or more to get the distance between them
The three algorithms, and which to trust
aHash shrinks the image to 8×8 grey, takes the mean, and records whether each of the 64 cells is above it. It is the simplest and the weakest: any change in overall brightness moves the mean and flips bits all over the hash, so a brightened copy can look like a different image.
dHash shrinks to 9×8 and records, for each of the 64 adjacent horizontal pairs, whether the left cell is darker than the right. Because it stores relationships rather than absolute levels, it survives brightness and contrast changes almost completely. It is fast and a good default.
pHash works at 32×32, runs a discrete cosine transform, keeps the top-left 8×8 block of low-frequency coefficients (discarding the DC term, which is just overall brightness) and compares each to the median. Working in the frequency domain makes it the most robust of the three — it tolerates gamma shifts, mild cropping and heavy re-compression — and the most expensive to compute. When two hashes disagree about whether something is a duplicate, believe pHash.
Reading the Hamming distance
With more than one file you also get the number of differing bits between each pair, out of 64. Zero means the hashes are identical, which for a 64-bit perceptual hash means the same image at any size or quality. Up to about 10 bits is the usual "same picture" range — a resize, a re-encode, a small watermark, a slight crop. Between 10 and 20 is ambiguous and worth looking at by eye. Over 20 means different images; two unrelated photographs typically land around 32, which is what pure chance predicts.
The one thing perceptual hashes are bad at is rotation and mirroring. Flip an image horizontally and every one of these will report a large distance, because the spatial relationships they encode are direction-dependent. If you need mirror-invariant matching, hash the image and its flip and compare both.
FAQ
Why is the hash 16 hex characters?
64 bits, four bits per hex digit. That is the standard size for these algorithms and it is what libraries like ImageHash and pHash emit, so the values are directly comparable with hashes computed elsewhere — as long as the grey conversion matches, which is the usual source of small discrepancies between implementations.
Should I use this for security?
No. Perceptual hashes are designed to collide for similar inputs, which is the opposite of what a security hash needs, and they are straightforward to attack deliberately. Use SHA-256 for integrity and these for similarity.
I just want to find duplicates in a folder.
Then Find duplicates is the tool — it does the hashing and the grouping for you and reports clusters. This page is for when you want the hash values themselves, to store in a database or compare against a set you already have.
Why is pHash 63 bits plus a pad?
Because the DC coefficient is excluded, leaving 63 comparisons in the 8×8 block. A zero bit is appended so the value still fits the conventional 64-bit, 16-character form. Implementations differ slightly here — some keep the DC term — which is worth knowing if you are comparing against hashes from another tool.
Does the file format affect the hash?
Only through the pixels. A PNG and a high-quality JPEG of the same image give hashes a bit or two apart at most. A very low-quality JPEG changes enough pixels to move it further, which is exactly the sensitivity you want.