| 1 | # Mosaic |
| 2 | |
| 3 | Mosaic is a tool that allows you to display images in your terminal programs. It |
| 4 | will break down your image to contain a certain number of pixels per cell, then |
| 5 | render those cells. This works best with monospaced fonts. |
| 6 | |
| 7 | > [!NOTE] |
| 8 | > We will be providing a more full-fledged implementation of image |
| 9 | > support for Bubble Tea, but this package is one step in that direction. |
| 10 | |
| 11 | To use Mosaic, you need to... |
| 12 | |
| 13 | 1. Open an image file e.g. `f, err := os.Open(path)` |
| 14 | 2. Decode the image e.g. `img, err := jpeg.Decode(f)` |
| 15 | 3. Create a new Mosaic renderer e.g. `m := mosaic.New().Width(80).Height(40)` |
| 16 | 4. Render the image with Mosaic! e.g. `m.Render(dogImg)` |
| 17 | |
| 18 | Here's a full-blown example: |
| 19 | |
| 20 | ``` go |
| 21 | package main |
| 22 | |
| 23 | import ( |
| 24 | "fmt" |
| 25 | "image" |
| 26 | "image/jpeg" |
| 27 | "os" |
| 28 | |
| 29 | "github.com/charmbracelet/lipgloss" |
| 30 | "github.com/charmbracelet/x/mosaic" |
| 31 | ) |
| 32 | |
| 33 | func main() { |
| 34 | dogImg, err := loadImage("./pekinas.jpg") |
| 35 | if err != nil { |
| 36 | fmt.Print(err) |
| 37 | os.Exit(1) |
| 38 | } |
| 39 | |
| 40 | m := mosaic.New().Width(80).Height(40) |
| 41 | |
| 42 | fmt.Println(lipgloss.JoinVertical(lipgloss.Right, lipgloss.JoinHorizontal(lipgloss.Center, m.Render(dogImg)))) |
| 43 | } |
| 44 | |
| 45 | func loadImage(path string) (image.Image, error) { |
| 46 | f, err := os.Open(path) |
| 47 | defer f.Close() |
| 48 | if err != nil { |
| 49 | return nil, err |
| 50 | } |
| 51 | return jpeg.Decode(f) |
| 52 | } |
| 53 | ``` |
| 54 | |
| 55 | Check out all of the mosaic [examples](https://github.com/charmbracelet/x/tree/main/examples/mosaic)! |
| 56 | |
| 57 | ## Feedback |
| 58 | |
| 59 | We'd love to hear your thoughts on this project. Feel free to drop us a note! |
| 60 | |
| 61 | - [Twitter](https://twitter.com/charmcli) |
| 62 | - [The Fediverse](https://mastodon.social/@charmcli) |
| 63 | - [Bluesky](https://bsky.app/profile/charm.sh) |
| 64 | - [Discord](https://charm.sh/chat) |
| 65 | |
| 66 | ## License |
| 67 | |
| 68 | [MIT](https://github.com/charmbracelet/x/raw/main/LICENSE) |
| 69 | |
| 70 | --- |
| 71 | |
| 72 | Part of [Charm](https://charm.sh). |
| 73 | |
| 74 | <a href="https://charm.sh/"><img alt="The Charm logo" src="https://stuff.charm.sh/charm-badge.jpg" width="400"></a> |
| 75 | |
| 76 | Charm热爱开源 • Charm loves open source • نحنُ نحب المصادر المفتوحة |