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...
- Open an image file e.g.
f, err := os.Open(path) - Decode the image e.g.
img, err := jpeg.Decode(f) - Create a new Mosaic renderer e.g.
m := mosaic.New().Width(80).Height(40) - Render the image with Mosaic! e.g.
m.Render(dogImg)
Here's a full-blown example:
package main
import (
"fmt"
"image"
"image/jpeg"
"os"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/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!
We'd love to hear your thoughts on this project. Feel free to drop us a note!
Part of Charm.
Charm热爱开源 • Charm loves open source • نحنُ نحب المصادر المفتوحة
