Skip to main content

image

Python package

smartcrop.py

smartcrop implementation in Python.

smartcrop finds good crops for arbitrary images and crop sizes, based on Jonas Wagner's smartcrop.js.

image image image

Requirements

Python requirements are defined in pyproject.toml.

Installation

pip3 install -U pip setuptools wheel # optional but recommended
pip3 install smartcrop

or directly from GitHub:

pip3 install -U pip setuptools wheel # optional but recommended
pip install -e git+git://github.com/smartcrop/smartcrop.py.git@master#egg=smartcrop

Usage

Use the basic command-line tool:

$ smartcroppy --help
usage: smartcroppy [-h] [--debug-file DEBUG_FILE] [--width WIDTH] [--height HEIGHT] INPUT_FILE OUTPUT_FILE

positional arguments:
  INPUT_FILE            Input image file (BMP, GIF, JPEG, PNG, TIFF, WEBP)
  OUTPUT_FILE           Output image file, in the format of its extension

options:
  -h, --help            show this help message and exit
  --debug-file DEBUG_FILE
                        Debugging image file
  --width WIDTH         Crop width, at most
  --height HEIGHT       Crop height, at most

Processing an image:

smartcroppy --width 300 --height 300 tests/images/business-work-1.jpg output.jpg --debug-file debug.jpg

Or use the module it in your code (this is a really basic example, see examples/ and smartcrop/cli.py for inspiration):

import json
import sys

import smartcrop
from PIL import Image

image = Image.open(sys.argv[1])
cropper = smartcrop.SmartCrop()
result = cropper.crop(image, 100, 100)
print(json.dumps(result['top_crop'], indent=2))

Boost

A boost biases crop selection toward regions of interest. It is a weight map, a NumPy array or a Pillow image, added to the image features before the crops are scored. Any library can produce it: a saliency or segmentation model, a face detector's boxes drawn with numpy, ImageDraw, skimage.draw or OpenCV.

  • Resolution: any, with the image's aspect ratio. The map is averaged down to the features grid (score_down_sample), and follows the image through prescaling.
  • Values: the same units as the other features, scaled by SmartCrop.boost_weight (default 1.0). A float array or a mode F image is taken as-is; any other image mode, such as an alpha channel, is scaled to 0..1.

The RectangleBoost helper draws rectangles in image pixels. Boosts add up with +, and overlapping weights accumulate. Any iterable of boosts, such as a detector's boxes, adds up with smartcrop.BoostSum(boosts), an all-zero map when it is empty. image | boost draws them into a weight map sized to the image:

import smartcrop
from PIL import Image

image = Image.open('photo.jpg')
face = smartcrop.RectangleBoost(x=120, y=40, width=80, height=80, weight=1.0)
eyes = smartcrop.RectangleBoost(x=130, y=60, width=60, height=20, weight=0.5)

cropper = smartcrop.SmartCrop()
result = cropper.crop(image, 300, 300, boost=image | face + eyes)
print(result['top_crop'])

Subclass smartcrop.Boost and implement paint() to draw other shapes.

The alpha channel of a transparent image boosts its opaque content:

cropper = smartcrop.SmartCrop(alpha_strategy='composite')
result = cropper.crop(image, 300, 300, boost=image.getchannel('A'))

Testing

Install dependencies for testing, then call pytest:

pip3 install smartcrop[test]
pytest

License

MIT

Metadata

Release files for smartcrop 0.6.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 smartcrop 0.6.0
File Size Uploaded Provenance Actions
smartcrop-0.6.0.tar.gz 6.6 MB Without provenance Details for smartcrop-0.6.0.tar.gz

Built distribution (wheel)

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

Total release size: 6.7 MB

Release files / smartcrop-0.6.0.tar.gz

Download URL smartcrop-0.6.0.tar.gz
Size 6.6 MB
Tags Source
SHA-256 checksum
How to use checksums
60f2bb5ac88192a5e1e82ffa6edeb832c3eb4cf3ce1ebc73cf7872ac6145a150
BLAKE2b-256 checksum
How to use checksums
88cb415b702eabd91219fbbcd77a748ddb46bd983b104816d85c1834014d611a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.10

Release files / smartcrop-0.6.0-py3-none-any.whl

Download URL smartcrop-0.6.0-py3-none-any.whl
Size 13.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fa5ee291464fccee0992598c7ffacb8fedc4cd597c6d04e724be8570e486801a
BLAKE2b-256 checksum
How to use checksums
c0842a5a5ae3c5e4046848bddecd0e88d500cbd9a56b5e141ca400d323b05738
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.10

Security for smartcrop 0.6.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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