ascii

package module
v0.0.0-...-7d5ab38 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2023 License: BSD-3-Clause Imports: 9 Imported by: 0

README

Go-Ascii

Overview

Library which renders images using ascii characters

Install

go get github.com/fiwippi/go-ascii

Usage

Errors ignored for brevity

Default
// Read in an image...
img := ...

// Generate the ascii version
asciiImg, _ := ascii.Convert(img)
With Interpolation
// Given a slice of images
images := ...

// Generate the interpolated images
mem := &ascii.Memory{}
for _, img := range images {
    asciiImg, _ := ascii.ConvertWithOpts(img, ascii.Interpolate(Mem))
}
With Custom Font

Warning go-ascii expects monospace fonts!

// Read in the font file
data, _ := os.ReadFile("font_file.ttf")

// Parse the font
font, _ := opentype.Parse(data)

// Perform the conversion
asciiImg, _ := ascii.ConvertWithOpts(img, ascii.Font(font))

Examples

example 1

example 2

To convert videos check out the example at examples/video

example 3

License

BSD-3-Clause

Documentation

Index

Constants

View Source
const (
	CharsetLimited  Charset = " .:-=+*#%@"
	CharsetExtended         = ".'`^\",:;Il!i><~+_-?][}{1)(|\\/tfjrxnuvczXYUJCLQ0OZmwqpdbkhao*#MW&8%B@$"
	CharsetBlock            = "█"
)

Variables

This section is empty.

Functions

func Convert

func Convert(img image.Image) (image.Image, error)

Convert renders the given image using ascii characters using default options. If you want to select specific configuration parameters such as font size you should use ConvertWithOpts

func ConvertWithOpts

func ConvertWithOpts(img image.Image, opts ...Option) (image.Image, error)

ConvertWithOpts renders the given image using ascii characters.

You can pass in Option(s) to configure the settings which the renderer users.

Types

type Charset

type Charset string

Charset is the set of characters which go-ascii uses to convert a pixel into an ascii character.

The leftmost character is used to render the darkest pixel (black) and the rightmost is used for the lightest pixel (white). As the brightness the index of the character used increases to the right

type Memory

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

Memory stores the brightnesses of pixels at specific coordinates.

If passed in to ConvertWithOpts using the Interpolate option for successive calls, go-ascii interpolates the character used to represent the pixel.

This is useful for converting multiple frames in a video where you might want the gradual change between characters to be less pronounced

func (*Memory) Reset

func (m *Memory) Reset()

Reset clears any data the Memory may hold so that if used again with Interpolate, no interpolation would occur for the first call

type Option

type Option func(args *options) error

Option is a function which is supplied to ConvertWithOpts and which mutates the settings which the convertor uses.

Options include:

  • CSet -> Character set
  • FontPts -> Font size in pts
  • Font -> Font
  • Interpolate -> Interpolation of characters

func CSet

func CSet(c Charset) Option

CSet changes the character set that the convertor uses

func Font

func Font(f *opentype.Font) Option

Font changes which font the convertor uses, fonts are expected to be monospace

func FontPts

func FontPts(pts float64) Option

FontPts changes the font size the convertor renders the characters in which is specified in points (pts) as opposed to pixels

func Interpolate

func Interpolate(mem *Memory) Option

Interpolate is able to interpolate the character used if multiple ConvertWithOpts calls are used with this option and if a valid Memory struct is provided.

This makes the change in characters less pronounced between successive images, this is useful if you are converting successive frames of a video.

Jump to

Keyboard shortcuts

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