-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
55 lines (47 loc) · 899 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"fmt"
"image"
"os"
"golang.org/x/crypto/ssh/terminal"
)
func main() {
// get terminal size
tw, _, err := terminal.GetSize(int(os.Stdout.Fd()))
if err != nil {
errh(err)
}
// loop through image arguments
for _, filename := range os.Args[1:] {
// open image file
f, err := os.Open(filename)
if err != nil {
errh(err)
}
// decode image
img, _, err := image.Decode(f)
if err != nil {
errh(err)
}
// calculate image sizing
iw := img.Bounds().Max.X
ih := img.Bounds().Max.Y
r := (iw / tw) + 1
// print image
for y := 0; y < ih-r; y += r {
for x := 0; x < iw-r; x += r {
c := img.At(x, y)
r, g, b, _ := c.RGBA()
r /= 256
g /= 256
b /= 256
fmt.Printf("\x1b[38;2;%d;%d;%dm█\x1b[0m", r, g, b)
}
fmt.Println()
}
}
}
func errh(err error) {
fmt.Fprintf(os.Stderr, "icat: %s\n", err)
os.Exit(1)
}