What does A Screenshot Is Not a Payment conclude, in numbers, about “Average Hash and Hamming Distance: Detecting Repeated Screenshots, and Where Detection Is Blind”?

Answer: Chapter 5 shows that a whole-image average hash at threshold 4 flags 0.9135 of same-layout genuine receipts as resends, while a hash of the fields alone recognises 0.8333 of resends at threshold 10 with no false positives.

Scope of the numbers: Orders, account tails, amounts, screenshots and hashes are synthesised from fixed seeds, parameters are illustrative, and every calculation is generated by the book's programs. Marked boxes separate published evidence, runnable Python calculations (20 programs), classical sources and the author's hypotheses.

中文版

Data table

The numbers below are printed verbatim by the chapter's Python program and can be reproduced by running it. standard-library Python onlyprinted values that match the answer: 0.9135, 10

pairs: copies 300, same-layout genuine 14850, cross-layout 30000
median distance: copies 0, same layout 1, cross layout 17
tTPR copiesFPR same layoutFPR cross layout
00.59330.27730.0000
10.67000.58720.0000
20.70000.73040.0000
30.74000.83390.0000
40.75000.91350.0000
60.81670.98960.0000
80.87330.99950.2544
100.88671.00000.3036
Show the full program
# Chapter 5. p52: whole-image hashes cannot tell a layout from a copy
# Model: 300 synthetic genuine receipts (3 app layouts x 100, random
# amounts, times, last five digits and transaction numbers from a
# seeded generator) and 300 returned copies, each a genuine receipt
# put through one of six benign changes from p51 in turn (noise,
# brightness, contrast, rescale, 3-row shift, new status clock). For
# a threshold t, a pair at aHash distance <= t is flagged as the same
# screenshot. TPR = copies flagged against their source; FPR = pairs
# of different genuine receipts of the same layout that are flagged.
import math
import hashlib
import random

SIZE = 64
FONT = {"0": "111101101101111", "1": "010110010010111",
        "2": "111001111100111", "3": "111001111001111",
        "4": "101101111001001", "5": "111100111001111",
        "6": "111100111101111", "7": "111001001001001",
        "8": "111101111101111", "9": "111101111001111",
        ":": "000010000010000", ".": "000000000000010"}
# three illustrative app layouts: header shade, header rows, logo box
APPS = [(40, 9, (11, 18, 26, 38)), (70, 12, (14, 20, 4, 14)),
        (245, 0, (3, 9, 50, 60))]


def blank(app):
    shade, rows, (r0, r1, c0, c1) = APPS[app]
    img = [[245] * SIZE for _ in range(SIZE)]
    for r in range(rows):
        img[r] = [shade] * SIZE
    for r in range(r0, r1):
        for c in range(c0, c1):
            img[r][c] = 90
    for r in (37, 43, 49, 55):               # fixed field labels
        for c in range(4, 20):
            img[r][c] = img[r + 1][c] = 150
    return img


