16
\$\begingroup\$

I've got 12,000 photos from an event where there's only one person in each photo. I need to crop these photos to apply rule of thirds. The idea of doing all of these individually is daunting and I can't just take 10% off the top of them all since every photo is a little different.

Is there a way for me to automatically crop all of these photos based on the position of the person?

\$\endgroup\$
4
  • 2
    \$\begingroup\$ Does it have to be with photoshop? Maybe you coule manage with Mathematica or a script in python, if you are versed to programming. If possible, add two pictures to illustrate the problem/contrast/etc. \$\endgroup\$
    – anderstood
    Commented Feb 23, 2015 at 3:17
  • \$\begingroup\$ If you are familiar with ImageMagick, this should do what you are looking for. stackoverflow.com/questions/4813608/… \$\endgroup\$ Commented Feb 23, 2015 at 4:03
  • \$\begingroup\$ I tried something similar using OpenCV and python. I can put my script in an answer if you are ok with this direction. \$\endgroup\$
    – agtoever
    Commented Feb 23, 2015 at 10:38
  • \$\begingroup\$ It does not have to be photoshop, I'll try these solutions! \$\endgroup\$
    – Citizen
    Commented Feb 23, 2015 at 15:48

5 Answers 5

16
\$\begingroup\$

Here's a solution using python and opencv:

This will crop all the faces it finds in the jpeg photos in whatever folder you run it in, with the padding specified by the left, right, top, bottom variables:

import cv2
import sys
import glob 

cascPath = "haarcascade_frontalface_default.xml"

# Create the haar cascade
faceCascade = cv2.CascadeClassifier(cascPath)

files=glob.glob("*.jpg")   
for file in files:

    # Read the image
    image = cv2.imread(file)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # Detect faces in the image
    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.1,
        minNeighbors=5,
        minSize=(30, 30),
        flags = cv2.cv.CV_HAAR_SCALE_IMAGE
    )

    print "Found {0} faces!".format(len(faces))

    # Crop Padding
    left = 10
    right = 10
    top = 10
    bottom = 10

    # Draw a rectangle around the faces
    for (x, y, w, h) in faces:
        print x, y, w, h

        # Dubugging boxes
        # cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)


    image  = image[y-top:y+h+bottom, x-left:x+w+right]

    print "cropped_{1}{0}".format(str(file),str(x))
    cv2.imwrite("cropped_{1}_{0}".format(str(file),str(x)), image)

To Use

To use the above script you need python and opencv installed (just google how to install opencv for your platform).

Then save the above code as a .py file, "autocrop.py"or something, Then download and save this file and put it in the same directory as your images.

The script should find all the .jpg files in the folder and crop them based on the padding settings set in the python code.

Example:

With the above code set to 10 px padding to be dramatic, here's the source and result:

enter image description here

Result:

enter image description here

Here's the tutorial I shamelessly adapted:

https://realpython.com/blog/python/face-recognition-with-python/

That tutorial is far better at explaining everything than I am. Basically I just took that code and added in the little bit to batch-process stuff (instead of typing filenames) and then told it to crop and save instead of drawing a rectangle and displaying the picture.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ For python3: 1. pip install opencv-python, 2. Update all print statements to use parentheses, 3. Change cv2.cv.CV_HAAR_SCALE_IMAGE to cv2.CASCADE_SCALE_IMAGE (source: stackoverflow.com/a/36243142/2125392) \$\endgroup\$
    – CivFan
    Commented Oct 11, 2018 at 18:49
7
\$\begingroup\$

Thanks to @Ryan's answer which I adapted 5 years ago, I automated most of my first desk job. It's now the open source package autocrop on PyPI, and can be used from your terminal, or through a Python API.

Autocrop Obama crop

If you have Python installed, install it via pip install autocrop and use it thusly from the command line:

autocrop -i pics -o crop -r reject -w 400 -H 400

In this example, it will crop every image file it can find in the pics folder, resize them to 400 px squares, and output them in the crop directory. Images where it can't detect a face will be sent to the reject directory.

\$\endgroup\$
2
  • \$\begingroup\$ Cool! What does it do when there are two faces in a picture? \$\endgroup\$ Commented Jan 12, 2021 at 15:27
  • \$\begingroup\$ Currently, it crops the biggest face detected. \$\endgroup\$ Commented Jan 12, 2021 at 19:19
4
\$\begingroup\$

Disclaimer: Im the developer of this tool.

You can use Face Crop Jet to detect and crop faces from photos in Bulk.Images of any Format or Size is supported.Faces will be detected and cropped automatically(not just the face,a profile picture for id cards).

The software can be downloaded from http://www.facecropjet.com

enter image description here

\$\endgroup\$
2
\$\begingroup\$

In Adobe Photoshop, you can use this plugin from Exchange:

https://exchange.adobe.com/creativecloud.details.103952.portraitcrop-automated-crop-with-face-detection.html

or: https://rezzomedia.com/portraitCrop

\$\endgroup\$
0
\$\begingroup\$

Once you have python, and opencv installed, and then you install autocrop like explained above, then just make sure you have your input folder and output folder entered correctly into terminal for autocrop to work.

To do this - drag and drop your folder into terminal and copy and paste the file locations into the code line using text edit (the input goes after -i and the output goes after the -o in the code line). Now that your code line is complete copy and paste the finished code into terminal and press return or enter.

Heres an example line (important: all you have to do is replace the input and output):

autocrop -i /Users/yourusername/Desktop/photos -o /Users/yourusername/Desktop/cropped -r reject -w 1024 -H 1024

note: i stands for the input folder o stands for the folder where the recropped file or images will be saved to aka the output folder

\$\endgroup\$

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.