When I started building a passport photo tool, I assumed the work would be image editing: remove the background, fix the lighting, done. Most of it turned out to be measuring. A photo can look perfect and still be rejected because the head is 2 mm too small.
This post covers how VisaPicture works, what I got wrong along the way, and where it still falls short.
The rules are numbers
Every document has a spec, and the parts that decide whether a photo passes are measurements:
- US passport: a 2 × 2 inch (51 × 51 mm) photo, with 25 to 35 mm from the chin to the top of the hair.
- UK passport: a 35 × 45 mm photo, with 29 to 34 mm from crown to chin.
- India and Schengen: 35 × 45 mm photos, each with its own face-size and background rules.
Many documents also have separate online-upload rules with exact pixel sizes and file-size limits. The tool has to fit a face inside all of those ranges at once, at print resolution, from a photo someone took in their kitchen.
So before anything else, I need to know where the chin, the eyes and the top of the head are, in millimetres.
Finding the face
VisaPicture uses MediaPipe's face landmarker, which returns 478 points on the face, including the irises. I only need a few of them: the chin, the forehead, the two sides of the face, the eye corners and the iris centres.
The eyes give me the eye line, which many specs constrain, and the roll angle, which says how far the head is tilted. MediaPipe also returns a transformation matrix for the face. I turn that into yaw, pitch and roll, so I can tell someone looking off to the side apart from someone who is only tilting their head.
Detection runs in the visitor's browser, in WebAssembly. The photo doesn't have to leave the device to be measured, and the servers stay small. The server still checks the browser's work: it re-runs the crop maths on the measurements and renders the final image from the original upload.
Finding the top of the head
Face landmarks stop at the forehead. The specs measure to the top of the head, and for the US that means the top of the hair.
I tried two ways of finding it and ended up keeping both. The first is anatomical: extend the line from the chin through the forehead by a typical skull proportion. It gives a steady estimate even when the hair is hard to see. The second uses a segmentation model, which labels every pixel as hair, face skin, body, clothes, background and so on. I take the top of the hair region, using a low percentile of the dome rather than the single highest pixel, so a few stray hairs don't count.
Then the engine compares the two. A plausible hair estimate wins. If the hair reaches far above the skull, which happens with big hair, a hat, or a busy background the model mistakes for hair, the engine caps it. If the hair estimate is lower than a skull could be, the engine throws it out and uses the anatomical estimate. It records which source it used for every photo, and that log has turned out to be the most useful debugging tool I have.
Solving for the crop
With the chin, eyes and crown known, choosing the crop is a small optimisation problem. For each document, the solver:
- Rotates the measurements so the eyes are level. It corrects tilt automatically up to 25°. Past that, the photo is more likely a bad pose than a crooked phone.
- Tries a grid of candidate crops and scores each one against the targets for head height, eye line and top margin, converted from millimetres to pixels at 300 DPI.
- Throws out any candidate that falls outside the official ranges, or that would need pixels the original photo doesn't have, such as space above a head that was cut off.
I set the targets inside the official ranges on purpose. For the US, I aim for a head of roughly 31 to 32 mm and keep the eye line a couple of millimetres above the minimum, so normal variation in printing doesn't push a photo past a limit.
All of these numbers live in a per-document config in the database. When I find out a document behaves differently than I expected, I can retune it without redeploying.
Checking quality before cropping
Precise measurements of a blurry or badly lit photo are no use, so every photo passes a quality check first:
- Sharpness uses the Laplacian variance over the face. Blurred test photos scored 6 to 10, and the softest acceptable original scored 19, so the threshold sits between the two.
- Exposure uses the face's average brightness and the share of blown-out highlights. A slightly dim photo is brightened automatically, with a warning. A photo too dark to recover is rejected.
- Colour uses a saturation score near zero to catch black-and-white photos.
One early mistake: I measured exposure over the whole image, so anyone standing in front of a white wall looked overexposed, and that's exactly the photo you want. Measuring a padded box around the face instead removed a whole category of false rejections.
I set every threshold above with a stress-test set of real portraits, plus copies I'd deliberately broken by blurring, darkening, overexposing or tilting them. That set showed me where my guesses were wrong.
Appearance warnings
Glasses, hats, a covered face and harsh light from one side are common reasons for rejection that measurements can't catch. These checks also run on the device, and they only ever warn.
I kept them as warnings because they aren't accurate enough to block anyone. On a set of labelled portraits they catch about two thirds of glasses and headwear, and wrongly flag about 2% of clean photos. So the tool asks a question instead, such as "Are you wearing glasses?" Each answer also tells me how often a warning is right on real photos.
Rendering at print size
The final render runs on the server with sharp. It rotates the original, crops it, and resizes it to the document's exact pixel size at 300 DPI. Then it builds the two downloads: the exact-size file and a 4 × 6 inch print sheet.
Some online uploads must be unedited. For those, the engine keeps the original background and doesn't straighten the photo. It only crops and resizes, and flags a tilt that's too large rather than fixing it quietly.
The developer API uses the same engine. On the server, the same browser detector runs inside headless Chrome, so a photo sent through the API is measured exactly the way it would be on the website.
Advice for anyone building something similar
- Decide what you're measuring, and in what units, before you edit any pixels.
- Keep two estimates for anything uncertain, and log which one won.
- Tune thresholds on a dataset that includes photos you broke on purpose.
- Keep the numbers in config, because the specs and your understanding of them will change.
- When you aren't confident, ask the user a question rather than rejecting their photo.
Where it still falls short
The issuing authority always makes the final call, and no tool can promise a photo will be accepted. Very curly or voluminous hair still makes the top of the head harder to find. The covered-face warning misses more cases than I'd like. And the specs keep changing, so the config needs regular upkeep.
VisaPicture is free at visapicture.com. Upload a photo and you can watch the checks run.
Originally published on the VisaPicture blog.
