DEV Community

Peiguan
Peiguan

Posted on

Is This Selfie Too Close, Filling the Frame and Cropping the Hair? A Python Face-Fill Check Before a Malfoid AI Upload

Is This Selfie Too Close, Filling the Frame and Cropping the Hair? A Python Face-Fill Check Before a Malfoid AI Upload

The studio at Malfoid AI asks for one clear, front-facing photo of one adult, as a JPEG, PNG or WebP up to 10 MB. A file can meet that test and still be cropped too tight. A close selfie that looked fine in a square camera preview can arrive with the crown of the hair on the edge of the frame, or with the face already filling half the picture. The studio will not zoom out for you. This is a pre-upload check for that case, so you can retake a step back before you pay.

It is not a two-face count, not a group-photo crop, and not a "the whole photo is padded with letterbox bars" check. Those are different guides. This one asks how much of the frame the face already occupies, and whether the hair has been cut by the edge.

I run the studio. It makes a still 3:4 Female Malfoy / Fem Draco portrait at a 1K setting, from one photo of an adult or without a photo. A portrait is $4.99 and spends 2 credits. There is no video, no prompt box, and no clip length. The look is unofficial fan art, not affiliated with the author, the publishers, or the studios. The fixtures in this post are synthetic sitters — shapes only, platinum hair, an oval face, academy clothes, on a candlelit corridor drawing. No real photograph, no customer render, no studio showcase. The tight and loose frames are documented crops of that drawing, not camera zoom.

The script, tests, and bench live next to this post as face_fill_check.py, make_face_fill_fixtures.py, test_face_fill_check.py, and bench_face_fill.py. They import the existing make_test_portrait.py.

What the check measures

I use the construction boxes of the synthetic sitter — the face oval and the hair ellipse — mapped through the same crop that built the fixture. On a real selfie you can pass a detected box the same way; this guide does not invent a new detector.

Four numbers come out of those boxes, after they have been clamped to the frame:

  • face_h_frac: face height divided by frame height
  • face_w_frac: face width divided by frame width
  • top_clear_frac: the y-coordinate of the hair top, divided by frame height
  • side_clear_frac: the smaller of the left and right face margins, divided by frame width

I also set hair_clipped when the construction hair ellipse starts above the frame, or hangs off a side. A crop that has already cut the crown will show a hair top at y = 0 and hair_clipped = True.

The thresholds I locked

After the bench below I locked these cuts:

  • face_h_frac >= 0.42, or
  • top_clear_frac <= 0.08, or
  • side_clear_frac <= 0.08, or
  • hair_clipped

→ too_close, advice retake_or_step_back. Otherwise ok.

Any one of those is enough. A face that fills the frame but still has a little sky above the hair should still be flagged. A face of a normal size whose crown has been cut should be flagged even if the remaining face fraction is modest. The cuts sit in the gaps on this synthetic bench, not at pretty round numbers chosen first. A real selfie has more hair volume than a drawn ellipse, so I treat a flag as a prompt to look at the file, not as an automatic reject.

The fixtures

Every sitter starts from the same frontal drawing, with eight seeds. Seed 1 is the default sitter. Seeds 2–8 shift skin tone and face width. I then take a 3:4 window and resize it back to 768×1024, so every file is the same size:

  • ok: the full frame, no extra crop
  • tight: a 62% window centred on the face, with the top of the window 4% of the frame above the hair, so the face grows and the crown approaches the edge without being cut
  • haircut: the same 62% zoom, but the window starts 20 px below the hair top, so the crown is cut
  • loose: a window 36% larger than the frame, which pads the sitter and shrinks the face

Each sitter × level is written as a PNG and as a JPEG at quality 95, because that is how an upload often arrives. 8 sitters × 4 levels × two encodings = 64 files.

The numbers

All 64/64 classified as intended: 16/16 ok, 16/16 loose kept at ok, 16/16 tight too_close, 16/16 haircut too_close with hair_clipped. Median analyse time was 6.49 ms (mean 6.22 ms) on this box. Tests: 4 passed.

The default sitter (seed 1) is the distinctive walk-through.

  • ok: face_h_frac 0.302734375, top_clear_frac 0.146484375, side_clear_frac 0.34375, not clipped, state ok.
  • tight: face_h_frac 0.48828125, top_clear_frac 0.0645161290322581, side_clear_frac 0.2479838710, not clipped, state too_close. The face fraction and the top clearance both cross their cuts. The side margin is still well above 0.08, so this flag is not a side-crop.
  • haircut: face_h_frac 0.48828125, top_clear_frac 0.0, side_clear_frac 0.2479838710, hair_clipped True, state too_close.
  • loose: face_h_frac 0.2225988051, top_clear_frac 0.2400620404411765, side_clear_frac 0.3851102941, not clipped, state ok.

Across all eight sitters the face fraction is identical at each level (the face height does not change with the width shift), and every haircut is clipped. The side margins do move with seed, because face width moves: seed 1's ok side clearance is 0.34375; seed 2's is 0.3112.

The gap the cuts sit in is wide. The ok face fraction is 0.3027 and the tight face fraction is 0.4883, with the cut at 0.42. The ok top clearance is 0.1465 and the tight top clearance is 0.0645, with the cut at 0.08.

How to run it

python make_face_fill_fixtures.py --out fixtures/fill
python face_fill_check.py fixtures/fill/s1_tight.png --seed 1
python -m pytest test_face_fill_check.py
python bench_face_fill.py
Enter fullscreen mode Exit fullscreen mode

On a real photo, pass the face and hair boxes you already have. The construction boxes are a stand-in so the measurements can be repeated.

What this is not

A face that is too small is a different problem, and so is a group photo with two faces, a backlight silhouette, or a colour cast. This check will not catch those. It only answers whether the sitter is already too close to the edge of a 3:4 frame.

If you want one still 3:4 Malfoid portrait and you do not want to iterate on a prompt, malfoidai.com does this at $4.99 a portrait, or 2 credits from a one-time pack ($13.99/30, $35.99/100, $59.99/200; packs need sign-in). There is no free portrait and no subscription. Prices were re-checked on the pricing page on 26 November 2026, 18:22 Asia/Shanghai, unchanged since 31 October 2026.

Top comments (0)