Skip to main content

autocrop

CI codecov Documentation PyPI version Downloads

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 if a face was detected:
if cropped_array:
    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.

Detecting faces from video files

You can use autocrop to detect faces in frames extracted from a video. A great way to perform the frame extraction step is with ffmpeg:

mkdir frames faces

# Extract one frame per second
ffmpeg -i input.mp4 -filter:v fps=fps=1/60 frames/ffmpeg_%0d.bmp

# Crop faces as jpg
autocrop -i frames -o faces -e jpg

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)

Misc

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 currently stalled due to the complexity of setting up the workflow with OpenCV. Please leave feedback on issue #7 to see past attempts 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.7 to 3.10
  • 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.

Release files for autocrop 1.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for autocrop 1.3.0
File Size Uploaded
autocrop-1.3.0.tar.gz 160.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for autocrop 1.3.0
File Interpreter ABI Platform
autocrop-1.3.0-py3-none-any.whl Python 3 none any Details

Total release size:318.9 kB

Release files / autocrop-1.3.0.tar.gz

Download URL autocrop-1.3.0.tar.gz
Size 160.0 kB
Tags Source
SHA-256 checksum
How to use checksums
68268ff7382bbf758bad5ad5ba19a3217c62cd3c5c04eab3e2e270a4562f2e91
BLAKE2b-256 checksum
How to use checksums
63262398b55ee12628b7b80f25fcf583de528b1ff948e9387119074b4a47476f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

Release files / autocrop-1.3.0-py3-none-any.whl

Download URL autocrop-1.3.0-py3-none-any.whl
Size 159.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d922724191c581cd2d3ea1649f566d1dae0ace99f042f557fcaaf11055340cc7
BLAKE2b-256 checksum
How to use checksums
d8818a5de656a24627d8f4f58ae24516d4418b2d44fb1ab872ea231961a6ef1b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

Release history Release notifications | RSS feed

This release

1.3.0 This release

2 release files

1.2.0

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.2

1 release file

1.0.1

1 release file

1.0.0

1 release file

0.3.5

1 release file

0.3.4

1 release file

0.3.3

1 release file

0.3.2

1 release file

0.3.1

1 release file

0.3.0

1 release file

0.2.0

2 release files

0.1.5

1 release file

0.1.4

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page