I'm looking for a tool that will take a (high-resolution) group photo, locate all the faces automatically and extract them as individual headshots. Is there such a tool? Recognizing faces is pretty standard, but automatically extracting them as single files is something I haven't yet come across.

I wouldn't mind if the tool was free/cheap, either :)

  • 1
    which OS should it run on?
    – albert
    Commented Sep 7, 2018 at 13:53
  • 1
    Oh right. Thanks for asking. I'd prefer Windows, but iOS or OS X would be fine, too. Commented Sep 7, 2018 at 14:14
  • 2
    If you have any scripting ability, on OSX you could use FaceDetect to extract coordinates which could then be passed to ImageMagick for extraction.
    – StarGeek
    Commented Sep 7, 2018 at 18:11
  • 1
    @StarGeek: Wow, that looks promising - it's a Python script, so it should work on Windows as well (where I feel more comfortable). Commented Sep 9, 2018 at 8:54
  • 2
    It takes a bit of work to make it usable on Windows. Check this Reddit post I made which details the steps I had to go through to make it work.
    – StarGeek
    Commented Sep 9, 2018 at 17:26

1 Answer 1


Thanks to @StarGeek (relevant Reddit post), I was able to write a small script for this.

To get the environment set up in Python 3 (on a Windows 10 machine using Anaconda), I went through the following steps:

  • pip install opencv-python
  • Download and extract facedetect
  • Rename facedetect to facedetect.py
  • In the file facedetect.py, edit the line

DATA_DIR = '/usr/share/opencv/'

to the proper path. On my Windows 10 it was:

DATA_DIR = 'C:/Anaconda3/Lib/site-packages/cv2/data/'; if you have Python installed only for the current user, it might look something like

DATA_DIR = '%LOCALAPPDATA%/Programs/Python/Python37/Lib/site-packages/cv2/data/'

  • Finally, change the line

'HAAR_FRONTALFACE_ALT2': 'haarcascades/haarcascade_frontalface_alt2.xml'


'HAAR_FRONTALFACE_ALT2': 'haarcascade_frontalface_alt2.xml',

  • Place the image(s) to be processed into the same directory as facedetect.py
  • Run the following script in that directory:
import cv2
import facedetect
import glob
import os


for file in glob.glob("*.jpg"):
    original = cv2.imread(file)
    im, faces = facedetect.face_detect_file(file)
    if len(faces):
        directory = os.path.splitext(os.path.split(file)[1])[0]
        for i, (x,y,w,h) in enumerate(faces):
            face = original[y:y+h, x:x+w]
            cv2.imwrite(os.path.join(directory, "{0:03}.jpg".format(i)), face)

For example, on this image (by Hayden Schiff (IagoQnsi, Wikimedia Commons))

FC Cincinnati 2016

the script produces the following output:

Windows Explorer screenshot

  • Don't forget to accept your own answer (you many have to wait until you can; use dot be 2 days, not sure now). Doing so will help others in future. if you really want to go over the top, you could find an image with a creative commons license and embed the image and the results in your answer, so that we get a feel for what it does.
    – Mawg
    Commented Sep 12, 2018 at 14:43
  • @Mawg: Wonderful idea :) Commented Sep 12, 2018 at 15:12
  • 2
    @TimPietzcker Nicely done. Sorry I didn't post as an answer, I felt that the answer should have included any scripting you actually did to extract the images. Also, did you have to make changes to the facedetect script as I mentioned in the Reddit post or where you able to get it to run as is?
    – StarGeek
    Commented Sep 12, 2018 at 19:33
  • Please, do post such examples. Thanks++
    – Mawg
    Commented Sep 12, 2018 at 19:40
  • @StarGeek: Ah, yes, I forgot that step. Will add that in momentarily. Commented Sep 12, 2018 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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