neuquant

package module
v0.0.0-...-1862aec Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2025 License: MIT Imports: 4 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

NeuQuant

Neural network based color quantizer.

Almost one-to-one port of this rust implementation.

Install

$ go get github.com/dveselov/NeuQuant

Usage

// Open & read image file
file, _ := os.Open("image.png")
reader := bufio.NewReader(file)
img, _ := png.Decode(reader)
bounds := img.Bounds()

// Quantize image to 256 colors and create color palette
q := NewNeuquant(10, 256, img)
palette := q.GetPalette()

// Create new paketted image
dist := image.NewPaletted(bounds, palette)

// Iterate over all pixels in source image
// and set appropriate color in output image
for x := 0; x < bounds.Max.X; x++ {
    for y := 0; y < bounds.Max.Y; y++ {
        r, g, b, a := img.At(x, y).RGBA()
        index := q.indexSearch(b, g, r, a)
        c := q.Colormap[index]
        dist.Set(x, y, color.RGBA{
            R: uint8(c.r >> 8),
            G: uint8(c.g >> 8),
            B: uint8(c.b >> 8),
            A: uint8(c.a >> 8),
        })
    }
}

out, err := os.Create("output.png")
if err != nil {
    panic(err)
}
writer := bufio.NewWriter(out)
err = png.Encode(writer, dist)
if err != nil {
    panic(err)
}
writer.Flush()

Examples

Source image Result image Source filesize Result filesize
50 KB 11 KB
148 KB 56 KB

Documentation

Index

Constants

View Source
const (
	RADIUS_DEC      = 30                   // factor of 1/30 each cycle
	ALPHA_BIASSHIFT = 10                   // alpha starts at 1
	INIT_ALPHA      = 1 << ALPHA_BIASSHIFT // biased by 10 bits
	GAMMA           = 1024.0
	BETA            = 1.0 / GAMMA
	BETAGAMMA       = BETA * GAMMA
	NCYCLES         = 600
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Color

type Color struct {
	// contains filtered or unexported fields
}

type NeuQuant

type NeuQuant struct {
	Network    []*Neuron
	Colormap   []*Color
	Netindex   []int
	Bias       []float64
	Freq       []float64
	Samplefrac int
	Netsize    int
}

func NewNeuquant

func NewNeuquant(samplefrac, colors int, img image.Image) NeuQuant

func (*NeuQuant) GetPalette

func (n *NeuQuant) GetPalette() []color.Color

func (*NeuQuant) IndexSearch

func (n *NeuQuant) IndexSearch(b, g, r, a uint32) int

Search for best matching color

func (*NeuQuant) Init

func (n *NeuQuant) Init(img image.Image)

Initializes the neuronal network and trains it with the supplied data

func (*NeuQuant) Learn

func (n *NeuQuant) Learn(img image.Image)

Main learning loop Note: the number of learning cycles is crucial and the parameters are not optimized for net sizes < 26 or > 256. 1064 colors seems to work fine

type Neuron

type Neuron struct {
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL