octreequant

package module
v0.0.0-...-2675d82 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2024 License: MIT Imports: 2 Imported by: 0

README

octreequant GoDoc

A simple library for converting images to paletted images (color quantization) fast.

Quantizing an image takes a substantial amount of time, in the order of a second, so using an optimized algorithm/library matters. This library uses the fastest mainstream quantization algorithm.

Main quantization algorithms:

  • Slowest: Generating a palette, then "brute-forcing" all the possible palettes for each pixel (what Go's draw.Draw does)
  • Fast: Generating clusters of pixels, then cutting the biggest in half on their median element until the image has <= N clusters (what https://github.com/soniakeys/quant does)
  • Fastest: Adding all the pixel colors to an octree (tree with 8 children per node), then pruning the tree bottom-up until it has <= N leafs (this repo)

In practice, this library can be typically used to render SIXEL images fast.

On a typical system, encoding a 1000x1000 image takes:

  • 2s with the brute-force algorithm
  • 0.8s with the median algorithm
  • 0.3s with this repo's algorithm

Usage

go get github.com/delthas/octreequant@master

The API is well-documented in its GoDoc

To convert an image to a paletted image with a 256-color palette:

var in image.Image // = ...
out := octreequant.Paletted(in, 256)

Example

Before quantization: Original image

After quantization: Quantized image

License

MIT

Documentation

Overview

Package octreequant implements functions to convert images to paletted images.

Images are quantized (converted) using the Octree Color Quantization algorithm, which is the fastest, but provides slightly worse results, quality-wise, than a median cut clustering quantizer, or a brute-force quantizer.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Paletted

func Paletted(img image.Image, colors int) *image.Paletted

Paletted quantizes an image and returns a paletted image, with a palette up to the specified color count.

Types

This section is empty.

Jump to

Keyboard shortcuts

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