-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
144 lines (128 loc) · 3.63 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
package main
import (
"fmt"
"idea-test-project/src/image/prominentcolor"
"image"
_ "image/jpeg"
_ "image/png"
"io/ioutil"
"log"
"os"
"strings"
)
/*
Find the K most dominant colors in an image
https://github.com/EdlinOrg/prominentcolor.git
*/
func loadImage(fileInput string) (image.Image, error) {
f, err := os.Open(fileInput)
defer f.Close()
if err != nil {
log.Println("File not found:", fileInput)
return nil, err
}
img, _, err := image.Decode(f)
if err != nil {
return nil, err
}
return img, nil
}
func outputColorRange(colorRange []prominentcolor.ColorItem) string {
var buff strings.Builder
buff.WriteString("<table><tr>")
for _, color := range colorRange {
buff.WriteString(fmt.Sprintf("<td style=\"background-color: #%s;width:200px;height:50px;text-align:center;\">#%s %d</td>", color.AsString(), color.AsString(), color.Cnt))
}
buff.WriteString("</tr></table>")
return buff.String()
}
func outputTitle(str string) string {
return "<h3>" + str + "</h3>"
}
func processBatch(k int, bitarr []int, img image.Image) string {
var buff strings.Builder
prefix := fmt.Sprintf("K=%d, ", k)
resizeSize := uint(prominentcolor.DefaultSize)
bgmasks := prominentcolor.GetDefaultMasks()
for i := 0; i < len(bitarr); i++ {
res, err := prominentcolor.KmeansWithAll(k, img, bitarr[i], resizeSize, bgmasks)
if err != nil {
log.Println(err)
continue
}
buff.WriteString(outputTitle(prefix + bitInfo(bitarr[i])))
buff.WriteString(outputColorRange(res))
}
return buff.String()
}
func bitInfo(bits int) string {
list := make([]string, 0, 4)
// random seed or Kmeans++
if prominentcolor.IsBitSet(bits, prominentcolor.ArgumentSeedRandom) {
list = append(list, "Random seed")
} else {
list = append(list, "Kmeans++")
}
// Mean or median
if prominentcolor.IsBitSet(bits, prominentcolor.ArgumentAverageMean) {
list = append(list, "Mean")
} else {
list = append(list, "Median")
}
// LAB or RGB
if prominentcolor.IsBitSet(bits, prominentcolor.ArgumentLAB) {
list = append(list, "LAB")
} else {
list = append(list, "RGB")
}
// Cropping or not
if prominentcolor.IsBitSet(bits, prominentcolor.ArgumentNoCropping) {
list = append(list, "No cropping")
} else {
list = append(list, "Cropping center")
}
// Done
return strings.Join(list, ", ")
}
func main() {
// Prepare
outputDirectory := "./src/image/prominentcolor/"
dataDirectory := "./src/image/prominentcolor/"
var buff strings.Builder
buff.WriteString("<html><body><h1>Colors listed in order of dominance: hex color followed by number of entries</h1><table border=\"1\">")
// for each file within working directory
files, err := ioutil.ReadDir(dataDirectory)
if err != nil {
log.Fatal(err)
}
for _, f := range files {
filename := f.Name()
// Only process jpg
if !(strings.HasSuffix(filename, ".jpg") || strings.HasSuffix(filename, ".png")) {
continue
}
// Define the differents sets of params
kk := []int{
prominentcolor.ArgumentAverageMean | prominentcolor.ArgumentNoCropping,
prominentcolor.ArgumentNoCropping,
prominentcolor.ArgumentDefault,
}
// Load the image
img, err := loadImage(dataDirectory + filename)
if err != nil {
log.Printf("Error loading image %s\n", filename)
log.Println(err)
continue
}
// Process & html output
buff.WriteString("<tr><td><img src=\"" + filename + "\" width=\"200\" border=\"1\"></td><td>")
buff.WriteString(processBatch(3, kk, img))
buff.WriteString("</td></tr>")
}
// Finalize the html output
buff.WriteString("</table></body><html>")
// And write it to the disk
if err = ioutil.WriteFile(outputDirectory+"output.html", []byte(buff.String()), 0644); err != nil {
panic(err)
}
}