-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
118 lines (89 loc) · 2.02 KB
/
main.go
File metadata and controls
118 lines (89 loc) · 2.02 KB
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
package main
import (
"fmt"
"image"
"image/color"
_ "image/gif"
"image/jpeg"
_ "image/png"
"os"
"slices"
"sync"
)
var white = color.RGBA{255, 255, 255, 255}
var black = color.RGBA{0, 0, 0, 255}
func threshold(original image.Image) image.Image {
img, lums := toGray(original)
median := (slices.Max(lums) + slices.Min(lums)) / 2
X := img.Bounds().Max.X
Y := img.Bounds().Max.Y
for y := range Y {
for x := range X {
r, g, b, _ := img.At(x, y).RGBA()
if getLum(r, g, b) > median {
img.Set(x, y, white)
} else {
img.Set(x, y, black)
}
}
}
return img
}
func toGray(img image.Image) (*image.RGBA, []float64) {
X := img.Bounds().Max.X
Y := img.Bounds().Max.Y
filtered := image.NewRGBA(img.Bounds())
lums := make([]float64, X*Y)
pixel := 0
for y := range Y {
for x := range X {
r, g, b, _ := img.At(x, y).RGBA()
lum := getLum(r, g, b)
lums[pixel] = lum
pixel++
var grayPixel = color.Gray{uint8(lum / 256)}
filtered.Set(x, y, grayPixel)
}
}
return filtered, lums
}
func getLum(r, g, b uint32) float64 {
return float64(r)*0.299 + float64(g)*0.587 + float64(b)*0.114
}
func main() {
var wg sync.WaitGroup
if len(os.Args) <= 1 {
fmt.Println("No images passed as arguments")
return
}
for idx, path := range os.Args[1:] {
wg.Add(1)
go func() {
defer wg.Done()
file, err := os.Open(path)
if err != nil {
fmt.Printf("failed to open file %s:\n %s\n", path, err.Error())
return
}
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
fmt.Printf("failed to decode image from %s:\n %s\n", path, err.Error())
return
}
img = threshold(img)
newfile := fmt.Sprintf("results/result%d.jpg", idx)
save, err := os.Create(newfile)
if err != nil {
fmt.Printf("failed to create file %s: \n %s", path, err.Error())
return
}
defer save.Close()
if err := jpeg.Encode(save, img, nil); err != nil {
fmt.Printf("failed to save file %s: \n %s \n", newfile, err.Error())
return
}
}()
}
wg.Wait()
}