Rust implementation of k-d tree to efficiently perform color quantization to predefined sets
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


See the article for this repo here:

An implementation of a k-d tree to quantize color images down to a defined set.

After cloning, use cargo build --release to download dependencies and compile. From there, you can invoke the binary directly in ./target/release/chromatic_confiner <args> or you can use cargo run --release -- <args>.

There are three required files to be passed into the program.

-c <FILE> The color specification file to be used in restricting the image. This should be formatted as a list of r,g,b color values, with one color per line. See for an example.

-i <FILE> The input image to quantize

-o <FILE> The path of the resultant output image (this should end in .png).

You can pass the -n flag to run the slow version of this algorithm. This is the non k-d tree version, which runs much more slowly than the k-d tree version when the number of colors is large. See for a more detailed comparison.

Example usage (can be run immediately after cloning):

cargo run --release -- -c color_files/1kcolors.colors -i images/bridge_4096_2304.jpg -o image.png will produce this image (in ./image.png, the image you see here is scaled down to reduce load time of this repo. Image provided courtesy of Julian Herzog, see LICENSE for more information):

Exmaple output, scaled to 25% size, image of bridge quantized to 1024 random colors

If you would like to try out this code on huge images, I recommend searching for images on Wikimedia Commons. Here is a 30,000 x 23,756 pixel image of Starry Night, for example.