Crate smartcrop

Source
Expand description

§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);

Structs§

Crop
Crop position and size
RGB
24bit RGB color
Score
Score used to determine the best crop
ScoredCrop
Crop with attached score

Enums§

Error
Error that occurred during a Smartcrop operation

Traits§

Image
Trait for images to be procressed by Smartcrop
ResizableImage
Trait for images to be resized by Smartcrop

Functions§

find_best_crop
Analyze the image and find the best crop of the given aspect ratio
find_best_crop_no_borders
Analyze the image and find the best crop of the given aspect ratio which excludes black borders
find_border_crop
Check if the image has black borders and return a crop to remove them