DEV Community

Peiguan
Peiguan

Posted on

My Selfie Looks Fine — Why Is the Malfoid Portrait Mushy? Detecting JPEG Block Grids, Ghosts and Screenshots in Python

People ask assistants things like "my selfie looks fine, why is the AI portrait mushy?" and "can I upload a WhatsApp or Instagram screenshot?" The short version is on the photo guide at the studio I build, malfoidai.com: "The original photo beats screenshots, heavy compression, tiny crops." A still 3:4 Malfoid portrait is $4.99 (2 credits), watermark-free, and a delivered one isn't refunded as a change of mind, so a mushy source is an expensive way to find that out.

This post is a small Python detector for the three things that make a "fine looking" selfie a bad upload: an 8×8 JPEG block grid, a JPEG-ghost residual (the Farid 2009 trick), and a crop+scale+JPEG "screenshot of a photo". I measured it on synthetic, shape-only sitters (no real face, no AI output): an original PNG, JPEG at quality 95/80/60/40/20, a two-pass messenger-like recompress, and a screenshot chain. The combined rule flagged every bad file and none of the clean ones.

Short answer

  • Don't upload a screenshot or a chat-app copy if you still have the original. On the synthetic sitters, a crop+scale+JPEG "screenshot" kept only 39.5% of the original's fine-texture energy (hf_ratio 0.395). A two-pass messenger recompress (q=80, then q=70 at 4:2:0) always left a JPEG ghost at quality 70.
  • The useful block-grid number is not "is there an 8-pixel preference?" Drawn shapes have a mild preference at many periods. The score that rises as JPEG quality falls is period-8 specificity: score_8 / max(score_7, score_9). Means over 8 sitters: PNG 0.936, q=95 0.954, q=80 1.012, q=60 1.146, q=40 1.321, q=20 1.696. I flag at ≥ 1.08.
  • A JPEG ghost is a strict valley in the recompress-residual curve, not "the quality with the smallest residual". Never-compressed PNGs fall smoothly toward q=95 and have no valley (mean depth 0.027). A file last saved at q=80 has a real dip of depth 0.275 at 80; q=60 has depth 1.021. I only flag a ghost at quality 75 or below with depth ≥ 0.12, so a mild q=80 camera JPEG is reported but not treated as a bad upload.
  • Practical rule, on this set: flag if specificity ≥ 1.08, or a ghost at q≤75 with depth ≥ 0.12, or a screenshot score ≥ 0.50. Against "don't upload" = {q≤60, messenger, screenshot} and "ok" = {PNG, q=95, q=80}: precision 1.000, recall 1.000 (40 true positives, 0 false positives, 24 true negatives, 0 false negatives). Same 1.000 / 1.000 on every leave-one-sitter-out fold.

The test set

