Package: autocrop
autocropPerfect for profile picture processing for your website or batch work for ID cards, autocrop will output images centered around the biggest face detected.InstallationSimple!pipinstallautocropUseAutocrop can be usedfrom the command lineor directlyfrom Python API.From PythonImport theCropperclass, set some parameters (optional), and start cropping.Thecropmethod accepts filepaths ornp.ndarray, and returns Numpy arrays. These are easily handled withPILorMatplotlib.fromPILimportImagefromautocropimportCroppercropper=Cropper()# Get a Numpy array of the cropped imagecropped_array=cropper.crop('portrait.png')# Save the cropped image with PIL if a face was detected:ifcropped_array:cropped_image=Image.fromarray(cropped_array)cropped_image.save('cropped.png')Further examples and use cases are found in theaccompanying Jupyter Notebook.From the command lineusage: [-h] [-o OUTPUT] [-i INPUT] [-w WIDTH] [-H HEIGHT] [-e EXTENSION] [-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.
  -e, --extension
  		Enter the image extension which to save at output.
  		Default: Your current image extension
  -v, --version
  		Show program's version number and exitExamplesCrop every image in thepicsfolder, resize them to 400 px squares, and output them in thecropdirectory:autocrop -i pics -o crop -w 400 -H 400.Images where a face can't be detected will be left incrop.Same as above, but output the images with undetected faces to therejectdirectory:autocrop -i pics -o crop -r reject -w 400 -H 400.Same as above but the image extension will bepng:autocrop -i pics -o crop -w 400 -H 400 -e pngIf no output folder is added, asks for confirmation and destructively crops images in-place.Detecting faces from video filesYou can use autocrop to detect faces in frames extracted from a video. A great way toperform the frame extraction step is withffmpeg:mkdirframesfaces# Extract one frame per secondffmpeg-iinput.mp4-filter:vfps=fps=1/60frames/ffmpeg_%0d.bmp# Crop faces as jpgautocrop-iframes-ofaces-ejpgSupported file typesThe following file types are supported:EPS files (.eps)GIF files (.gif) (only the first frame of an animated GIF is used)JPEG 2000 files (.j2k,.j2p,.jp2,.jpx)JPEG files (.jpeg,.jpg,.jpe)LabEye IM files (.im)macOS ICNS files (.icns)Microsoft Paint bitmap files (.msp)PCX files (.pcx)Portable Network Graphics (.png)Portable Pixmap files (.pbm,.pgm,.ppm)SGI files (.sgi)SPIDER files (.spi)TGA files (.tga)TIFF files (.tif,.tiff)WebP (.webp)Windows bitmap files (.bmp,.dib)Windows ICO files (.ico)X bitmap files (.xbm)MiscInstalling directlyIn some cases, you may wish the package directly, instead of throughPyPI:cd ~
git clone https://github.com/leblancfg/autocrop
cd autocrop
pip install .condaDevelopment of aconda-forgepackage for theAnaconda Python distributionis currently stalled due to the complexity of setting up the workflow with OpenCV. Please leave feedback onissue #7to see past attempts if you are insterested in helping out!RequirementsBest practice for your projects is of course touse virtual environments. At the very least, you will need tohave pip installed.Autocrop iscurrently being tested on:Python 3.7 to 3.10OS:LinuxmacOSWindowsMore InfoCheck out:http://docs.opencv.org/master/d7/d8b/tutorial_py_face_detection.html#gsc.tab=0http://docs.opencv.org/master/d5/daf/tutorial_py_histogram_equalization.html#gsc.tab=0Adapted from:http://photo.stackexchange.com/questions/60411/how-can-i-batch-crop-based-on-face-locationContributingAlthough 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 thecontribution docs.