gotuipic
Convert an image to UTF-8 art: colored terminal pictures in pure Go
(no CGO; the core is standard-library-only, and golang.org/x/image is
linked only if you import the opt-in formats package). Two styles:
- sextant (default) - two colors in six subpixels per cell using the
U+1FB00 block-sextant glyphs, split per cell by an exact minimum-error
partition;
- ascii - one truecolor glyph per cell from a luminance ramp, picked so
the cell’s own color actually hits the target brightness; needs no
non-ASCII glyph, so it survives every terminal, font, and pipe.
The input can be a file, an io.Reader, a []byte or an image.Image.
The output is an Art - a grid of cells you can print as ANSI, flatten to
runes, write to any io.Writer, or hand to a TUI toolkit.
go get github.com/radix29/gotuipic # Go 1.27+; core has zero dependencies
Quick start
art, err := gotuipic.ConvertFile("tools/stylepreview/testdata/sample.jpg",
gotuipic.WithWidth(80), // columns
gotuipic.WithMode(gotuipic.ModeSextant), // or ModeASCII
gotuipic.WithColor(gotuipic.ColorTrueColor), // 256/16/None too
)
if err != nil {
return err
}
fmt.Print(art) // ANSI; art.Plain() for runes; art.Cells for TUIs
Or run the example program (a cmd/gotuipic CLI is planned):
go run ./examples/basic -w 100 tools/stylepreview/testdata/sample.jpg | less -R
The library never reads $TERM, $COLORTERM or $NO_COLOR, queries the
terminal size, or checks for a TTY - pass a width, and pick a profile
(gotuipic.ProfileFromEnv helps programs that want auto-detection: the
caller passes os.Getenv, so the library itself still reads nothing. An
unset or empty TERM, dumb and off give ColorNone.)
Getting an image in
Four entry points, same options, same *Art back. The format is sniffed
from the stream header, never from a name or extension, and nothing needs
Seek or Stat:
| Function |
Takes |
Use for |
ConvertFile(path, ...) |
file path |
an image on disk |
ConvertReader(r, ...) |
io.Reader |
stdin, pipes, sockets, HTTP bodies, gzip.Reader, embed.FS files, tar entries - anything |
ConvertBytes(b, ...) |
[]byte |
data already whole in memory: a buffered response body, fs.ReadFile, a base64-decoded blob, a WebSocket frame payload |
Convert(img, ...) |
image.Image |
images you decoded yourself, drew in code, or got from a library gotuipic knows nothing about |
From a byte stream (io.Reader)
// Standard input: `gotuipic - < cat.png`, or `curl -sL URL | gotuipic -`
art, err := gotuipic.ConvertReader(os.Stdin, gotuipic.WithWidth(100))
// An HTTP response body. The stream is read once into memory (capped by
// WithMaxBytes) and decoded from its header, with no buffer of yours; Go's transport already inflates gzip
// responses it asked for, so this reads real PNG/JPEG bytes.
resp, err := http.Get("https://picsum.photos/800/600")
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return errors.New(resp.Status)
}
art, err := gotuipic.ConvertReader(resp.Body,
gotuipic.WithWidth(100),
gotuipic.WithMaxPixels(32<<20)) // header is checked before any pixel is allocated
// A producer goroutine feeding a pipe - no temp file; the receiving side buffers it in memory, capped by WithMaxBytes.
r, w := io.Pipe()
go func() { _ = png.Encode(w, img); w.Close() }()
art, err := gotuipic.ConvertReader(r, gotuipic.WithWidth(80))
It works the same for gzip.NewReader(f), archive/tar entry bodies,
net.Conn, io.LimitReader(r, n) when you want a hard byte cap, or an
embed.FS file handle. See examples/stream for a complete URL-or-stdin
program.
From bytes in memory ([]byte)
data, err := fs.ReadFile(files, "static/cat.png") // or base64.StdEncoding.DecodeString(s)
if err != nil {
return err
}
art, err := gotuipic.ConvertBytes(data, gotuipic.WithWidth(80))
The slice is only read, never retained, so you may reuse the buffer.
From a decoded image (image.Image)
art, err := gotuipic.Convert(img, gotuipic.WithWidth(80))
// Or draw one from scratch - gradients, charts, noise, a captured frame.
im := image.NewNRGBA(image.Rect(0, 0, 64, 32))
/* ... fill im ... */
art, err := gotuipic.Convert(im, gotuipic.WithMode(gotuipic.ModeASCII))
PNG, JPEG and GIF are registered by the core. BMP, TIFF and WebP come from
the opt-in formats package, imported for its side effect:
import _ "github.com/radix29/gotuipic/formats" // bmp, tiff, webp
It blank-imports the golang.org/x/image decoders, so a program that never
imports it never links x/image and the core stays standard-library-only.
Anything else that plugs into the standard image registry works the same
way - ConvertFile, ConvertReader, ConvertBytes and Decode all pick it
up automatically.
Decode once, render many times
Decode/DecodeBytes run the same guarded decode (WithMaxBytes,
WithMaxPixels from the header) and return the image.Image plus its
format name. A program that renders one picture repeatedly - a TUI viewer
re-fitting on resize, zooming, or switching modes - decodes once, flattens
once (Flatten composites over WithBackground into the opaque NRGBA the
pipeline works on, which Convert then passes through without touching a
pixel), and calls Convert per rendering:
img, format, err := gotuipic.DecodeBytes(blob) // "png", "jpeg", "bmp", ...
flat, err := gotuipic.Flatten(img)
// on every resize:
art, err := gotuipic.Convert(flat, gotuipic.WithWidth(cols), gotuipic.WithHeight(rows))
``` Animated GIFs give the first frame for
now; `ConvertFrames` is planned.
## Getting the art out
`Art` is the product - a `Cols x Rows` grid of `Cell{Rune, FG, BG}`. ANSI
text is one rendering of it, not the only one.
```go
fmt.Print(art) // ANSI truecolor/256/16 SGR; Art.String() is the same text
plain := art.Plain() // runes only, no escape sequences, paste-able anywhere
_, _ = art.WriteTo(os.Stdout) // io.WriterTo: a file, a socket, an http.ResponseWriter, a log
_, _ = io.Copy(w, strings.NewReader(art.String())) // or anything that wants a Reader
Writing art somewhere is the mirror image of reading an image in:
func handleIndex(w http.ResponseWriter, r *http.Request) {
art, err := gotuipic.ConvertReader(r.Body, gotuipic.WithWidth(60),
gotuipic.WithColor(gotuipic.ColorANSI256))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
_, _ = art.WriteTo(w)
}
For TUIs, read the cells instead of parsing escape sequences back out of a
string. Every glyph is single-width, so the column index equals the rune
index:
for row := 0; row < art.Rows; row++ {
for col := 0; col < art.Cols; col++ {
c := art.At(col, row) // never panics: out-of-range gives the zero Cell
// c.Rune, c.FG, c.BG is all a screen backend needs. c.BG.A == 0
// means "no background": paint with the terminal's own, not black.
screen.Set(col+offX, row+offY, c)
}
}
An Art is not mutated after Convert returns, so it is safe to render the
same one from several goroutines.
Options
| Option |
Default |
Effect |
WithWidth(n) / WithHeight(n) |
80 x auto |
output size in cells |
WithFit(FitContain|FitStretch|FitCrop) |
FitContain |
what to do when both width and height are given |
WithCellAspect(w/h) |
0.5 |
cell shape; set it to match a font that is not 1:2 |
WithMode(ModeSextant|ModeASCII) |
ModeSextant |
glyph style |
WithColor(ColorTrueColor|ColorANSI256|ColorANSI16|ColorNone) |
ColorTrueColor |
color depth |
WithRamp(" .:-=+*#%@") |
that ramp |
ASCII glyphs, dark to light |
WithInvert(true) |
false |
reverse the ASCII ramp, for terminals that show glyphs as ink on a light background; ASCII mode only, no effect on sextant |
WithBrightness(0) |
0 |
shift all channels by b*255, b in [-1, 1]; 0 is unchanged |
WithContrast(1) |
1 |
scale distance from mid-gray, c >= 0; 1 is unchanged |
WithBackground(c) |
black |
what transparent pixels composite onto (nil keeps the default) |
WithMaxPixels(n) |
64 Mi pixels |
decompression-bomb guard, checked on the header before decoding; also applied to Convert(image.Image) |
WithMaxCells(n) |
1 Mi cells |
cap on output Cols*Rows; larger requests are ErrInvalidOption |
WithMaxBytes(n) |
64 MiB |
cap on input read by ConvertReader/ConvertBytes/ConvertFile; longer is ErrTooLarge |
Bad option values are collected and returned together as one
ErrInvalidOption (errors.Is works), not at construction.
Gallery
The three example photos that ship in tools/stylepreview/testdata (all 800x600), each shown as the original and then as ModeSextant output in a truecolor terminal, 100 columns wide (-w 100):
Cat
Original:

