-
Notifications
You must be signed in to change notification settings - Fork 0
/
eyespye.go
218 lines (174 loc) · 6.32 KB
/
eyespye.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package main
import (
"gopkg.in/gographics/imagick.v1/imagick"
"fmt"
"sync"
"runtime"
"os"
"time"
"encoding/json"
)
func makeTimestamp() int64 {
return time.Now().UnixNano()
}
type ImageInfo struct {
Filename string
Coverage float64
Background float64
BackgroundColor string
Height int
Width int
}
func BlackOrWhiteBackground(mw *imagick.MagickWand, width int, height int, fuzz float64) string {
var pixIterators []*imagick.PixelIterator
//get corner 10x10 regions of the picture
pixIterators = append(pixIterators, mw.NewPixelRegionIterator(0, 0, 10, 10))
pixIterators = append(pixIterators, mw.NewPixelRegionIterator(width -10, 0, 10, 10))
pixIterators = append(pixIterators, mw.NewPixelRegionIterator(0, height - 10, 10, 10))
pixIterators = append(pixIterators, mw.NewPixelRegionIterator(width - 10, height - 10, 10, 10))
white, black := imagick.NewPixelWand(), imagick.NewPixelWand()
white.SetColor("white")
black.SetColor("black")
whiteCount, blackCount := 0, 0
//Match the pixel rows either to black or white
for _, iterator := range pixIterators {
for _, pixelRow := range iterator.GetNextIteratorRow() {
//fmt.Println(pixelRow.GetColorAsString())
if pixelRow.IsSimilar(white, fuzz) {
whiteCount++
} else if pixelRow.IsSimilar(black, fuzz) {
blackCount++
}
}
}
//fmt.Println("Black count:", blackCount, " White count:", whiteCount)
if blackCount > whiteCount && blackCount > 0 {
return "black"
} else {
return "white"
}
}
func GetCoverage(mw *imagick.MagickWand, backgroundColorAsString string, width int, height int, fuzz float64) float64{
// fillcolor and bordercolor
transparentColor, bc := imagick.NewPixelWand(), imagick.NewPixelWand()
transparentColor.SetColor("none")
bc.SetColor(backgroundColorAsString)
rgba := imagick.CHANNELS_RGB | imagick.CHANNEL_ALPHA
//fill from all corners +-1 pixels
mw.FloodfillPaintImage(rgba, transparentColor, fuzz, bc, width-1, height-1, false)
mw.FloodfillPaintImage(rgba, transparentColor, fuzz, bc, 1, 1, false)
mw.FloodfillPaintImage(rgba, transparentColor, fuzz, bc, width-1, 1, false)
mw.FloodfillPaintImage(rgba, transparentColor, fuzz, bc, 1, height-1, false)
mw.LevelImage(0.9, 0, 1)
number, pixels := mw.GetImageHistogram()
pixels = pixels[:number];
transparent, allcount := uint(0), uint(0)
for _, pix := range pixels {
if pix.IsVerified() == true {
if pix.GetAlpha() == 0 {
transparent = transparent + pix.GetColorCount()
}
allcount = allcount + pix.GetColorCount()
}
}
coverage := 100 - (float64(transparent) / float64(allcount) * 100)
return coverage
}
func GetBackgroundInfo(filePath string, c chan ImageInfo, wg *sync.WaitGroup) {
var err error
wroteToChannel := false;
defer func() {
if (!wroteToChannel) {
wg.Done()
}
}()
mw := imagick.NewMagickWand()
err = mw.ReadImage(filePath)
if err != nil {
panic(err)
}
// Get original logo size
width := int(mw.GetImageWidth())
height := int(mw.GetImageHeight())
backgroundColorString := BlackOrWhiteBackground(mw, width, height, 200)
coverage := GetCoverage(mw, backgroundColorString, width, height, 1500)
c <- ImageInfo{filePath, coverage, 100 - coverage, backgroundColorString, height, width}
wroteToChannel = true
}
func GetBackgroundInfoSync(filePath string) ImageInfo {
mw := imagick.NewMagickWand()
err := mw.ReadImage(filePath)
if err != nil {
panic(err)
}
// Get original logo size
width := int(mw.GetImageWidth())
height := int(mw.GetImageHeight())
backgroundColorString := BlackOrWhiteBackground(mw, width, height, 200)
coverage := GetCoverage(mw, backgroundColorString, width, height, 1500)
//mw.DisplayImage(os.Getenv("DISPLAY"))
return ImageInfo{filePath, coverage, 100 - coverage, backgroundColorString, height, width}
}
func main() {
var wg sync.WaitGroup
files := os.Args[1:]
showStats := true
asynchronous := true
//files = []string{
// "/home/luka/Desktop/IMAGES/25percentWithHoleInTheMiddle.jpg",
// "/home/luka/Desktop/IMAGES/25percent.jpg",
// "/home/luka/Desktop/IMAGES/25percentUpperCorner.jpg",
// "/home/luka/Desktop/IMAGES/whiteSquare.png",
// "/home/luka/Desktop/IMAGES/a.jpg",
// "/home/luka/Desktop/IMAGES/b.jpg",
// "/home/luka/Desktop/IMAGES/c.png",
// "/home/luka/Desktop/IMAGES/d.jpg",
// "/home/luka/Desktop/IMAGES/e.png",
// "/home/luka/Desktop/IMAGES/f.jpg",
// "/home/luka/Desktop/IMAGES/g.jpg",
// "/home/luka/Desktop/IMAGES/i.jpg",
// "/home/luka/Desktop/IMAGES/blackBackground.jpg",
// "/home/luka/Desktop/IMAGES/transparent.png",
// "/home/luka/Desktop/IMAGES/transparentWhite.png",
//}
var data []ImageInfo
channel := make(chan ImageInfo)
imagick.Initialize()
startMark := makeTimestamp()
if (asynchronous) {
wg.Add(len(files))
//run a routine for each file
for _, file := range files {
go GetBackgroundInfo(file, channel, &wg)
}
//Collect ImageInfo objects and mark routines as done
go func() {
defer wg.Done()
for response := range channel {
data = append(data, response)
wg.Done()
}
}()
wg.Wait()
} else {
for _, file := range files {
data = append(data, GetBackgroundInfoSync(file))
}
}
endMark := makeTimestamp()
json, _ := json.Marshal(data)
fmt.Println(string(json))
for _, info := range data {
fmt.Println(info.Filename, info.Coverage)
}
if (showStats) {
s := new(runtime.MemStats)
runtime.ReadMemStats(s)
fmt.Println("Alloc : ", s.Alloc /1024 , "MB")
fmt.Println("Total Alloc : ", s.TotalAlloc /1024 , "MB")
fmt.Println("Sys : ", s.Sys /1024 , "MB")
fmt.Println("Lookups : ", s.Lookups)
duration := (endMark - startMark) / (int64(time.Millisecond)/int64(time.Nanosecond))
fmt.Println("Runtime:", duration,"ms")
}
}