Skip to main content

Automatically crops faces from batches of pictures

Project description

autocrop

Travis Status AppVeyor Build Status codecov Documentation PyPI version Downloads Language grade: Python

Perfect for profile picture processing for your website or batch work for ID cards, autocrop will output images centered around the biggest face detected.

Installation

Simple!

pip install autocrop

Use

Autocrop can be used from the command line or directly from Python API.

From Python

Import the Cropper class, set some parameters (optional), and start cropping.

The crop method accepts filepaths or np.ndarray, and returns Numpy arrays. These are easily handled with PIL or Matplotlib.

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] [-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 exit

Examples

  • Crop every image in the pics folder, resize them to 400 px squares, and output them in the crop directory:
    • autocrop -i pics -o crop -w 400 -H 400.
    • Images where a face can't be detected will be left in crop.
  • Same as above, but output the images with undetected faces to the reject directory:
    • autocrop -i pics -o crop -r reject -w 400 -H 400.
  • Same as above but the image extension will be png:
    • autocrop -i pics -o crop -w 400 -H 400 -e png

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 (.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)

Gotchas

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.

Installing directly

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 .

conda

Development of a conda-forge package for the Anaconda Python distribution is also currently slated for development. Please leave feedback on issue #7 if you are insterested in helping out.

Requirements

Best practice for your projects is of course to use virtual environments. At the very least, you will need to have pip installed.

Autocrop is currently being tested on:

  • Python 3.6+
  • OS:
    • Linux
    • macOS
    • Windows

More Info

Check out:

Adapted from:

Contributing

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

autocrop-1.1.1.tar.gz (166.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

autocrop-1.1.1-py3.8.egg (167.0 kB view details)

Uploaded Egg

File details

Details for the file autocrop-1.1.1.tar.gz.

File metadata

  • Download URL: autocrop-1.1.1.tar.gz
  • Upload date:
  • Size: 166.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.0

File hashes

Hashes for autocrop-1.1.1.tar.gz
Algorithm Hash digest
SHA256 8bad5a9a47f303c902b56863b85c51d9f511e4e9c3d0a6232e6e362f0d91acb1
MD5 1106ad8823bf1bc2a298653de6748d56
BLAKE2b-256 314d1e64f88dc69f8ab1b4c58807793f656549841f0c50fe226516e6dd999600

See more details on using hashes here.

File details

Details for the file autocrop-1.1.1-py3.8.egg.

File metadata

  • Download URL: autocrop-1.1.1-py3.8.egg
  • Upload date:
  • Size: 167.0 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.0

File hashes

Hashes for autocrop-1.1.1-py3.8.egg
Algorithm Hash digest
SHA256 5de3b75d3da2a35fd1146ed20ebc8ed50ba4b54dd6d82509935e934bbb42eb0c
MD5 e75a3e5b7a0f401ed50fd2505230ff7b
BLAKE2b-256 d6aeebcfe930ad45f977a5857e109969da30e015d9c8d212418d2fb08282a63e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page