8 synthetic sitters from make_test_portrait.py (seeds 1–8, 768×1024, --texture, the same drawing my other posts use). Each one is saved as:

  • the original PNG
  • JPEG at q=95, 80, 60, 40, 20 (4:4:4, so the grid isn't mixed with chroma subsampling)
  • messenger: JPEG q=80, then JPEG q=70 with 4:2:0 — a small, no-resize stand-in for a chat-app recompress
  • screenshot: crop a few non-multiple-of-8 pixels off the edges, bilinear-scale back to 768×1024, JPEG q=75 with 4:2:0

That's 64 files. hf_ratio is the mean-squared 3×3 Laplacian of the file, divided by the original PNG's, a mushiness proxy.

The three measurements

1. An 8×8 block grid, made specific. At each period p from 5 to 12, take the mean first-difference along rows and columns, and compare the p-spaced boundaries to the interior. A JPEG's discontinuities are special at p = 8. The ratio of that score to the stronger of p = 7 and p = 9 is block_specificity. A constant image (no differences at all) scores 1.0.

2. A JPEG ghost. Recompress the working copy at quality 20, 25, …, 95 and measure the mean absolute residual. A file that was already a JPEG at quality Q0 usually shows a strict local minimum around Q0 (both neighbours higher). A PNG's curve just falls toward q=95; a shallow wrinkle on the way down is not a ghost. Depth is that valley, divided by the curve's median residual.

3. A screenshot score. A crop+scale+JPEG is still a JPEG — there's usually a ghost at the last save quality — but scaling smears the previous 8×8 lattice, so specificity is milder than a camera JPEG of the same quality. The heuristic fires when there's a ghost in the q=68–79 band and specificity is in 1.04–1.20. On this set it scored 0.70 on all 8 screenshots and 0.00 on every PNG, q=95 and q=80.

# Thresholds from the bench (8 sitters × 8 variants).
T_SPEC, T_GHOST, T_SCREEN, T_HEAVY_Q, T_HEAVY_SPEC = 1.08, 0.12, 0.50, 65, 1.14

def decide(grid, ghost, shot):
    flagged_block = grid["block_specificity"] >= T_SPEC
    flagged_ghost = ghost["ghost_depth"] >= T_GHOST and ghost["ghost_quality"] <= 75
    flagged_shot = shot["screenshot_score"] >= T_SCREEN
    heavy = (
        (ghost["ghost_depth"] >= T_GHOST and ghost["ghost_quality"] <= T_HEAVY_Q)
        or grid["block_specificity"] >= T_HEAVY_SPEC
    )
    flag = bool(flagged_block or flagged_ghost or flagged_shot)
    ...
Enter fullscreen mode Exit fullscreen mode

The full script is attached (jpeg_detect.py); it prints one JSON line. Exit code 2 means "don't upload this if you have a better file."

python jpeg_detect.py photo.jpg --pretty
Enter fullscreen mode Exit fullscreen mode

The numbers (verbatim from bench_jpeg.py)

======== TABLE 1: means by variant (8 sitters) ========
kind          n flag heavy  block   spec  g_q  depth  shot  hf_r align
png           8    0     0  1.003  0.936   57  0.027  0.00 1.000     0
jpeg_q95      8    0     0  0.973  0.954   52  0.055  0.00 1.008     0
jpeg_q80      8    0     0  1.123  1.012   80  0.275  0.00 1.016     4
jpeg_q60      8    8     8  1.257  1.146   60  1.021  0.20 1.008     4
jpeg_q40      8    8     8  1.433  1.321   40  1.211  0.03 0.954     5
jpeg_q20      8    8     8  1.834  1.696   40  1.597  0.00 0.843     8
messenger     8    8     0  1.111  1.019   70  0.439  0.09 1.174     5
screenshot    8    8     0  1.256  1.112   75  0.282  0.70 0.395     8

======== TABLE 2: precision / recall for 'flag before paying' ========
positives: jpeg q<=60, messenger two-pass, screenshot
negatives: original PNG, jpeg q=95, jpeg q=80
flag (combined)                          tp=40 fp= 0 tn=24 fn= 0  precision=1.000 recall=1.000
block_specificity >= 1.08                tp=31 fp= 0 tn=24 fn= 9  precision=1.000 recall=0.775
ghost_depth >= 0.05                      tp=40 fp=12 tn=12 fn= 0  precision=0.769 recall=1.000
screenshot_score >= 0.50                 tp= 9 fp= 0 tn=24 fn=31  precision=1.000 recall=0.225
heavy only                               tp=24 fp= 0 tn=24 fn=16  precision=1.000 recall=0.600

======== TABLE 3: leave-one-sitter-out of the combined flag ========
  held seed=1 tp=5 fp=0 tn=3 fn=0 precision=1.000 recall=1.000
  ... (seeds 2–8 identical) ...
  pooled          tp=40 fp=0 tn=24 fn=0 precision=1.000 recall=1.000
Enter fullscreen mode Exit fullscreen mode

A few things the table is saying that are easy to misread:

  • q=80 is not flagged, on purpose. It has a real ghost at 80 (depth 0.275) but specificity only 1.012. The photo guide is about heavy compression and screenshots; a mild camera JPEG is usually fine. The original is still better.
  • Specificity alone misses the messenger copies (mean spec 1.019): that's why the ghost scan is there. All 8 last-saved at q=70, depth 0.439.
  • hf_ratio 1.174 on the messenger copies is not "sharper". Double compression adds blocking and ringing, which a Laplacian also counts. The screenshot is the one that actually goes mushy (0.395).
  • The ghost_depth >= 0.05 row above is a looser cut than the one I ship (0.12, and only at q≤75). At 0.05 it flags some q=95 wrinkle-dips; that's why I raised it.

What to do with the result

If the script flags the file, go and find the original — the Camera roll, not the chat bubble, not a zoomed screenshot. The studio accepts JPEG, PNG or WebP up to 10 MB, one clear front-facing photo of one adult. A new selfie by a window takes a minute and almost always beats a favourite picture that's been through two apps. If you don't have a photo you want to use, the no-photo preset is the same $4.99 and needs none.

These numbers are from drawings, not from your phone. The direction should transfer; the exact thresholds might move a little on a real camera JPEG. That's a fair reason to treat a "flag" as "use the original if you have one", not as a medical diagnosis of the file.

I build malfoidai.com. Prices and photo rules re-read on the live pricing and photo-requirements pages on 2026-11-16 around 18:32 CST (UTC+8): $4.99 / 2 credits per still 3:4 1K portrait, packs $13.99/30, $35.99/100, $59.99/200, credits never expire. Malfoid is unofficial fan art; the studio is independent and not affiliated with any book, film, studio or author. Scripts and tests are attached; everything runs on the synthetic sitters, no real faces.

Top comments (0)