Fraudsters often try to create multiple accounts by submitting the same ID card photo, tweaking it with simple edits like resizing or compression to slip past basic checks. Standard file hashes such as SHA-256 miss these near-duplicates because any small change scrambles the hash completely. Perceptual hashing takes a different approach by examining the image's visual content, making it possible to catch images that look almost the same even if their data differs.
Understanding Perceptual Hashing and dHash
Perceptual hashes create a compact fingerprint of an image based on how it looks. The dHash algorithm works by converting the image to grayscale, shrinking it to a 9×8 pixel grid, and comparing each pixel to its neighbor on the right to see which is brighter. This produces a 64-bit hash that captures the image's structure, not its raw data. Images with only slight visual changes will have hashes that differ by just a few bits, so you can measure similarity using Hamming distance.
Large-scale identity verification systems like the European Commission's Visa Information System (VIS) show why detecting reused identity images matters in practice. VIS collects ten fingerprints and a digital photo from each visa applicant, keeping these records for up to five years to compare across applications and spot identity theft, document fraud, or attempts to create multiple identities. The system is built to compare identity features across applications and border checks, rather than just matching files byte-for-byte. For more, see the European Commission's VIS overview.
Why Focus on the Portrait Instead of the Whole Card?
ID cards of the same type often share layouts and background patterns. Hashing the entire card can make different people's cards look similar, which weakens duplicate detection. By isolating and hashing just the portrait region—whose position stays fixed for each document type—the difference between reused photos and genuine ones becomes clearer. For instance, comparing whole cards from different people with dHash might show a minimum difference of 15 bits, but focusing on the portrait pushes this to 23 bits or more, making detection thresholds more dependable.
In formal systems like VIS, the photo is stored alongside biometric data as part of the identity record, supporting both verification (checking if an applicant matches the document) and identification (spotting fraudulent documents). The five-year reuse period for fingerprints and photos shows how regulators set retention and comparison windows for security.
Implementing dHash in Python
The following Python code shows how to compute a dHash for an image and compare hashes using Hamming distance. The PIL library handles image processing:
from PIL import Image
def dhash(image: Image.Image, size: int = 8) -> int:
"""Shrink to (size+1) x size greyscale; one bit per pixel: is it brighter than its right neighbour?"""
small = image.convert("L").resize((size + 1, size), Image.Resampling.LANCZOS)
px = small.tobytes() # one byte per pixel in mode L
bits = 0
for row in range(size):
for col in range(size):
left, right = px[row * (size + 1) + col], px[row * (size + 1) + col + 1]
bits = (bits << 1) | (left > right)
return bits
def distance(a: int, b: int) -> int:
return (a ^ b).bit_count() # Hamming distance: how many of the 64 bits differ
To use this for KYC fraud detection, first extract and normalize the portrait region from each ID card image. Then, compute the dHash for the portrait and compare it to hashes already seen. If the Hamming distance falls below a set threshold, the system can flag the upload for review.
Evaluating Robustness Against Edits
Tests with synthetic ID cards show that dHash holds up against common tweaks like resizing and JPEG compression—these changes often leave the hash untouched. Cropping or rotating the image, though, can shift the hash more, since they change pixel gradients. That's why it's important to preprocess images by flattening and cropping them to the card's edges before hashing, as outlined in document photo preprocessing guides.
In real deployments, a perceptual image hash can act as a low-cost pre-screening step before more expensive biometric or human review, especially when images are resized, recompressed, or converted. Still, a visual similarity score is just a fraud signal, not proof of identity fraud. Effective controls need governance, audit trails, and escalation steps, as the European Court of Auditors stresses in its oversight of fraud prevention systems.
Building a Duplicate Photo Index
To track and compare uploaded portraits, a simple Python class can store hashes and report near-duplicates from other accounts. Here is an example implementation:
class PhotoIndex:
"""Remembers each upload's hash; reports earlier uploads by other accounts within max_distance bits."""
def __init__(self, max_distance: int = 10):
self.max_distance = max_distance
self.seen: list[tuple[int, str]] = []
def check(self, account: str, image: Image.Image) -> list[tuple[str, int]]:
h = dhash(image)
hits = {}
for seen_hash, other in self.seen:
d = distance(h, seen_hash)
if other != account and d <= self.max_distance:
hits[other] = min(d, hits.get(other, d))
self.seen.append((h, account))
return sorted(hits.items(), key=lambda kv: kv[1])
This setup lets the system spot when a portrait has been reused by a different account, while ignoring repeated uploads from the same user. For larger datasets, indexing tricks like chunking the hash or using a BK-tree can speed things up. For more on secure key management in Python, see this related guide.
Handling Matches and Review Workflow
When a match is detected, it should prompt a manual review instead of an automatic rejection. The same face might show up in legitimate cases, such as someone opening both business and personal accounts. Perceptual hashes can't tell the difference between a reused photo and a photo of a photo. Record the matched account and Hamming distance along with other checks like face matching and liveness detection, so reviewers get the full picture.
For more on preparing ID card photos and using perceptual hashing in KYC workflows, see the original article by Ali Ahmed.