def text(img, s, row, col, scale, ink):
    for ch in s:
        bits = FONT[ch]
        for k, b in enumerate(bits):
            if b == "1":
                y, x = row + (k // 3) * scale, col + (k % 3) * scale
                for dy in range(scale):
                    for dx in range(scale):
                        if 0 <= y + dy < SIZE and 0 <= x + dx < SIZE:
                            img[y + dy][x + dx] = ink
        col += 4 * scale


def receipt(app, amount, clock, tail, txn, status="09:41"):
    img = blank(app)
    text(img, status, 0, 44, 1, 200 if app < 2 else 30)
    text(img, amount, 23, 8, 2, 20)
    text(img, clock, 36, 30, 1, 60)
    text(img, tail, 42, 30, 1, 60)
    text(img, txn[:8], 48, 28, 1, 60)
    text(img, txn[8:], 54, 28, 1, 60)
    return img


def ahash(img, n=8):
    step = SIZE // n
    means = []
    for i in range(n):
        for j in range(n):
            cell = [img[r][c] for r in range(i * step, i * step + step)
                    for c in range(j * step, j * step + step)]
            means.append(math.fsum(cell) / len(cell))
    avg = math.fsum(means) / len(means)
    bits = 0
    for m in means:
        bits = (bits << 1) | (m > avg)
    return bits


def dhash(img):
    bits = 0
    for i in range(8):
        rows = range(i * 8, i * 8 + 8)
        cols = [(j * SIZE) // 9 for j in range(10)]
        cells = []
        for j in range(9):
            cell = [img[r][c] for r in rows
                    for c in range(cols[j], cols[j + 1])]
            cells.append(math.fsum(cell) / len(cell))
        for j in range(8):
            bits = (bits << 1) | (cells[j] > cells[j + 1])
    return bits


def ham(a, b):
    return bin(a ^ b).count("1")


def raw(img):
    return bytes(v for row in img for v in row)


def clip(v):
    return 0 if v < 0 else 255 if v > 255 else int(v)


def noisy(img, rng, amp=6):
    return [[clip((v + rng.randint(-amp, amp)) // 4 * 4) for v in row]
            for row in img]


def bright(img, d=25):
    return [[clip(v + d) for v in row] for row in img]


def contrast(img, k=0.85):
    return [[clip(128 + (v - 128) * k) for v in row] for row in img]


def rescale(img, m=48):
    small = [[img[(r * SIZE) // m][(c * SIZE) // m] for c in range(m)]
             for r in range(m)]
    return [[small[(r * m) // SIZE][(c * m) // SIZE] for c in range(SIZE)]
            for r in range(SIZE)]


def shift_up(img, k=3):
    return [row[:] for row in img[k:]] + [[245] * SIZE for _ in range(k)]


rng = random.Random(20261113)


def fresh(app):
    amt = "%d.%02d" % (rng.randrange(20, 1000), rng.choice((0, 0, 50)))
    clock = "%02d:%02d" % (rng.randrange(24), rng.randrange(60))
    tail = "%05d" % rng.randrange(100000)
    txn = "".join(rng.choice("0123456789") for _ in range(16))
    return receipt(app, amt, clock, tail, txn)


def variant(img, k):
    if k == 0:
        return noisy(img, rng)
    if k == 1:
        return bright(img)
    if k == 2:
        return contrast(img)
    if k == 3:
        return rescale(img)
    if k == 4:
        return shift_up(img)
    out = [row[:] for row in img]
    text(out, "%02d:%02d" % (rng.randrange(24), rng.randrange(60)),
         0, 44, 1, 200 if out[0][0] < 100 else 30)
    return out


genuine, apps = [], []
for app in range(3):
    for _ in range(100):
        genuine.append(fresh(app))
        apps.append(app)
H = [ahash(g) for g in genuine]
dup = []
for i, g in enumerate(genuine):
    dup.append(ham(H[i], ahash(variant(g, i % 6))))
same = [ham(H[i], H[j]) for i in range(300) for j in range(i + 1, 300)
        if apps[i] == apps[j]]
cross = [ham(H[i], H[j]) for i in range(300) for j in range(i + 1, 300)
         if apps[i] != apps[j]]
print("pairs: copies %d, same-layout genuine %d, cross-layout %d"
      % (len(dup), len(same), len(cross)))
print("median distance: copies %d, same layout %d, cross layout %d"
      % (sorted(dup)[150], sorted(same)[len(same) // 2],
         sorted(cross)[len(cross) // 2]))
print("   t    TPR copies   FPR same layout   FPR cross layout")
for t in (0, 1, 2, 3, 4, 6, 8, 10):
    tpr = len([d for d in dup if d <= t]) / len(dup)
    fs = len([d for d in same if d <= t]) / len(same)
    fc = len([d for d in cross if d <= t]) / len(cross)
    print("%4d  %11.4f  %16.4f  %17.4f" % (t, tpr, fs, fc))

Single source and citation

Fan, Y. (2026). Fault-Tolerant Parsing of Payment Receipts and Spoken Orders: Last-Five-Digit Write-Off Automata and Blocking Duplicate-Screenshot Fraud (Version 1.0.0), Chapter 5: Average Hash and Hamming Distance: Detecting Repeated Screenshots, and Where Detection Is Blind. 5D Library. DOI pending

DOI pending · monograph page goes live after publication · 5DL-B-267 · v1.0.0 · CC-BY-NC-ND-4.0

BibTeX
@book{fan2026267,
  author    = {Fan, Yafu},
  title     = {Fault-Tolerant Parsing of Payment Receipts and Spoken Orders: Last-Five-Digit Write-Off Automata and Blocking Duplicate-Screenshot Fraud},
  year      = {2026},
  publisher = {5D Library},
  edition   = {1.0.0},
  note      = {DOI pending}
}

Other chapter answers in this book