Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

At my work place, passport sized photographs are scanned together, then cut up into individual pictures and saved with unique file numbers. Currently we use to manually select, cut and save the pictures.

I have seen Sony's Cybershot Camera has face detection. Google also gives me something about iphoto when searching for face detection. Picasa has facedetection too. Are there any ways to autodetect the faces in a document, which would improve productivity at my workplace by reducing the time needed to cut up individual images.

Sample Scanned Document(A real document has 5 rows of 4 images each=20 pics): (from:, fairuse)

sample image

For eg. In Picasa 3.8, On clicking View> People, all the faces are shown and I am asked to name them, can I save these individual pictures automatically with the names as different pictures.

share|improve this question
Is face detection really necessary(you need to grab just the face) or is splitting the whole images what you really need to do...Most scanners support this out of box, and a simple script could split on CONSISTENT coordinates...just a thought. btw this would be on linux. Not 100% on windows – RobotHumans Oct 6 '10 at 12:18
face detection is not necessary, but splitting them is. The problem is the photos are not placed uniformly as in the above image. manual placement into scanners creates haphazard arrangements plus the passport size images come in various sizes, 4x4, 5x4, 4x3 etc – abel Oct 6 '10 at 12:29
@aking1012 what did you mean by most scanners support this out of the box, i have a hp scanjet 2400 and a panasonic scanner – abel Oct 6 '10 at 12:49
if you don't have the pictures overlapping, most scanners I have used come with software that will split on the "perfect white" background – RobotHumans Oct 6 '10 at 13:16
I don't have either one of those scanners at the ready, but I would look into the software provided - I have done this on another HP flatbed. – RobotHumans Oct 6 '10 at 13:19

Cropping passport pictures automatically sounds definitely doable. Fixed lighting conditions, always facing forward, consistent image format... I don't think one could ask for more favorable conditions for face detection.

I tried to use facedetect to see the results on your sample image:

raw facedetect output

Passports come in different formats and sizes, so they will be packed on the flatbed of the scanner irregularly, but I assume you will place the pictures always upright. facedetect will give us the centers and size of all the faces. In particular, we can use the size of the face and crop the area around it proportionally. Since passport pictures tend to cover a fixed area of the photograph, it seems to be a relatively safe assumption.

Cropping subregions of an image is really easy using ImageMagick. I wrote a little (and rough) shell script to automate the process:


for file in $files; do
  facedetect $file | while read x y w h; do
    border=$(($w * $pc / 100))
    x=$(($x - $border))
    y=$(($y - $border))
    w=$(($w + $border * 2))
    h=$(($h + $border * 2))
    echo $x $y $w $h
    convert "$file" -gravity NorthWest -crop "${w}x${h}+$x+$y" "${fileno}_$n.jpg"
    n=$(($n + 1))
  fileno=$(($fileno + 1))

I empirically defined a border area of 60% (in the second line of the script) of the width of the detected face. These are the four images I get:

1_1 1_2 1_3 1_4

which is already pretty good. There's always some white space left at the top, which I was able to remove by just adding "-fuzz 10% -trim" in the convert invocation. Here's the result of the first image after that:

1_1 trimmed

Not too shabby for a quick script, and there's plenty of room for improvement. For instance, most passports use a portrait orientation, so having a different vertical/horizontal factor (usually 1.3). Also, faces tend to be slightly moved upward (by 1.3 probably). Correcting for those would result in a better crop, avoiding the need of trimming the white space at the top entirely.

It would be nice if you could post (even privately) a sample flatbed scan to test this script against some real output.

Of course, this suggestion requires a Linux installation. But if I understand correctly, it's not entirely unreasonable to setup a dedicated installation or even a virtual machine to automate this tedious task.

If privacy is not an issue (but I doubt), I would actually be interested helping writing a decent solution in exchange for the cropped faces, which I would use to improve the face detection model.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.