Crates.io | palette_extract |
lib.rs | palette_extract |
version | 0.1.0 |
source | src |
created_at | 2022-03-06 18:56:03.17068 |
updated_at | 2022-03-06 18:56:03.17068 |
description | A Rust port of Leptonica's modified media cut quantization algorithm |
homepage | https://github.com/DigitalZebra/palette-extract-rs |
repository | https://github.com/DigitalZebra/palette-extract-rs.git |
max_upload_size | |
id | 544630 |
size | 741,331 |
A Rust lib for extracting a color palette from an image!
Based on a port of Leptonica's modified median cut quantization algorithm.
This Rust implementation of modified median cut quantization (MMCQ) is adapted from Kazuki Ohara's ColorThiefSwift.
To use, add the following to Cargo.toml
under [dependencies]
:
palette_extract = "0.1.0"
Using the library consists of calling get_palette_rgb
or get_palette_with_options
with a set of RGB or RGBA pixels represented as a u8
slice.
A minimal example using 4 red pixels represented in RGB looks like this:
use palette_extract::{get_palette_rgb, Color};
fn main() {
let pixels: [u8; 12] = [255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0];
let r = get_palette_rgb(&pixels);
assert_eq!(r.len(), 1);
assert_eq!(r[0], Color::new(252, 4, 4));
}
Here's one way to extract the color palette of an image by leveraging the image
crate to read and decode an image file (see full working example in examples
directory):
let image_path = "./path/to/image.jpg";
// open and decode the image using the `image` crate
let img = image::open(image_path).unwrap();
// grab a reference to the underlying pixels/RGB buffer
let pixels = img.as_bytes();
// extract the color palette
let palette = get_palette_rgb(&pixels);
// output the extracted color palette
palette.iter().for_each(|x| println!("{:?}", x));
More usage examples can be found in the examples
directory!
The paper describing the MMCQ algorithm: http://leptonica.org/papers/mediancut.pdf
A great post on different ways to extract color palettes: Color quantization
Image credit: João Pacheco ↩