colorquant

Go library for color quantization and dithering

Github星跟蹤圖

Colorquant

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 the core draw method does provide an error quantization algorithm, it does not implement the support for quantization level (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 get 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 dither

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 dither

The same, but this time using a dithering method:

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

where ditherer is 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, , , ## License

This software is distributed under the MIT license found in the LICENSE file.

主要指標

概覽
名稱與所有者esimov/colorquant
主編程語言Go
編程語言Go (語言數: 1)
平台
許可證MIT License
所有者活动
創建於2017-03-19 18:58:02
推送於2021-01-21 14:33:23
最后一次提交2021-01-21 16:33:22
發布數1
最新版本名稱v1.0.0 (發布於 2017-09-28 14:44:35)
第一版名稱v1.0.0 (發布於 2017-09-28 14:44:35)
用户参与
星數85
關注者數5
派生數5
提交數36
已啟用問題?
問題數0
打開的問題數0
拉請求數0
打開的拉請求數0
關閉的拉請求數0
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?