aHash
Also known as: average hash, ahash image, ahash algorithm, average hashing
aHash (average hash) is a perceptual image hashing algorithm that fingerprints a photo by comparing each pixel of a tiny grayscale thumbnail to the image's average brightness. Similar images produce similar hashes, so near-duplicates can be matched even after resizing or recompression.
- Reduces images to an 8x8 grayscale grid and thresholds each pixel against the mean brightness, producing a 64-bit fingerprint.
- Similarity is measured by Hamming distance between hashes — fewer differing bits means more visually similar.
- Fast but sensitive to brightness/gamma changes; pHash is generally more robust for tough edits.
How aHash works
aHash reduces an image to a small fixed grid, typically 8x8 pixels, then converts it to grayscale. This discards color, resolution, and most detail, leaving only the broad structure of light and dark regions.
The algorithm computes the average brightness of all 64 pixels. Each pixel is then compared to that average: pixels brighter than the mean become a 1, darker ones become a 0. Reading those bits in order yields a 64-bit hash.
To compare two images, the hashes are XORed and the number of differing bits is counted. This count is the Hamming distance — a small distance (often a threshold of around 5-10 bits) means the images are visual near-duplicates.
Strengths and limitations
aHash is extremely fast and trivial to implement, which makes it attractive for a first-pass scan over thousands of photos. Because it ignores fine detail, it tolerates resizing, JPEG recompression, and minor color shifts.
Its main weakness is sensitivity to changes in overall brightness, gamma, and contrast, since the threshold is the image's own average. It is also less robust than DCT-based pHash against edits like cropping or large tonal adjustments. In practice aHash is often used as a cheap pre-filter, with a stronger hash applied only to candidate matches.