mosaic

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2025 License: MIT Imports: 8 Imported by: 0

README

Mosaic

Mosaic is a tool that allows you to display images in your terminal programs. It will break down your image to contain a certain number of pixels per cell, then render those cells. This works best with monospaced fonts.

[!NOTE] We will be providing a more full-fledged implementation of image support for Bubble Tea, but this package is one step in that direction.

To use Mosaic, you need to...

  1. Open an image file e.g. f, err := os.Open(path)
  2. Decode the image e.g. img, err := jpeg.Decode(f)
  3. Create a new Mosaic renderer e.g. m := mosaic.New().Width(80).Height(40)
  4. Render the image with Mosaic! e.g. m.Render(dogImg)

Here's a full-blown example:

package main

import (
	"fmt"
	"image"
	"image/jpeg"
	"os"

	"git.core.id/coreid/lipgloss"
	"git.core.id/coreid/x/mosaic"
)

func main() {
	dogImg, err := loadImage("./pekinas.jpg")
	if err != nil {
		fmt.Print(err)
		os.Exit(1)
	}

	m := mosaic.New().Width(80).Height(40)

	fmt.Println(lipgloss.JoinVertical(lipgloss.Right, lipgloss.JoinHorizontal(lipgloss.Center, m.Render(dogImg))))
}

func loadImage(path string) (image.Image, error) {
	f, err := os.Open(path)
	defer f.Close()
	if err != nil {
		return nil, err
	}
	return jpeg.Decode(f)
}

Check out all of the mosaic examples!

Feedback

We'd love to hear your thoughts on this project. Feel free to drop us a note!

License

MIT


Part of Charm.

The Charm logo

Charm热爱开源 • Charm loves open source • نحنُ نحب المصادر المفتوحة

Documentation

Overview

Package mosaic provides a unicode image renderer.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Render

func Render(img image.Image, width int, height int) string

Render mosaic with default values.

Types

type Mosaic

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

Mosaic represents a Unicode image renderer.

Example:

```go
art := mosaic.New().Width(100). // Limit to 100 cells
    Scale(mosaic.Fit).          // Fit to width
    Render()
```

func New

func New() Mosaic

New creates and returns a [Renderer].

func (Mosaic) Dither

func (m Mosaic) Dither(dither bool) Mosaic

Dither sets the dither level on Mosaic.

func (Mosaic) Height

func (m Mosaic) Height(height int) Mosaic

Height sets the maximum height the image can have. Defaults to the image height.

func (Mosaic) IgnoreBlockSymbols

func (m Mosaic) IgnoreBlockSymbols(fgOnly bool) Mosaic

IgnoreBlockSymbols set UseFgBgOnly on Mosaic.

func (Mosaic) InvertColors

func (m Mosaic) InvertColors(invertColors bool) Mosaic

InvertColors whether to invert the colors of the mosaic image.

func (*Mosaic) Render

func (m *Mosaic) Render(img image.Image) string

Render renders the image to a string.

func (Mosaic) Scale

func (m Mosaic) Scale(scale int) Mosaic

Scale sets the [ScaleMode] on Mosaic.

func (Mosaic) Symbol

func (m Mosaic) Symbol(symbol Symbol) Mosaic

Symbol sets the mosaic symbol type.

func (Mosaic) Threshold

func (m Mosaic) Threshold(threshold int) Mosaic

Threshold sets the threshold level on Mosaic. It expects a value between 0-255, anything else will be ignored.

func (Mosaic) Width

func (m Mosaic) Width(width int) Mosaic

Width sets the maximum width the image can have. Defaults to the image width.

type Symbol

type Symbol uint8

Symbol represents the symbol type to use when rendering the image.

const (
	All Symbol = iota
	Half
	Quarter
)

Symbol types.

Source Files

  • mosaic.go

Jump to

Keyboard shortcuts

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