Sextant, go run ./examples/basic -w 100 tools/stylepreview/testdata/sample.jpg:

Fruit
Original:

Sextant, go run ./examples/basic -w 100 tools/stylepreview/testdata/sample-fruit.jpg:

Landscape
Original:

Sextant, go run ./examples/basic -w 100 tools/stylepreview/testdata/sample-landscape.jpg:

tools/stylepreview writes full-color montages of every style it measured,
shipped and rejected, into tools/stylepreview/preview-*.png.
Runnable examples
| Path |
Shows |
examples/basic |
file or stdin → art, with every flag (-w -h -mode -color -fit -ramp -invert) |
examples/stream |
URL, pipe or file → art: the io.Reader path, HTTP status handling, MaxPixels |
example_test.go |
short // Output:-checked Go examples for godoc: Convert, ConvertReader, ConvertBytes, ConvertFile, Art.WriteTo, Art.Cells |
The smallest complete program:
package main
import (
"io"
"os"
"github.com/radix29/gotuipic"
)
func main() {
// `cat cat.png | go run . -`, or `go run . cat.png`
var src io.Reader = os.Stdin
if len(os.Args) > 1 && os.Args[1] != "-" {
f, err := os.Open(os.Args[1])
if err != nil {
panic(err)
}
defer f.Close()
src = f
}
art, err := gotuipic.ConvertReader(src,
gotuipic.WithWidth(80),
gotuipic.WithColor(gotuipic.ProfileFromEnv(os.Getenv)))
if err != nil {
panic(err)
}
_, _ = art.WriteTo(os.Stdout)
}
go run ./examples/basic -w 100 tools/stylepreview/testdata/sample.jpg | less -R
go run ./examples/basic -mode ascii -color 256 tools/stylepreview/testdata/sample-fruit.jpg
go run ./examples/stream -mode ascii https://picsum.photos/800/600 | less -R
go doc github.com/radix29/gotuipic # API tour
Terminal notes
-
sextant needs a terminal font that covers the U+1FB00 block-sextant
range, and plenty of monospace fonts do not. The library cannot check for
you (it never sees the terminal), so if the glyphs come out as boxes or
blanks, switch to ModeASCII: it emits only ASCII glyphs and always
renders.
-
Color detection is the caller’s job, because the library never reads
the environment:
prof := gotuipic.ProfileFromEnv(os.Getenv) // NO_COLOR, COLORTERM, TERM
-
Pipe output to a file or a pager safely: ColorNone emits no escape
sequences at all, and Art.Plain() is always escape-free.
Errors and limits
_, err := gotuipic.ConvertReader(r)
switch {
case err == nil: // rendered
case errors.Is(err, gotuipic.ErrInvalidOption): // bad option values, reported together
case errors.Is(err, gotuipic.ErrTooLarge): // header claims more pixels than MaxPixels
case errors.Is(err, image.ErrFormat): // not a decodable image (nil/empty/garbage)
}
MaxPixels is evaluated from the image header before any pixel buffer is
allocated, so a 100000x100000 PNG header is rejected cheaply.
Status
Alpha, untagged. Design: ARCHITECTURE.md; mode measurements (why sextant + ascii won):
tools/stylepreview/. MIT license.
评论