-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
91 lines (78 loc) · 1.82 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
// -*- Author: Ali -*-
// -*- Info: Image to ASCII Art Converter -*-
package main
import (
"errors"
"fmt"
"image"
"image/color"
"image/jpeg"
"image/png"
"log"
"os"
"strings"
)
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: go run main.go <image_path>")
os.Exit(1)
}
imagePath := os.Args[1]
img, err := loadImage(imagePath)
logFatalIfError(err)
density := []string{" ", ".", "=", "!", "░", "▒", "▓", "█"}
lengthDensity := len(density)
maxDensityIndex := lengthDensity - 1
outputFile, err := os.Create("output.txt")
logFatalIfError(err)
defer outputFile.Close()
for y := img.Bounds().Min.Y; y < img.Bounds().Max.Y; y++ {
for x := img.Bounds().Min.X; x < img.Bounds().Max.X; x++ {
c := color.GrayModel.Convert(img.At(x, y)).(color.Gray)
densityValue := getDensityValue(int(c.Y), lengthDensity)
if densityValue < 0 {
densityValue = 0
}
if densityValue > maxDensityIndex {
densityValue = maxDensityIndex
}
densityString := " " + string(density[densityValue])
fmt.Fprint(outputFile, densityString)
}
fmt.Fprint(outputFile, "\n")
}
fmt.Println("Output saved to output.txt")
}
func loadImage(imagePath string) (image.Image, error) {
var img image.Image
var err error
sl := strings.Split(imagePath, ".")
ext := sl[len(sl)-1]
image, err := os.Open(imagePath)
logFatalIfError(err)
defer image.Close()
switch strings.ToLower(ext) {
case "png":
{
img, err = png.Decode(image)
break
}
case "jpg", "jpeg":
{
img, err = jpeg.Decode(image)
break
}
default:
err = errors.New("unsupported image format")
}
return img, err
}
func getDensityValue(grayScaleValue int, lengthDensity int) int {
const maxColorValue = 255
return grayScaleValue/(maxColorValue/lengthDensity) - 1
}
func logFatalIfError(err error) {
if err != nil {
log.Fatal(err)
}
}