Dithergo 在Go中实现的各种抖动算法

Dithergo 一个简单的Go库实现各种抖动算法以产生半色调图像。 它支持彩色和单色图像输出。

Go 图片处理

访问GitHub主页

共115Star

详细介绍

dithergo

Dithergo is a simple Go library implementing various dithering algorithms to produce halftone images. It supports color and monochrome image outputs.

Currently the library includes the following dithering algorithms: Floyd Steinberg, Atkinson, Burkes, Stucki, Sierra-2, Sierra-3, Sierra-Lite. All of these algorithms have something in common: they diffuse the error in two dimensions, but they always push the error forward, never backward.

We can represent this with the following diagram:

         X   7   5 
 3   5   7   5   3
 1   3   5   3   1

       (1/48)

where X represent the current pixel processed. The fraction at the bottom represents the divisor for the error. Above is the the Floyd-Steinberg dithering algorithm transposable into the following Go code:

ditherers = []dither.Dither{
	dither.Dither{
		"FloydSteinberg",
		dither.Settings{
			[][]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 },
			},
			float32(multiplier),
		},
	},
}

You can plug in any dithering algorithm, so the library can be further extended.

Installation

go get -u github.com/esimov/dithergo

Running

Type go run cmd/main.go --help to check all the supported commands. The library supports the following commands:

Usage of commands:
  -export string
    	Generate the color and greyscale dithered images. Options: 'all', 'color', 'mono' (default "all")
  -grayscale
    	Convert image to grayscale (default true)
  -multiplier float
    	Error multiplier (default 1.18)
  -outputdir string
    	Directory name, where to save the generated images (default "output")
  -treshold
    	Export treshold image (default true)

You can run all the dithering algorithms at once, or you can run a specific one from the cmd directory.

Examples:

Input

The below images are generated with the default options using Michelangelo's David statue as sample image.

Color Monochrome
Atkinson Atkinson
Burkes Burkes
Floyd-Steinberg Floyd-Steinberg
Sierra-2 Sierra-2
Sierra-3 Sierra-3
Sierra-Lite Sierra-Lite
Stucki Stucki

License

Copyright © 2018 Endre Simo

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

推荐源码