views:

41

answers:

1

Hi I am wanting to use the python imaging library to crop images to a specific size for a website. I have a problem, these images are meant to show people's faces so I need to automatically crop based on them.

I know face detection is a difficult concept so I'm thinking of using the face.com API http://developers.face.com/tools/#faces/detect which is fine for what I want to do.

I'm just a little stuck on how I would use this data to crop a select area based on the majority of faces.

Can anybody help?

Joe

+1  A: 

If you have some rectangle that you want to excise from an image, here's what I might try first:

  1. (optional) If the image is large, do a rough square crop centered on the face with dimensions sqrt(2) larger than the longer edge (if rectangular). Worst-case (45° rotation), it will still grab everything important.
  2. Rotate based on the face orientation (something like rough_crop.rotate(math.degrees(math.atan(ydiff/xdiff)), trig is fun)
  3. Do a final crop. If you did the initial crop, the face should be centered, otherwise you'll have to transform (rotate) all your old coordinates to the new image (more trig!).
Nick T
the angle of rotation seems to be directly provided by the api as `roll` value, so we can go even without trig knowledge :)
mykhal
That's good. I don't like trig (i've done it in school and don't really see how it works!).I'll see what that does :)
Joe Simpson
@{Joe Simpson} oops, bad news.. i take it back.. roll, pitch and yaw seem to be 3D rotation coords :) so roll is probably not exactly 2D rotation angle
mykhal
The API provides all three axes of rotation...`yaw`, `pitch`, and `roll`, so `roll` is probably a 3-D property, not a *direct* correlation to the face on the 2-D image. In most circumstances though, they should be pretty close, and it's not like the value is exact anyways.
Nick T
@mykhal Ugh, I type slow... I'm thinking that if faces are generally pointed at the camera (huge assumption), `yaw` and `pitch` should be fairly small.
Nick T
I will have to see what I can play around with :/
Joe Simpson