Crates.io | rscolorq |
lib.rs | rscolorq |
version | 0.2.0 |
source | src |
created_at | 2020-10-23 21:47:55.02404 |
updated_at | 2021-03-30 23:08:06.636269 |
description | Spatial color quantization, a Rust port of `scolorq`. |
homepage | https://github.com/okaneco/rscolorq |
repository | https://github.com/okaneco/rscolorq |
max_upload_size | |
id | 304847 |
size | 121,519 |
Rust port of Derrick Coetzee's scolorq
, based on the 1998 paper
"On spatial quantization of color images" by Jan Puzicha, Markus Held, Jens
Ketterer, Joachim M. Buhmann, & Dieter Fellner. Spatial quantization is
defined as simultaneously performing halftoning (dithering) and color
quantization (limiting the colors in an image). For more information, visit
the original implementation's website.
The algorithm is excellent for retaining image detail and minimizing visual distortions for color palettes in the neighborhood of 4, 8, or 16 colors, especially as the image size is reduced. It combines limiting the color palette and dithering the image into a simultaneous process as opposed to sequentially limiting the colors then dithering. Colors are chosen based on their context in the image, hence the "spatial" aspect of spatial color quantization. As in Pointillism, the colors are selected based on their neighbors to mix as an average illusory color in the human eye.
To use as a library, add the following to your Cargo.toml
; add the
palette_color
feature to enable Lab color quantization. Executable builds can
be found at https://github.com/okaneco/rscolorq/releases.
[dependencies.rscolorq]
version = "0.2"
default-features = false
Images are best viewed at 100% magnification.
Top row: Original image, RGB 2 colors
Bottom row: RGB 4 colors, RGB 8 colors
rscolorq -i mandrill.jpg -o mandrill-rgb2.png -n 2 --auto -s 0 --iters 5
rscolorq -i mandrill.jpg -o mandrill-rgb4.png -n 4 --auto -s 0 --repeats 3
rscolorq -i mandrill.jpg -o mandrill-rgb8.png -n 8 --auto -s 0 --iters 5
The --iters
and --repeats
options can be used to increase their values over
the default to improve the quality of output. --auto
sets the dithering
level based on the image size and desired palette size. The --seed
or -s
option sets the random number generator seed; otherwise, it's seeded randomly.
Palette swatches can be generated by passing --op
plus a filename. --width
and --height
can be passed to specify the width and height of the resulting
palette image. The following swatches are the colors that comprise 4 and 8 color
dithered images in the bottom row of the previous image.
rscolorq -i mandrill-resize.jpg --op mandrill-rgb4-pal.png -n 4 --auto -s 0 --repeats 3
rscolorq -i mandrill-resize.jpg --op mandrill-rgb8-pal.png -n 8 --auto -s 0 --iters 5 -p
Passing the --print
or -p
flag will print the hexadecimal colors to the
terminal as seen in the second example above. If no --output
or -o
is
passed, the dithered image will not be saved to a file.
b5c970,191821,b7cbe7,6d7f7b,5db7f0,4e5936,f05131,939bcc
You can supply your own palette to dither with by passing --colors
or
-c
followed by a list of hexadecimal colors as in the following example.
Original image on the left, fixed palette on the right.
rscolorq -i scenic.jpg -o mountain-pal.png -c FFBF82,09717E --auto -s 0 --iters 5`
Top row: Original image, RGB 4 colors, RGB 8 colors.
Bottom row: Lab 4 colors, Lab 8 colors.
rscolorq -i rainbow.png -o rainbow-rgb4.png -n 4
rscolorq -i rainbow.png -o rainbow-rgb8.png -n 8 --iters 8 --repeats 2
rscolorq -i rainbow.png -o rainbow-lab4.png -n 4 --lab
rscolorq -i rainbow.png -o rainbow-lab8.png -n 8 --lab --iters 8 --repeats 2
Left to right: Original image, 2 colors filter size 3, 2 colors filter size 5.
If you get an invalid color error or hex color length error with the command line tool, try enclosing the color string in quotes.
For example, instead of -c 000000,ffffff
use -c '000000,ffffff'
.
This crate is licensed under either
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
Copyright of the original images is property of their respective owners.