-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrayscale.go
108 lines (101 loc) · 2.32 KB
/
grayscale.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
package main
import (
"fmt"
"image"
"image/color"
"image/png"
"os"
)
func Lightness() {
file, err := os.Open("./Input/image.png")
if err != nil {
panic(err.Error())
}
defer file.Close()
img, err := png.Decode(file)
if err != nil {
panic(err.Error())
}
bounds := img.Bounds()
width, height := bounds.Max.X, bounds.Max.Y
Image := image.NewGray(image.Rect(0, 0, width, height))
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
r, g, b, _ := img.At(x, y).RGBA()
gray := (max(r, g, b) + min(r, g, b)) / 2
//fmt.Println(r, " ", g, " ", b, " ", gray)
Image.Set(x, y, color.Gray{Y: uint8(gray / 256)})
}
}
outFile, err := os.Create("./Output/output.png")
if err != nil {
panic(err.Error())
}
defer outFile.Close()
err = png.Encode(outFile, Image)
if err != nil {
panic(err.Error())
}
}
func Avarage() {
file, err := os.Open("./Input/image.png")
if err != nil {
panic(err.Error())
}
defer file.Close()
img, err := png.Decode(file)
if err != nil {
panic(err.Error())
}
bounds := img.Bounds()
width, height := bounds.Max.X, bounds.Max.Y
Image := image.NewGray(image.Rect(0, 0, width, height))
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
r, g, b, _ := img.At(x, y).RGBA()
gray := (r + g + b) / 3
//fmt.Println(r, " ", g, " ", b, " ", gray)
Image.Set(x, y, color.Gray{Y: uint8(gray / 256)})
}
}
outFile, err := os.Create("./Output/output.png")
if err != nil {
panic(err.Error())
}
defer outFile.Close()
err = png.Encode(outFile, Image)
if err != nil {
panic(err.Error())
}
}
func Luminosity() {
file, err := os.Open("./Input/Low.png")
if err != nil {
panic(err.Error())
}
defer file.Close()
img, err := png.Decode(file)
if err != nil {
panic(err.Error())
}
bounds := img.Bounds()
width, height := bounds.Max.X, bounds.Max.Y
Image := image.NewGray(image.Rect(0, 0, width, height))
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
r, g, b, _ := img.At(x, y).RGBA()
gray := 0.21*float64(r) + 0.72*float64(g) + 0.07*float64(b)
fmt.Println(r, " ", g, " ", b, " ", uint8(gray/256))
Image.Set(x, y, color.Gray{Y: uint8(gray / 256)})
}
}
outFile, err := os.Create("./Output/output.png")
if err != nil {
panic(err.Error())
}
defer outFile.Close()
err = png.Encode(outFile, Image)
if err != nil {
panic(err.Error())
}
}