On a recent project I was working on, we needed to collect a bunch of images and generate the thumbnails for them automatically with Paperclip. The problem we found was that on most of the collected pictures people did appear on them and their heads where cut out off the photo on the generated thumbnails because Paperclip uses the center of the image to calculate the new area.

In order to solve this issue, I came with the idea of writing a Paperclip processor to try to recognize all the faces located in the picture, calculate a surrounding area and generate the thumbnail with the desired width and height by using this new area as the center point. The faces are found by using the OpenCV library so it only has to calculate the new area and write the thumbnail image.

Original image:
Paperclip::Thumbnail processor
class Image < ActiveRecord::Base
  has_attached_file :attachment,
      :styles => {:thumbnail => "200x125"}
end

Paperclip::Facecrop processor:
class Image < ActiveRecord::Base
  has_attached_file :attachment,
      :styles => {:thumbnail => "200x125"},
      :processors => [:face_crop]
end

The solution is far from being perfect because sometimes the faces aren't recognized but I guess it could generate more accurate results by using more trained filters sets

Source code and usage instructions can be found at github: [https://github.com/dagi3d/paperclip-facecrop]