·9 min read

Transparent PNGs broke my perceptual hash

My image pipeline threw away new transparent sprite sheets as duplicates. The perceptual hash read RGB and ignored alpha, so every sheet looked the same.

gamedevspritesimagestesting

Contents

The art for ASMR Logo Craft Block comes out of an image model as sprite sheets on a transparent background, which I then cut into separate sprites. A sheet takes the model about a minute and a half. For a few days some of them took fifteen minutes and came back with nothing.

The picture had been drawn every time. My own tooling looked at it, decided it was a copy of one of the reference images I had sent along with the prompt, and threw it away. The reason was a perceptual hash that never looked at the alpha channel.

The setup

An internal tool sends a prompt plus a few reference images to the model and saves whatever image comes back. What comes back can also include the reference images themselves, and those must not be saved as "the result". So the tool fingerprints each returned image and compares it with the attachments. A match means an echo, and the image is skipped.

Exact bytes cannot do that job, because the echoes come back re-encoded. So the fingerprint is a perceptual hash: specifically a difference hash (dHash). Shrink the image to a 9×8 grid of grayscale averages, and for each of the 64 neighbouring pairs record one bit: is the left cell brighter than the right? Two images count as the same if their hashes differ in 5 bits or fewer out of 64. Re-encoding moves a hash by a bit or two, and the design assumed unrelated pictures land 20 or more bits apart.

That is a reasonable design, and for opaque images it worked.

What the job log said

I had Claude Code read the tool's job database rather than guess, and the numbers were plain:

  • 211 completed jobs: median 84 seconds, slowest 142.
  • 15 jobs that ran out the full deadline, about 3.75 hours of waiting in total.
  • In the last two days of that log, 9 of the 10 timeouts had a finished picture sitting in the tool's blob store, never written to an image row.

The waiting was made worse twice over. The tool ignored the model's own "response finished" signal when nothing had been kept, so a discarded picture meant sitting out the whole timeout. And my wrapper script ran each job with a 900-second timeout and three tries. A sheet that got discarded was discarded every time, so one sheet could cost 45 minutes.

False leads

Three explanations came before the right one, and each sounded fine at the time.

The model is just slow. For a while I believed a sheet took five to ten minutes. The log says 84 seconds. The slow ones were not slow, they were stuck.

A stale build is running. One component was loaded from a folder named after an old release, which looked like a smoking gun. A file-by-file diff showed it was identical to the current build. Wrong, and it cost me a pointless manual step.

Only revisions are affected. Revisions, where I attach the current sheet and ask for one change, failed often enough that I wrote it down as the rule. Then a fresh sheet of three hands, drawn from scratch, failed the same way. Fresh drawings were affected too.

Why the alpha channel matters to a perceptual hash

The decoder that fed the hash computed brightness from the red, green and blue channels and never read alpha. A transparent pixel still has an RGB value, it is just invisible, and in these sheets that value was black or close to it. A sprite sheet is mostly empty background, so to the hash every sheet was a mostly black picture with a few shapes on it.

The fingerprints show what that does. These are the two transparent attachments from one job, which have nothing in common visually, and the new hand sheet the model drew:

level sheet (attachment)        6969696b6a696d69
style-reference.png             6969696d69696949
new hand sheet                  4949696d69696d69

The two attachments are 6 bits apart. The new sheet was 4 bits from style-reference.png and 6 from the level sheet, so it fell inside the 5-bit threshold and was skipped as an echo of a picture it does not resemble. In the same job, the real echo of the level sheet matched at 0. Across the nine lost jobs, the false matches sat 1 to 5 bits from an attachment, and the true echoes were all at 0.

So the threshold was not measuring sameness. It was measuring "is this a transparent sprite sheet", and every image in the job was one.

The fix: composite onto a fixed background

Lay each pixel over a fixed background colour, weighted by its alpha, before taking its brightness. Transparent pixels then all become the same known colour, and only what is visible shapes the hash. Opaque pixels have alpha 255 and come out exactly as before, so opaque images keep the fingerprints they already had.

