4 releases (2 breaking)

0.4.0 Mar 4, 2025
0.3.1 Oct 23, 2024
0.3.0 Jul 31, 2024
0.2.0 Jul 31, 2024

#248 in Images

Download history 2/week @ 2024-11-19 1/week @ 2024-11-26 5/week @ 2024-12-03 15/week @ 2024-12-10 10/week @ 2024-12-17 16/week @ 2025-02-04 3/week @ 2025-02-11 3/week @ 2025-02-18 13/week @ 2025-02-25 128/week @ 2025-03-04

148 downloads per month
Used in 2 crates (via rustypipe-downloader)

MIT license

56KB
1.5K SLoC

smartcrop.rs

Current crates.io version License CI status

Smartcrop is a content-aware image cropping library that attempts to find the best crop for a given image and aspect ratio.

The original Javascript implementation smartcrop.js was developed by Jonas Wagner.

This is a fork of https://github.com/bekh6ex/smartcrop.rs by Bekh-Ivanov Aleksey, since the original project is unmaintained and does not support newer versions of the image crate.

It also includes a new feature: removing black borders around an image before finding the best crop.

How to use

let height = 1920;
let width = 1080;

let res = smartcrop::find_best_crop(
    &image,
    NonZeroU32::new(height).unwrap(),
    NonZeroU32::new(width).unwrap()
).expect("Failed to find crop");
let c = res.crop;
let cropped = image.crop_imm(c.x, c.y, c.width, c.height);
let scaled = cropped.resize(width, height, image::imageops::FilterType::Lanczos3);

Dependencies

~1.5MB
~30K SLoC