smartcrop/smartcrop.py
소개
smartcrop implementation in Python
디자인 및 미디어측정된 스타 증가
- in 24h
- 0
- in 7 days
- 0
- in 15 days
- 0
- in 1 month
- 기록 수집 중
서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.
README.md
Originalsmartcrop.py
smartcrop implementation in Python.
smartcrop finds good crops for arbitrary images and crop sizes, based on Jonas Wagner's smartcrop.js.

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(default1.0). A float array or a modeFimage 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
관련 프로젝트 살펴보기
불러오는 중…
