Skip to main content

smartcrop implementation in Python

Project description

image

smartcrop.py

smartcrop implementation in Python.

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

image{width="50.0%"}

image{width="25.0%"}

image{width="50.0%"}

Requirements

Python requirements are defined in pyproject.toml.

Moreover the packages such as PyGObject and GExiv2 requires additional system packages that has to be installed using your OS package manager. Please check requirements.install.apt for that purpose (tuned on Ubuntu 22.04).

Installation

sudo apt-get install <...> # list from requirements.install.apt
pip3 install -U pip setuptools wheel # optional but recommended
pip3 install smartcrop

or directly from GitHub:

sudo apt-get install <...> # list from requirements.install.apt
pip3 install -U pip setuptools wheel # optional but recommended
pip install -e git+git://github.com/hhatto/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
  OUTPUT_FILE           Output image file

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

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, indent=2))

Testing

Install dependencies for testing, then call pytest:

pip3 install smartcrop[test]
pytest

License

MIT

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

smartcrop-0.4.0.tar.gz (6.6 MB view hashes)

Uploaded Source

Built Distribution

smartcrop-0.4.0-py3-none-any.whl (8.6 kB view hashes)

Uploaded Python 3

Supported by

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