How Image Hashing Works

A visual guide to perceptual image hashing using the average hash method.

What is Image Hashing?

Image hashing (also called perceptual hashing) creates a compact fingerprint of an image based on its visual content.

Unlike cryptographic hashes, similar images produce similar hashes, making it perfect for finding duplicates or near-duplicates.

We'll focus on average_hash - the simplest method to understand.

Image
...

Start with an Image

Any image will work. The algorithm reduces it to its essential visual structure.

Let's use this photo and walk through each transformation step by step.

Convert to Grayscale

First, we reduce complexity by converting to grayscale (luminance).

Each pixel changes from three values (R, G, B) to a single brightness value.

This removes color information, focusing only on light and dark patterns.

Resize to 8×8

Next, we drastically shrink the image to just 8×8 pixels - only 64 values total.

This removes fine details and keeps only the basic structure.

Each pixel now represents the average brightness of a larger region.

Calculating...

Measure Luminance Values

Each of the 64 pixels has a luminance value from 0 (black) to 255 (white).

We record all 64 values and calculate their average.

This average will be our threshold for the next step.

0
255
Calculating...

Convert to Bits

Now we compare each pixel's luminance to the average:

  • Above or equal to average? Set bit to 1
  • Below average? Set bit to 0

This gives us 64 bits of data - a compact fingerprint of the image!

Converting...

The Final Hash

Those 64 binary bits can be expressed as a 16-character hexadecimal (base 16) string.

Every 4 bits becomes one hex character: convert the binary nibble to base 10, then map that value (0-9, A-F) to a single base 16 digit.

Nerd note: a 4-bit nibble ranges from 0000 to 1111 - 15 in base 10, giving 16 possible values (0-15). That's exactly what one base 16 digit (0-F) can express, which is why two hex digits line up perfectly with one byte (8 bits, 256 values).

This compact representation captures the image's visual essence and can be quickly compared to other hashes.

Binary Base 10 Base 16
Ready...
Computing...

Other Hashing Methods

Average hash is just one approach. There are several other methods, each using a different technique to generate the 64 bits:

  • pHash (Perceptual Hash): Uses Discrete Cosine Transform (DCT) to find frequency patterns
  • dHash (Difference Hash): Compares each pixel to its neighbor (horizontal or vertical)
  • wHash (Wavelet Hash): Uses wavelet transforms for more robust comparisons

All methods follow the same pattern: transform → measure → threshold → 64 bits

Average Hash
Grayscale → 8×8 → Compare to average
pHash
Grayscale → DCT → Low frequencies → Threshold
dHash
Grayscale → 9×8 → Compare adjacent pixels
wHash
Grayscale → Wavelet transform → Threshold

Hamming Distance

To compare two images, we calculate the Hamming distance between their hashes.

Hamming distance counts how many bits differ between two binary numbers.

Lower distance = more similar images. A distance of 0 means identical (or nearly identical) images.

Hamming Distance: 4

Storing & Querying Hashes

Since our hashes are 64-bit numbers, we can store them efficiently and perform fast similarity searches.

To find similar images:

  • Store hashes as BIGINT values in a database
  • Use bitwise XOR to calculate Hamming distance
  • Count the number of 1s in the result (popcount)

This allows queries like: "Find all images with Hamming distance ≤ 5 from this image"

SELECT
image_id,
hash,
BIT_COUNT(hash XOR 0x8000001898dcfedf)
AS distance
FROM
images
WHERE
BIT_COUNT(hash XOR 0x8000001898dcfedf) <= 5
ORDER BY
distance;
Target: 0x8000001898dcfedf
Image 1: 0x8000001898dcfedb
XOR result: 0x0000000000000004
Distance: 1 bit differs ✓
Modern databases can index and query these efficiently, making it possible to search millions of images in milliseconds.