-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
303 lines (270 loc) · 7.42 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package main
import (
"encoding/json"
"flag"
"fmt"
"image"
"image/color"
"image/draw"
"image/jpeg"
"io/ioutil"
"log"
"os"
"sort"
"strings"
"github.com/disintegration/imaging" // hmm, has lots of options...
"github.com/nxshock/colorcrop"
)
type Config struct {
Mode string `json:"mode"`
ImageSize int `json:"image_size"` // for square
WhiteThresold float64 `json:"white_thresold"`
MainSpacer int `json:"main_spacer"`
ThumbnailSpacer int `json:"thumbnail_spacer"`
ThumbnailSize int `json:"thumbnail_size"` // for squares
ThumbnailPos int `json:"thumbnail_pos"`
DotScale float64 `json:"dot_scale"`
DotImage string `json:"dot_image"`
ImageEnding string `json:"image_ending"`
FlipImages string `json:"flip_images"`
}
func getConfig(cf string) (*Config, error) {
f, err := os.Open(cf)
if err != nil {
return nil, err
}
defer f.Close()
var config Config
if err = json.NewDecoder(f).Decode(&config); err != nil {
return nil, err
}
return &config, nil
}
func addMainImage(c *Config, filePath string, finalImg *image.RGBA) error {
fmt.Printf("\rImporting image %d", 1)
imgFile, err := os.Open(filePath)
if err != nil {
return err
}
img, _, err := image.Decode(imgFile)
if err != nil {
return err
}
if c.FlipImages != "no" {
img = imaging.FlipH(img)
}
imgCropped := colorcrop.Crop(img, color.RGBA{215, 255, 255, 255}, c.WhiteThresold)
dy := float64(imgCropped.Bounds().Dy())
dx := float64(imgCropped.Bounds().Dx())
fx, fy := 0.0, 0.0
if c.Mode == "bottom" {
fx = float64(c.ImageSize)
fy = float64(c.ThumbnailPos - c.MainSpacer)
} else {
fx = float64(c.ThumbnailPos - c.MainSpacer)
fy = float64(c.ImageSize)
}
// calculate factor to fit image into the available space
f := 0.0
align := ""
if fx/dx < fy/dy {
f = fx / dx
align = "vertical"
} else {
f = fy / dy
align = "horizontal"
}
imgSized := imaging.Resize(imgCropped, int(dx*f), int(dy*f), imaging.Lanczos)
dx = float64(imgSized.Bounds().Dx())
dy = float64(imgSized.Bounds().Dy())
// calculate rect to align image (middle of available space plus half of the image)
var r image.Rectangle
if align == "vertical" {
r = image.Rect(0, int(fy/2-dy/2), int(fx), int(fy/2+dy/2))
} else {
r = image.Rect(int(fx/2-dx/2), 0, int(fx/2+dx/2), int(fy))
}
draw.Draw(finalImg, r, imgSized, image.Point{0, 0}, draw.Src)
return nil
}
func addThumbnails(c *Config, images []string, finalImg *image.RGBA) error {
printDots := ""
for i, path := range images {
fmt.Printf("\rImporting image %d", i+1)
if printDots != "" {
path = printDots
}
file, err := os.Open(path)
if err != nil {
return err
}
img, _, err := image.Decode(file)
if err != nil {
return err
}
if c.FlipImages != "no" {
img = imaging.FlipH(img)
}
imgCropped := colorcrop.Crop(img, color.RGBA{215, 255, 255, 255}, c.WhiteThresold)
dy := float64(imgCropped.Bounds().Dy())
dx := float64(imgCropped.Bounds().Dx())
fx, fy := float64(c.ThumbnailSize), float64(c.ThumbnailSize)
// calculate factor to fit image into the available space
f := 0.0
align := ""
if fx/dx < fy/dy {
f = fx / dx
align = "vertical"
} else {
f = fy / dy
align = "horizontal"
}
if printDots != "" {
f *= c.DotScale
}
imgSized := imaging.Resize(imgCropped, int(dx*f), int(dy*f), imaging.Lanczos)
dx = float64(imgSized.Bounds().Dx())
dy = float64(imgSized.Bounds().Dy())
var r image.Rectangle
if c.Mode == "bottom" {
if printDots != "" {
// vertical and horizontal (dotScale...)
r = image.Rect(
(i+1)*c.ThumbnailSpacer+i*c.ThumbnailSize+int(fx/2-dx/2),
c.ThumbnailPos+int(fy/2-dy/2),
(i+1)*c.ThumbnailSpacer+(i+1)*c.ThumbnailSize+int(fx/2+dx/2)+int(fx),
c.ThumbnailPos+c.ThumbnailSize+int(fy)+int(fy/2+dy/2),
)
} else if align == "vertical" {
r = image.Rect(
(i+1)*c.ThumbnailSpacer+i*c.ThumbnailSize,
c.ThumbnailPos+int(fy/2-dy/2),
(i+1)*c.ThumbnailSpacer+(i+1)*c.ThumbnailSize+int(fx),
c.ThumbnailPos+c.ThumbnailSize+int(fy/2+dy/2),
)
} else {
r = image.Rect(
(i+1)*c.ThumbnailSpacer+i*c.ThumbnailSize+int(fx/2-dx/2),
c.ThumbnailPos,
(i+1)*c.ThumbnailSpacer+(i+1)*c.ThumbnailSize+int(fx/2+dx/2),
c.ThumbnailPos+c.ThumbnailSize+int(fy),
)
}
} else {
if printDots != "" {
r = image.Rect(
c.ThumbnailPos+int(fx/2-dx/2),
(i+1)*c.ThumbnailSpacer+i*c.ThumbnailSize+int(fy/2-dy/2),
c.ThumbnailPos+c.ThumbnailSize+int(fx/2+dx/2)+int(fx),
(i+1)*c.ThumbnailSpacer+(i+1)*c.ThumbnailSize+int(fy)+int(fy/2+dy/2),
)
} else if align == "vertical" {
r = image.Rect(
c.ThumbnailPos,
(i+1)*c.ThumbnailSpacer+i*c.ThumbnailSize+int(fy/2-dy/2),
c.ThumbnailPos+c.ThumbnailSize+int(fx),
(i+1)*c.ThumbnailSpacer+(i+1)*c.ThumbnailSize+int(fy/2+dy/2),
)
} else {
r = image.Rect(
c.ThumbnailPos+int(fx/2-dx/2),
(i+1)*c.ThumbnailSpacer+i*c.ThumbnailSize,
c.ThumbnailPos+c.ThumbnailSize+int(fx/2+dx/2),
(i+1)*c.ThumbnailSpacer+(i+1)*c.ThumbnailSize+int(fy),
)
}
}
draw.Draw(finalImg, r, imgSized, image.Point{0, 0}, draw.Src)
// if not last and if next two do not fit, dots and stop
if printDots != "" {
break
}
if len(images[i+1:]) != 1 && (i+3)*c.ThumbnailSpacer+(i+3)*c.ThumbnailSize > c.ImageSize {
printDots = c.DotImage
}
}
return nil
}
func isDirectory(path string) (bool, error) {
fileInfo, err := os.Stat(path)
if err != nil {
return false, err
}
return fileInfo.IsDir(), err
}
func findImages(c *Config, path string) []string {
// find dir
var dir string
files, err := ioutil.ReadDir(path)
if err != nil {
log.Fatalln(err)
}
for _, f := range files {
if f.Name() == ".git" {
continue
}
if isDir, _ := isDirectory("./" + f.Name()); isDir {
if dir != "" {
log.Fatalln("Fehler. Es darf hier nur einen Ordner mit Bildern geben.")
}
dir = f.Name() + "/"
}
}
// find images
var images []string
files, err = ioutil.ReadDir(dir)
if err != nil {
log.Fatalln(err)
}
for _, f := range files {
if strings.HasSuffix(f.Name(), c.ImageEnding) || strings.HasSuffix(f.Name(), strings.ToUpper(c.ImageEnding)) {
images = append(images, dir+f.Name())
}
}
if len(images) <= 0 {
log.Fatalf("Fehler. Es wurden keine Bilder mit der Endung '%s', in '%s' gefunden.", c.ImageEnding, dir)
}
sort.Strings(images)
return images
}
func main() {
c := flag.String("config", "", "flag config (ebay or shopify) is required")
flag.Parse()
cf := ""
if *c == "ebay" {
cf = "config_ebay.json"
}
if *c == "shopify" {
cf = "config_shopify.json"
}
if cf == "" {
flag.PrintDefaults()
os.Exit(1)
}
config, err := getConfig(cf)
if err != nil {
log.Fatalf("Config konnte nicht gelesen werden: %v\n", err)
}
finalImg := image.NewRGBA(
image.Rectangle{
image.Point{0, 0},
image.Point{config.ImageSize, config.ImageSize},
},
)
draw.Draw(finalImg, finalImg.Bounds(), &image.Uniform{color.RGBA{255, 255, 255, 255}}, image.ZP, draw.Src)
images := findImages(config, "./")
if err := addMainImage(config, images[0], finalImg); err != nil {
log.Fatalln(err)
}
if err := addThumbnails(config, images[1:], finalImg); err != nil {
log.Fatalln(err)
}
out, err := os.Create("./output.jpg")
if err != nil {
log.Fatalln(err)
}
jpeg.Encode(out, finalImg, &jpeg.Options{Quality: 95})
fmt.Println("\r ")
fmt.Println("Erfolg. Startbild wurde geschrieben.")
fmt.Println("")
}