-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
119 lines (92 loc) · 2.1 KB
/
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"errors"
"fmt"
"image"
"image/png"
"log"
"math"
"os"
"sync"
"github.com/crazy3lf/colorconv"
"github.com/nfnt/resize"
)
func main() {
err := run()
if err != nil {
log.Fatal(err)
}
}
func run() error {
char_greyscale := []rune(" .'`^\",:;Il!i><~+_-?][}{1)(|\\/tfjrxnuvczXYUJCLQ0OZmwqpdbkhao*#MW&8%B@$")
if os.Args[1] == "" {
return errors.New("Provide image path")
}
if os.Args[2] == "" {
return errors.New("Provide output path")
}
image, err := readImage(os.Args[1])
if err != nil {
return err
}
new_size := uint(math.Pow(2, 8))
image = resize.Resize(new_size, 0, image, resize.Lanczos3)
bounds := image.Bounds()
start_x, start_y := bounds.Min.X, bounds.Min.Y
end_x, end_y := bounds.Max.X, bounds.Max.Y
width, height := bounds.Dx(), bounds.Dy()
char_grid := make([][]rune, height)
luminance_grid := make([][]int, height)
for i := range luminance_grid {
luminance_grid[i] = make([]int, width)
char_grid[i] = make([]rune, width)
for j := range luminance_grid[i] {
luminance_grid[i][j] = 0
char_grid[i][j] = 0
}
}
minL, maxL := 100, 0
for y := start_y; y < end_y; y++ {
for x := start_x; x < end_x; x++ {
_, _, l := colorconv.ColorToHSL(image.At(x, y))
l_int := int(l * 100)
minL = min(minL, l_int)
maxL = max(maxL, l_int)
luminance_grid[y][x] = l_int
}
}
n := len(char_greyscale) - 1
var wg sync.WaitGroup
for y, row := range luminance_grid {
wg.Add(1)
go func() {
defer wg.Done()
buffer := make([]rune, width)
for x, cell := range row {
i := (int(cell) - minL) * n / (maxL - minL)
buffer[x] = char_greyscale[i]
}
char_grid[y] = buffer
}()
}
wg.Wait()
outFile, err := os.OpenFile(os.Args[2], os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
for _, row := range char_grid {
outFile.WriteString(fmt.Sprintln(string(row)))
}
return nil
}
func readImage(path string) (image.Image, error) {
image_file, err := os.Open(os.Args[1])
if err != nil {
return nil, err
}
image, err := png.Decode(image_file)
if err != nil {
return nil, err
}
return image, nil
}