A simplified version, not the tool's code:

const BACKGROUND = 255; // white; any fixed value works if every image uses the same one

function grayAt(r, g, b, a) {
  const luma = 0.299 * r + 0.587 * g + 0.114 * b;
  return (luma * a + BACKGROUND * (255 - a)) / 255;
}

Recomputed on the real lost job, the hand sheet moved from 4 bits to 8 away from style-reference.png, and from 6 to 11 away from the level sheet. The true echo stayed at 0.

If you use a library rather than your own decoder, check what its grayscale step does with the alpha channel before you trust it. I have not tested other pHash or dHash libraries; I only know that this bug looks like nothing until you feed it a transparent PNG.

One more trap, on the test side. The first regression test I wrote passed against the old, broken decoder. Its fake sprite sheets used big filled shapes, which covered enough of the sheet that the alpha-blind hash could still tell them apart. Real sheets are mostly line art and empty space. Switching the fixture to thin outlines reproduced the bug, and only then did the test mean anything. Same lesson as the check that passed while broken: watch the test fail before you believe it.

Not enough on its own

Compositing fixed fresh drawings. It did not fix revisions, and once I looked at why, it could not have.

Recomputed with the alpha fix, 6 of the 9 lost jobs still had their picture 2 to 4 bits from the attachment it was drawn from. Those were revisions: the same sheet with one thing changed. A perceptual hash is built to say "these look alike", and a sheet with one plaque redrawn does look like the original. No threshold separates "a re-encoded copy" from "a small, deliberate edit" if the edit is small enough.

The first fix had added a second guard for this: an image known to be the model's output skips the hash check entirely. On the live system that label was never set. In 451 jobs, not one image carried it, so the guard never ran.

After the first fix shipped, the tool also logged each image it set aside, with the distance, and that log made the next step easy. 39 images had been set aside: 36 were exact echoes at 0 bits, and 3 were real redraws at 2, 3 and 5 bits.

Using a second signal: when the image arrived

Echoes and redraws differ in time, not in pixels. An echo comes back while the request is still being taken in, before anything is drawn. A redraw arrives after the model has started drawing. The tool already recorded that "drawing started" moment, and the job log had it in about 396 jobs.

So the rule became: before drawing starts, the 5-bit threshold applies as before. After it starts, an image is an echo only if it is within 1 bit of an attachment.

Before releasing it, the rule was checked against the whole job history. It kept the three lost redraws and still caught all 36 real echoes, including three that arrived a few seconds after drawing began. Jobs that had worked before came out the same.

The test that mattered was a live one. I asked for the most dangerous kind of change: one coin's blocks turned blue, everything else identical, with the original sheet attached. That is the kind of edit that had failed three times the same day. It came back in 47 seconds, on the first try, with nothing set aside.

Stop waiting for a picture that was thrown away

The hash was the cause, but the cost came from waiting. Three smaller changes took most of it away:

  • When the model says it has finished and nothing was kept, the job now ends after about 32 seconds with a reason that names the set-aside images, instead of running to the deadline.
  • Every rejected image is written to the job's history with the attachment it matched and the distance. Without that, the only symptom was a generic timeout.
  • My wrapper uses a 300-second timeout, about twice the slowest real job, and no longer retries a job whose picture was set aside as an echo. The same image would be rejected again.

What I'd check next time

  • Any perceptual hash on a transparent PNG: find out what happens to alpha before the grayscale step. If transparent pixels keep their hidden RGB, the hash describes the empty background as much as the picture. Composite onto a fixed colour first.
  • A threshold on look-alike images is a threshold on content type, too. If all your inputs share a look (sprite sheets, icons, screenshots of one app), check how far apart unrelated ones actually land before you trust 5 bits.
  • "Looks the same" is not "is the same". If an edit can be legitimately tiny, a perceptual hash cannot decide alone. Find a second signal, and arrival time was the one I had.
  • Log what a filter discards. Every number in this post came from a filter that, until the fix, threw things away without a word.

More posts

New writing when there is something worth saying. By email or by RSS, whichever you prefer.