Skip to content

esimov/colorquant

Repository files navigation

Colorquant

CI go.dev reference license

Colorquant is an image / color quantization library written in Go. It can be considered as a replacement for the quantization and dithering part of the draw method from the core image library for various reasons (see below).

The purpose

The purpose of color quantization is to reduce the color palette of an image to a fraction of it's initial colors (usually 256), but to preserve it's representative colors and to elliminate visual artifacts at the same time. Even with the best set of 256 colors, there are many images that look bad. They have visible contouring in regions where the color changes slowly.

To create a smoother transition between colors and to wash out the edges various dithering methods can be plugged in.

Implementation

The implementation is mainly based on the article from Leptonica.

The reason why I opted for a custom quantization and dithering algorithm are twofold:

  • First, even if the core draw method uses an error quantization algorithm, it does not provide support for the quantization level, which means to how many colors we wish to reduce the original image.
  • Second, the dithering method is based exclusively on Floyd-Steinberg dithering method, but there are other dithering algorithm, which can be used (ex. Burkes, Stucki, Atkinson, Sierra etc.).

Installation

go get -u github.com/esimov/colorquant

Running

The library provides a CLI method to generate the quantified images. Type go run cli.go --help to check the supported commands.

Usage of commands:
  -compression int
    	JPEG compression. (default 100)
  -ditherer string
    	Dithering method. (default "FloydSteinberg")
  -no-dither
    	Use image quantizer without dithering.
  -output string
    	Output directory. (default "output")
  -palette int
    	The number of palette colors. (default 256)
  -type string
    	Image type. Possible options .jpg, .png (default "jpg")

The generated images will be exported into the output folder. By default the Floyd-Steinberg dithering method is applied, but if you whish to not use any dithering algorithm use the --no-dither flag.

Usage

➤ Without dithering

This is main method to generate a non-dithered quantified image:

colorquant.NoDither.Quantize(src, dst, numColors, false, true)

where the last paremeter means either to use the library quantization algorithm (if the parameter is true), otherwise use the quantization level provided by the paletted image (if the paramater is false).

➤ With dithering

ditherer.Quantize(src, dst, numColors, true, true)

where ditherer is a struct with the form of:

"FloydSteinberg" : colorquant.Dither{
	[][]float32{
		[]float32{ 0.0, 0.0, 0.0, 7.0 / 48.0, 5.0 / 48.0 },
		[]float32{ 3.0 / 48.0, 5.0 / 48.0, 7.0 / 48.0, 5.0 / 48.0, 3.0 / 48.0 },
		[]float32{ 1.0 / 48.0, 3.0 / 48.0, 5.0 / 48.0, 3.0 / 48.0, 1.0 / 48.0 },
	},
},

Examples

All the examples below are generated using Floyd-Steinberg dithering method with the following command line as an example:

go run cli.go ../input/treefrog.jpg -compression 100 -ditherer FloydSteinberg -palette 128

Number of colors Without dither With Dither
128
256
512
1024

Author

License

Copyright © 2017 Endre Simo

This software is distributed under the MIT license. See the LICENSE file for the full license text.

Sponsor this project

 

Packages

No packages published

Languages