Perfect for profile picture processing for your website or batch work for ID cards, autocrop will output images centered around the biggest face detected.
pip install autocrop
Cropper class, set some parameters (optional), and start cropping.
from PIL import Image from autocrop import Cropper cropper = Cropper() # Get a Numpy array of the cropped image cropped_array = cropper.crop('portrait.png') # Save the cropped image with PIL cropped_image = Image.fromarray(cropped_array) cropped_image.save('cropped.png')
Further examples and use cases are found in the accompanying Jupyter Notebook.
From the command line
usage: [-h] [-o OUTPUT] [-i INPUT] [-w WIDTH] [-H HEIGHT] [-v] Automatically crops faces from batches of pictures optional arguments: -h, --help Show this help message and exit -o, --output, -p, --path Folder where cropped images will be placed. Default: current working directory -r, --reject Folder where images without detected faces will be placed. Default: same as output directory -i, --input Folder where images to crop are located. Default: current working directory -w, --width Width of cropped files in px. Default=500 -H, --height Height of cropped files in px. Default=500 --facePercent Zoom factor. Percentage of face height to image height. -v, --version Show program's version number and exit
- Crop every image in the
picsfolder, resize them to 400 px squares, and output them in the
autocrop -i pics -o crop -w 400 -H 400.
- Images where a face can't be detected will be left in
- Same as above, but output the images with undetected faces to the
autocrop -i pics -o crop -r reject -w 400 -H 400.
If no output folder is added, asks for confirmation and destructively crops images in-place.
Supported file types
The following file types are supported:
- EPS files (
- GIF files (
.gif) (only the first frame of an animated GIF is used)
- JPEG 2000 files (
- JPEG files (
- LabEye IM files (
- macOS ICNS files (
- Microsoft Paint bitmap files (
- PCX files (
- Portable Network Graphics (
- Portable Pixmap files (
- SGI files (
- SPIDER files (
- TGA files (
- TIFF files (
- WebP (
- Windows bitmap files (
- Windows ICO files (
- X bitmap files (
Autocrop uses OpenCV to perform face detection, which is installed through binary wheels. If you already have OpenCV 3+ installed, you may wish to uninstall the additional OpenCV installation:
pip uninstall opencv-python.
In some cases, you may wish the package directly, instead of through PyPI:
cd ~ git clone https://github.com/leblancfg/autocrop cd autocrop pip install .
Autocrop is currently being tested on:
- Python 3.6+
Although autocrop is essentially a CLI wrapper around a single OpenCV function, it is actively developed. It has active users throughout the world.
If you would like to contribute, please consult the contribution docs.