Skip to content
Image similarity

Perceptual image hashing and compare by Hamming distance

Compute the perceptual hash of an image with the three classic algorithms, aHash (average), dHash (gradient) and pHash (discrete cosine transform), and compare two images via the Hamming distance between their hashes. Unlike MD5 or SHA, which change completely on the slightest edit, a perceptual hash stays similar when the image is similar: handy to find duplicates, resized or recompressed variants. Images are processed in the browser and never leave the device.

Image A: drop or click
JPG, PNG, WebP, GIF. The image stays in the browser.

How to compute and compare perceptual hashes

  1. 1

    Pick the mode

    Hash one image returns aHash, dHash and pHash of a single file. Compare two images computes both hashes and the distance between them.

  2. 2

    Load the images

    Drop the files into the drop zones or click to select them. JPG, PNG, WebP and GIF all work. Images are downscaled and converted to greyscale in memory only, in the browser.

  3. 3

    Compute

    You get the hashes as 16-character hex strings (64 bits each). In compare mode you also see the Hamming distance, i.e. how many bits differ, and a readable verdict.

  4. 4

    Interpret

    Distance 0 means perceptually identical; low values mean the same image resized, recompressed or lightly edited; high values mean different images. Copy the hashes to use them elsewhere.

How the three algorithms work

All three shrink the image to a small greyscale grid and derive 64 bits from it, but with different criteria. aHash (average hash) resizes to 8x8 pixels, computes the mean brightness and sets to 1 every pixel above the mean. It is very fast but weak against brightness changes. dHash (difference hash) works on a 9x8 grid and compares each pixel with its right neighbour: the bit says whether it gets brighter or darker. It captures gradient structure and is very stable against resizing and compression.

pHash (perceptual hash) is the most robust. It resizes to 32x32, applies the two-dimensional discrete cosine transform (DCT) and keeps only the top-left 8x8 block, where the low frequencies, i.e. the perceptible structure of the image, concentrate. It excludes the DC coefficient, computes the median of the rest and sets to 1 the coefficients above it. Because it uses low frequencies, it ignores fine detail and noise: two versions of the same photo at different qualities produce nearly identical pHashes.

Comparing two hashes uses the Hamming distance: the number of bits in different positions. Zero means identical hashes; a few differing bits mean very similar images. For the verdict this tool uses the pHash, the most reliable one, with practical thresholds: under 5 almost always the same image, over 10 different images. All the computation, resizing included, happens in the browser via canvas.

Glossary

Technical terms used on this page, briefly explained.

Perceptual hash #
A fingerprint that reflects the visual content of an image, not its bytes. Similar images produce similar hashes, unlike cryptographic hashes (MD5, SHA) where one differing bit changes everything.
aHash #
Average hash. Resizes to 8x8, computes the mean and marks 1 the pixels above it. Simple and fast, but sensitive to global brightness changes.
dHash #
Difference hash. On a 9x8 grid it compares each pixel with its neighbour: the bit indicates whether brightness rises or falls. Robust to resizing and compression.
pHash #
Perceptual hash based on the DCT. It keeps the low frequencies (perceptible structure) and ignores fine detail. It is the most robust to recompression and light edits.
DCT #
Discrete Cosine Transform: decomposes an image into its spatial frequencies. Low frequencies (top-left corner) describe the overall shape, high ones the detail. It is the same transform behind JPEG.
Hamming distance #
The number of positions at which two equal-length bit strings differ. Here it measures how different two 64-bit perceptual hashes are: 0 = identical.

Perceptual hashing FAQ

Why a perceptual hash instead of MD5 or SHA?
Cryptographic hashes like MD5 and SHA change completely if even one byte differs: useful to verify integrity, useless to find similar images. A perceptual hash instead stays similar when the image is similar, so it serves to find duplicates, resized, recompressed or lightly edited variants.
Which of the three algorithms is best?
It depends on the use. dHash and pHash are more robust than aHash. The pHash, based on the DCT, is generally the most reliable against recompression and quality changes, which is why this tool uses it for the verdict. dHash is a great trade-off between robustness and speed. aHash is the simplest and fastest but the least stable.
What distance threshold means two images are the same?
With 64-bit hashes, a Hamming distance of 0 means perceptually identical; values up to 4-5 almost always mean the same image edited, resized or recompressed; over 10 usually means different images. Thresholds are not absolute: they depend on the kind of images and should be tuned to your case.
Does it detect rotated or mirrored images?
No, not natively. Classic perceptual hashes are not invariant to rotation or flipping: the same photo rotated 90 degrees produces very different hashes. Handling rotations requires extra techniques (hashing all rotations, or invariant features). This tool compares images as they are.
Are the images uploaded anywhere?
No. They are read and resized in browser memory only, via canvas. No file is sent to a server. You can verify it in the Network tab of the developer tools, or use the tool offline after loading the page.
Can I compare an image against a hash computed elsewhere?
If the hash was computed with the same algorithm and parameters (for pHash: 32x32, 8x8 block, DC excluded, median threshold), the Hamming distance is comparable. Different implementations may use slightly different conventions, so always compare hashes produced by the same method.
Does it work with very large images?
Yes. The first thing the tool does is shrink the image to a small grid, so the original size has little effect on the result and the compute time. Very heavy files just take a moment longer for the browser to decode.

Who builds these tools?

Maurizio Fonte, senior IT consultant with 20+ years in PHP, Laravel, unmanaged Linux infrastructure, applied cybersecurity and AI/LLM integration. Production backends, legacy code modernization, security audits, custom AI agents and MCP servers: the work behind every tool published here.

About Maurizio Fonte