-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
177 lines (155 loc) · 4.23 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"bufio"
"fmt"
"image"
"image/color"
"image/draw"
_ "image/jpeg"
"image/png"
_ "image/png"
"os"
"golang.org/x/image/font"
"golang.org/x/image/font/basicfont"
"golang.org/x/image/math/fixed"
)
const ASCII_ARRAY = "@%#*+=-:. "
var isColourful = false
var colourMatrix [][]color.Color
func displayHelp() {
fmt.Println("Usage: go run main.go <image> <output-txt-name> <output-image-name>")
if len(os.Args) == 2 {
if os.Args[1] == "help" {
fmt.Println("This program converts an image to an ascii art text file and an image")
fmt.Println("Usage: go run main.go <image> <output-txt-name> <output-image-name> -<c>")
fmt.Println("-c makes the image colourful")
fmt.Println("Example: go run main.go image.jpg output.txt output.png")
}
}
}
func main() {
if len(os.Args) == 5 {
if os.Args[4] == "-c" {
isColourful = true
} else {
displayHelp()
return
}
if len(os.Args) < 4 {
displayHelp()
return
}
}
imagePath := os.Args[1]
outputTxtPath := os.Args[2]
outputImgPath := os.Args[3]
outputDirectory := "output/"
if _, err := os.Stat(outputDirectory); os.IsNotExist(err) {
os.Mkdir(outputDirectory, 0755)
}
outputTxtPath = outputDirectory + outputTxtPath
outputImgPath = outputDirectory + outputImgPath
err := imageToASCIIfile(imagePath, outputTxtPath, isColourful)
if err != nil {
panic(err)
}
err = txtToImage(outputTxtPath, outputImgPath, 9, 9, color.Black, color.White, isColourful)
if err != nil {
panic(err)
}
fmt.Printf("image successfully converted to ascii art \n output text file: %s \n output image file: %s \n", outputTxtPath, outputImgPath)
}
// convert a color to an ascii character
func colorToASCII(c color.Color) string {
gray := color.GrayModel.Convert(c)
grayValue := gray.(color.Gray).Y
asciiIndex := int(grayValue) * (len(ASCII_ARRAY) - 1) / 255
return string(ASCII_ARRAY[asciiIndex])
}
// convert an image to an ascii text file
func imageToASCIIfile(imagePath, outputTxtPath string, isColourful bool) error {
file, err := os.Open(imagePath)
if err != nil {
fmt.Println("Error opening image file")
return err
}
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
fmt.Println("Error decoding the image file")
return err
}
outputTxtFile, err := os.Create(outputTxtPath)
if err != nil {
fmt.Println("Error creating the output file")
return err
}
defer outputTxtFile.Close()
bounds := img.Bounds()
colourMatrix = make([][]color.Color, bounds.Max.Y)
if isColourful {
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
colourMatrix[y] = make([]color.Color, bounds.Max.X)
for x := bounds.Min.X; x < bounds.Max.X; x++ {
colourMatrix[y][x] = img.At(x, y)
}
}
}
// write ascii chars to txt file
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
asciiChar := colorToASCII(img.At(x, y))
_, err := outputTxtFile.WriteString(asciiChar)
if err != nil {
fmt.Println("Error while writing to output text file")
return err
}
}
outputTxtFile.WriteString("\n")
}
return nil
}
// convert a text file to an image
func txtToImage(inputFileName string, outputFileName string, charWidth, charHeight int, textColor, bgColor color.Color, isColourful bool) error {
file, err := os.Open(inputFileName)
if err != nil {
return err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil {
return err
}
imgWidth := len(lines[0]) * charWidth
imgHeight := len(lines) * charHeight
img := image.NewRGBA(image.Rect(0, 0, imgWidth, imgHeight))
draw.Draw(img, img.Bounds(), &image.Uniform{C: bgColor}, image.Point{}, draw.Src)
point := fixed.Point26_6{}
face := basicfont.Face7x13
for y, line := range lines {
for x, char := range line {
point.X = fixed.I(x * charWidth)
point.Y = fixed.I((y + 1) * charHeight)
if isColourful {
textColor = colourMatrix[y][x]
}
d := &font.Drawer{
Dst: img,
Src: image.NewUniform(textColor),
Face: face,
Dot: point,
}
d.DrawString(string(char))
}
}
outputFile, err := os.Create(outputFileName)
if err != nil {
return err
}
defer outputFile.Close()
return png.Encode(outputFile, img)
}