Introduction
Cryptographic hashes like SHA-256 produce completely different outputs for images that differ by a single pixel. Perceptual hashes are different: they produce similar outputs for visually similar images, allowing you to detect duplicates, near-duplicates, and manipulated copies. This tool computes three perceptual hashes (aHash, dHash, pHash) for two images and compares them using Hamming distance. Upload two images and the tool shows the hash values, visual hash grids, and a similarity percentage. All processing happens in your browser.
What this tool does
- Compute aHash (average hash): resize to 8x8 grayscale, threshold at the mean to produce a 64-bit hash
- Compute dHash (difference hash): resize to 9x8 grayscale, compare adjacent pixels to produce a 64-bit hash capturing gradient information
- Compute pHash (perceptual hash): resize to 32x32 grayscale, apply a 2D Discrete Cosine Transform (DCT), take the top-left 8x8 coefficients (excluding DC), threshold at the median to produce a 64-bit hash
- Compare two images by computing the Hamming distance (number of differing bits) between each hash type, and display the similarity percentage as (64 - distance) / 64 * 100
- Render visual hash grids (8x8 black-and-white squares) for each hash type, plus diff grids highlighting differing bits in red
- Display hash values as 16-character hex strings for easy copying and comparison
How this tool works
Each image is loaded into an HTML canvas at a reduced size, and the pixel data is converted to grayscale using the luminance formula `0.299*R + 0.587*G + 0.114*B` (ITU-R BT.601).
For aHash, the image is drawn at 8x8 pixels. The mean of all 64 grayscale values is computed. Each pixel is compared to the mean: if the value is greater than or equal to the mean, the bit is 1; otherwise 0. This produces a 64-bit hash that captures the overall brightness pattern.
For dHash, the image is drawn at 9x8 pixels. For each row, adjacent columns are compared: if the left pixel is darker than the right pixel, the bit is 1. This produces 8 comparisons per row times 8 rows = 64 bits. dHash captures the gradient (direction of brightness change) rather than absolute brightness, making it more robust to brightness and contrast adjustments.
For pHash, the image is drawn at 32x32 pixels. A 2D Discrete Cosine Transform is applied: first a 1D DCT to each row, then a 1D DCT to each column. The top-left 8x8 coefficients (lowest frequencies) are extracted. The DC term (coefficient at position 0,0) is excluded from the median calculation. The remaining 63 coefficients are sorted and the median is found. Each of the 64 coefficients is compared to the median: above median is 1, below is 0. pHash captures the low-frequency structure of the image, making it the most robust of the three against scaling, rotation, and compression artifacts.
The Hamming distance between two hashes is the number of bit positions where they differ. A distance of 0 means the hashes are identical. A distance of 10 or less typically indicates visually similar images. The similarity percentage is `(64 - distance) / 64 * 100`.
How perceptual image hashing works
Perceptual hashing was popularized by Dr. Neal Krawetz in his 2011 blog post 'Looks Like It' on The Hacker Factor blog, which explained aHash, dHash, and pHash in accessible terms. Krawetz demonstrated how these simple algorithms could detect duplicate and near-duplicate images even after resizing, compression, or minor editing.
The pHash algorithm using the Discrete Cosine Transform was described in detail by Christoph Zauner in his 2010 bachelor's thesis 'Implementation and Benchmarking of Perceptual Image Hash Functions' (University of Innsbruck). Zauner benchmarked several perceptual hashing algorithms and found that DCT-based pHash provides the best balance between accuracy and computational cost for general-purpose image matching.
The Discrete Cosine Transform (DCT) is the same transform used in JPEG compression. It converts spatial data (pixel values) into frequency data (coefficients representing how quickly the image changes). The top-left coefficients represent low frequencies (slow changes, overall structure), while the bottom-right coefficients represent high frequencies (rapid changes, fine detail). By keeping only the low-frequency coefficients, pHash captures the structural essence of the image while discarding detail that changes with compression or minor edits.
The Hamming distance is named after Richard Hamming, who introduced it in his 1950 paper 'Error Detecting and Error Correcting Codes' (Bell System Technical Journal). It counts the number of positions at which two binary strings differ. For 64-bit perceptual hashes, a Hamming distance of 0 means identical, 1-5 means nearly identical, 6-10 means similar, and above 10 means likely different.
Perceptual hashes are used in content moderation (detecting known illegal images even after re-encoding), copyright enforcement (finding unauthorized copies), and duplicate detection (de-duplicating photo libraries). Unlike cryptographic hashes, they tolerate minor modifications while still distinguishing visually different images.
For viewing image metadata, see the EXIF Metadata Viewer. For audio analysis, see the Spectrogram Viewer. For cryptographic hashing of text data, see the Hash Generator.
How to use this tool
- Upload Image A by clicking the first file input (supports PNG, JPEG, WebP, BMP, GIF, up to 20 MB)
- Upload Image B by clicking the second file input
- The tool computes aHash, dHash, and pHash for both images and displays them as hex strings and visual 8x8 grids
- Review the comparison section: Hamming distance and similarity percentage for each hash type, plus an average similarity across all three
- Check the diff grids (red squares indicate differing bits) to see where the hashes diverge
- Interpret the results: 100% similarity means identical hashes, 85%+ typically means visually similar, below 70% means likely different images
Real-world examples
Detecting a resized copy of an image
Upload an original 4000x3000 photo as Image A and a 800x600 resized version as Image B. All three hashes should match with Hamming distance 0 or 1, giving 98-100% similarity. This demonstrates that perceptual hashes are invariant to resizing, unlike cryptographic hashes which would produce completely different outputs.
Comparing an original with a JPEG-compressed version
Upload a PNG original and a heavily JPEG-compressed version of the same image. pHash should show high similarity (90%+) because the DCT-based hash captures low-frequency structure that survives JPEG compression. aHash may show slightly lower similarity because it is more sensitive to brightness changes caused by compression artifacts.
Distinguishing two different photos of the same subject
Upload two different photos of the same building taken from different angles. The similarity should be moderate (60-80%) because the overall structure is similar but the details differ. This shows that perceptual hashes capture structural similarity without being fooled by subject matter alone.
Verifying image integrity with pHash
Upload an image and a version with a small watermark added. pHash should still show high similarity (85%+) because the watermark affects high frequencies that pHash discards. aHash and dHash may show slightly lower similarity. This demonstrates pHash's robustness to minor modifications.
Comparison with similar methods
| Method | Complexity | Typical use |
|---|---|---|
| aHash (average hash) | 8x8 grayscale, mean threshold, O(64) | Fast duplicate detection, simple comparisons |
| dHash (difference hash) | 9x8 grayscale, gradient comparison, O(64) | Robust to brightness/contrast changes |
| pHash (perceptual hash) | 32x32 grayscale, 2D DCT, O(N^2) | Most robust, handles scaling and compression |
| SHA-256 (cryptographic) | O(n) over all bytes, avalanche effect | Exact file integrity, not visual similarity |
| SSIM (structural similarity) | O(n) with windowed computation | Quality assessment, perceptual quality metrics |
Limitations or considerations
Perceptual hashes are not cryptographic. They are designed for visual similarity detection, not integrity verification. A determined attacker can create an image that looks different but has the same perceptual hash (a hash collision attack). For integrity verification, use a cryptographic hash like SHA-256 via the Hash Generator.
The tool computes hashes from the canvas-rendered version of the image, which means the hash depends on the browser's image decoding and resizing implementation. Different browsers may produce slightly different hashes for the same image due to differences in image scaling algorithms. For consistent results across platforms, use a server-side implementation with a fixed image library.
pHash uses a 32x32 DCT, which is smaller than the 32x32 or 64x64 used in some academic implementations. This limits the frequency resolution but keeps computation fast in the browser. For research-grade pHash, use a library like `imagehash` (Python) with larger DCT sizes.
The tool does not handle rotation. Perceptual hashes are not rotation-invariant: a 90-degree rotated image will have a completely different hash. For rotation-invariant matching, additional preprocessing (e.g. computing hashes at multiple orientations) is needed.
The Hamming distance threshold for 'similar' depends on the use case. For strict duplicate detection, use a threshold of 5 or less. For near-duplicate detection (resized, recompressed), 10 or less is typical. For content-based image retrieval, higher thresholds may be appropriate.
The maximum file size is 20 MB per image. Larger files may cause the browser to run out of memory during canvas rendering.
Frequently asked questions
What is the difference between a perceptual hash and a cryptographic hash?
A cryptographic hash (like SHA-256) produces completely different outputs for inputs that differ by even one bit (the avalanche effect). A perceptual hash produces similar outputs for visually similar images. Cryptographic hashes verify exact file integrity; perceptual hashes detect visual similarity. They serve different purposes and are not interchangeable.
Which perceptual hash should I use?
pHash is the most robust, handling scaling, compression, and minor edits well due to its DCT-based approach. dHash is a good middle ground, robust to brightness and contrast changes. aHash is the simplest and fastest but the most sensitive to modifications. For most use cases, pHash is recommended.
What Hamming distance indicates a duplicate?
A distance of 0 means the hashes are identical (exact duplicate or very minor difference). 1-5 indicates a near-duplicate (resized or recompressed). 6-10 indicates visually similar images. Above 10, the images are likely different. These thresholds are guidelines and should be tuned for your specific use case.
Are perceptual hashes rotation-invariant?
No. The hashes computed by this tool are not rotation-invariant. A 90-degree rotated image will have a completely different hash because the spatial structure changes. For rotation-invariant matching, you would need to compute hashes at multiple orientations or use a rotation-invariant feature descriptor like ORB or SIFT.
Can perceptual hashes be used for content moderation?
Yes. Perceptual hashes are widely used in content moderation to detect known illegal images even after re-encoding, resizing, or minor editing. Organizations like the Internet Watch Foundation and Meta use perceptual hashing to identify and remove known illegal content. However, perceptual hashes can be evaded by significant image modifications.
Conclusion
Perceptual hashing provides a practical way to detect visually similar images, complementing cryptographic hashes that only detect exact matches. This tool computes aHash, dHash, and pHash with Hamming distance comparison and visual diff grids. For viewing image metadata, see the EXIF Metadata Viewer. For audio analysis, see the Spectrogram Viewer. For cryptographic hashing of text data, use the Hash Generator.