-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
437 lines (401 loc) · 12 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
package main
import (
"bytes"
"encoding/json"
"fmt"
"image"
"image/color"
"image/draw"
"image/jpeg"
"io"
"io/ioutil"
"log"
"math"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"github.com/Luxurioust/excelize"
)
type ByNumericalFilename []os.FileInfo
func main() {
//尺寸表路徑
data, err := ioutil.ReadFile("config.json")
if err != nil {
fmt.Print(err)
os.Exit(3)
}
type config struct {
WorkPath string `json:"WorkPath"`
SizeTablePath string `json:"SizeTablePath"`
}
var obj config
err = json.Unmarshal(data, &obj)
if err != nil {
fmt.Println("error:", err)
os.Exit(3)
}
DirPath := strings.Replace(obj.WorkPath, "\\", "\\\\", -1)
SpecPath := strings.Replace(obj.SizeTablePath, "\\", "\\\\", -1)
//掃描DIR
dirArr := scandir(DirPath)
//excel資料夾檔名
var excelArr []string
//圖片資料夾檔名
var imageDirArr []string
//掃描圖片儲存陣列
var imagePicArr []string
//掃描第一層有的 EXCEL 及 圖片資料夾
for _, file := range dirArr {
if strings.Index(file, ".xlsx") > -1 {
excelArr = append(excelArr, file)
} else {
imageDirArr = append(imageDirArr, file)
}
}
//讀圖片 塞入陣列
imagePath := DirPath + string(os.PathSeparator) + imageDirArr[0]
imageFileList := scandir_sort(imagePath)
for _, file := range imageFileList {
if strings.Index(file, ".jpg") > -1 || strings.Index(file, ".png") > -1 {
imagePicArr = append(imagePicArr, file)
}
}
//讀excel 跑陣列
excelPath := DirPath + string(os.PathSeparator) + excelArr[0]
xlsx, err := excelize.OpenFile(excelPath)
if err != nil {
fmt.Println(err)
return
}
sheetDefaultIndex := xlsx.GetActiveSheetIndex()
defaultSheetName := xlsx.GetSheetName(sheetDefaultIndex)
// 獲取 Sheet1 上所有儲存格
rows, err := xlsx.GetRows(defaultSheetName)
begin := 0
end := 3
var smaillSizeArr []string
var smaillPathArr []string
var failSizeTable []string
var totalExcelCount int = 0
for _, row := range rows {
i, err := strconv.Atoi(row[8])
if err != nil {
fmt.Println(err)
os.Exit(2)
}
totalExcelCount += i
}
imageFileListCount := len(imagePicArr)
if totalExcelCount != imageFileListCount {
fmt.Println("EXCEL 檔案I欄位總數:" + strconv.Itoa(totalExcelCount) + ",資料夾內圖片總數:" + strconv.Itoa(imageFileListCount) + ",親愛的請檢查您的圖片總數。")
fmt.Scanln()
os.Exit(2)
}
// fmt.Println(imagePicArr)
// os.Exit(2)
for index, row := range rows {
step, errturn := strconv.Atoi(row[8])
if errturn != nil {
fmt.Println("轉換失敗!!")
}
if index == 0 {
end = step - 1
} else {
end = end + step
}
leve1MakeDir := DirPath + string(os.PathSeparator) + row[0] + "_" + row[1]
row[6] = strings.Replace(row[6], "/", "", -1)
leve2MakeDir := leve1MakeDir + string(os.PathSeparator) + row[3] + "_" + row[6]
leve3MakeDir := leve2MakeDir + string(os.PathSeparator) + "BIG"
leve4MakeDir := leve2MakeDir + string(os.PathSeparator) + "SMALL"
leve15MakeDir := leve1MakeDir + string(os.PathSeparator) + "OUT"
styleNo := strings.Split(row[2], "-")
styleNoPath := SpecPath + string(os.PathSeparator) + styleNo[0] + ".jpg"
if _, err1 := os.Stat(leve1MakeDir); os.IsNotExist(err1) {
os.Mkdir(leve1MakeDir, 0755)
fmt.Println("建立資料夾:" + leve1MakeDir)
}
if _, err2 := os.Stat(leve2MakeDir); os.IsNotExist(err2) {
os.Mkdir(leve2MakeDir, 0755)
fmt.Println("建立資料夾:" + leve2MakeDir)
}
if _, err3 := os.Stat(leve3MakeDir); os.IsNotExist(err3) {
os.Mkdir(leve3MakeDir, 0755)
fmt.Println("建立資料夾:" + leve3MakeDir)
}
if _, err4 := os.Stat(leve4MakeDir); os.IsNotExist(err4) {
os.Mkdir(leve4MakeDir, 0755)
fmt.Println("建立資料夾:" + leve4MakeDir)
}
if _, err6 := os.Stat(leve15MakeDir); os.IsNotExist(err6) {
os.Mkdir(leve15MakeDir, 0755)
fmt.Println("建立資料夾:" + leve15MakeDir)
}
err3 := CopyFile(styleNoPath, leve15MakeDir+string(os.PathSeparator)+row[3]+"_"+styleNo[0]+".jpg")
if err3 != nil {
fmt.Printf("複製尺寸表 %s 失敗 %q\n", styleNoPath, err3)
failSizeTable = append(failSizeTable, "複製尺寸表:"+styleNoPath+"失敗\n")
} else {
fmt.Printf("複製尺寸表 %s\n", styleNoPath+".jpg")
}
count := 1
for i := begin; i <= end; i++ {
pathOneImg := imagePath + string(os.PathSeparator) + imagePicArr[i]
imageNewPath := leve3MakeDir + string(os.PathSeparator) + row[3] + "_0" + strconv.Itoa(count) + ".jpg"
err := CopyFile(pathOneImg, imageNewPath)
if err != nil {
fmt.Printf("複製圖片失敗 %q\n", err)
} else {
fmt.Println("複製圖片從:" + pathOneImg + "到 " + imageNewPath)
}
imageNewPath2 := leve4MakeDir + string(os.PathSeparator) + row[3] + "_0" + strconv.Itoa(count) + ".jpg"
err2 := CopyFile(pathOneImg, imageNewPath2)
if err2 != nil {
fmt.Printf("複製圖片失敗 %q\n", err2)
} else {
fmt.Println("複製圖片從:" + pathOneImg + "到 " + imageNewPath2)
smaillSizeArr = append(smaillSizeArr, imageNewPath2)
smaillPathArr = append(smaillPathArr, leve4MakeDir+string(os.PathSeparator)+";")
}
imageNewPath4 := leve15MakeDir + string(os.PathSeparator) + row[3] + "_0" + strconv.Itoa(count) + ".jpg"
err4 := CopyFile(pathOneImg, imageNewPath4)
if err4 != nil {
fmt.Printf("複製圖片失敗 %q\n", err4)
} else {
fmt.Println("複製圖片從:" + pathOneImg + "到 " + imageNewPath4)
}
count++
}
begin = begin + step
}
var txtString string = ""
//resize 圖片
if len(failSizeTable) > 0 {
for _, errorMsg := range failSizeTable {
txtString = txtString + errorMsg
}
content := []byte(txtString)
err := ioutil.WriteFile("log.txt", content, 0666)
if err != nil {
fmt.Println("ioutil WriteFile error: ", err)
}
}
if len(smaillPathArr) > 0 {
txtString = ""
for _, smallPath := range smaillPathArr {
txtString = txtString + smallPath + "\n"
}
content := []byte(txtString)
err := ioutil.WriteFile("smallPath.txt", content, 0666)
if err != nil {
fmt.Println("ioutil WriteFile error: ", err)
}
}
fmt.Println("執行完成")
fmt.Scanln()
}
func (nf ByNumericalFilename) Len() int { return len(nf) }
func (nf ByNumericalFilename) Swap(i, j int) { nf[i], nf[j] = nf[j], nf[i] }
func (nf ByNumericalFilename) Less(i, j int) bool {
// Use path names
pathA := nf[i].Name()
pathB := nf[j].Name()
if (strings.Index(pathA, ".jpg") > -1 || strings.Index(pathA, ".png") > -1) && strings.Index(pathB, ".jpg") > -1 || strings.Index(pathB, ".png") > -1 {
// Grab integer value of each filename by parsing the string and slicing off
// the extension
a, err1 := strconv.ParseInt(pathA[strings.LastIndex(pathA, "(")+1:strings.LastIndex(pathA, ")")], 10, 64)
b, err2 := strconv.ParseInt(pathB[strings.LastIndex(pathB, "(")+1:strings.LastIndex(pathB, ")")], 10, 64)
// If any were not numbers sort lexographically
if err1 != nil || err2 != nil {
return pathA < pathB
}
// Which integer is smaller?
return a < b
} else {
return false
}
}
//掃描資料夾底下檔案
func scandir_sort(dir string) []string {
var files []string
filelist, err := ioutil.ReadDir(dir)
if err != nil {
log.Fatal(err)
}
sort.Sort(ByNumericalFilename(filelist))
for _, f := range filelist {
files = append(files, f.Name())
}
return files
}
//掃描資料夾底下檔案
func scandir(dir string) []string {
var files []string
filelist, err := ioutil.ReadDir(dir)
if err != nil {
log.Fatal(err)
}
for _, f := range filelist {
files = append(files, f.Name())
}
return files
}
//byte 轉 string
func BytesToString(data []byte) string {
return string(data[:])
}
func moveFile(orgPath string, movePath string) {
fmt.Println(movePath)
path := filepath.Dir(movePath)
if _, err := os.Stat(path); os.IsNotExist(err) {
os.Mkdir(path, 0777)
}
err := os.Rename(orgPath, movePath)
if err != nil {
fmt.Println("移動檔案失敗!!")
}
}
// CopyFile copies a file from src to dst. If src and dst files exist, and are
// the same, then return success. Otherise, attempt to create a hard link
// between the two files. If that fail, copy the file contents from src to dst.
func CopyFile(src, dst string) (err error) {
sfi, err := os.Stat(src)
if err != nil {
return
}
if !sfi.Mode().IsRegular() {
// cannot copy non-regular files (e.g., directories,
// symlinks, devices, etc.)
return fmt.Errorf("CopyFile: non-regular source file %s (%q)", sfi.Name(), sfi.Mode().String())
}
dfi, err := os.Stat(dst)
if err != nil {
if !os.IsNotExist(err) {
return
}
} else {
if !(dfi.Mode().IsRegular()) {
return fmt.Errorf("CopyFile: non-regular destination file %s (%q)", dfi.Name(), dfi.Mode().String())
}
if os.SameFile(sfi, dfi) {
return
}
}
if err = os.Link(src, dst); err == nil {
return
}
err = copyFileContents(src, dst)
return
}
// copyFileContents copies the contents of the file named src to the file named
// by dst. The file will be created if it does not already exist. If the
// destination file exists, all it's contents will be replaced by the contents
// of the source file.
func copyFileContents(src, dst string) (err error) {
in, err := os.Open(src)
if err != nil {
return
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return
}
defer func() {
cerr := out.Close()
if err == nil {
err = cerr
}
}()
if _, err = io.Copy(out, in); err != nil {
return
}
err = out.Sync()
return
}
func resizeSelf(img image.Image, length int, width int) image.Image {
//truncate pixel size
minX := img.Bounds().Min.X
minY := img.Bounds().Min.Y
maxX := img.Bounds().Max.X
maxY := img.Bounds().Max.Y
for (maxX-minX)%length != 0 {
maxX--
}
for (maxY-minY)%width != 0 {
maxY--
}
scaleX := (maxX - minX) / length
scaleY := (maxY - minY) / width
imgRect := image.Rect(0, 0, length, width)
resImg := image.NewRGBA(imgRect)
draw.Draw(resImg, resImg.Bounds(), &image.Uniform{C: color.White}, image.ZP, draw.Src)
for y := 0; y < width; y += 1 {
for x := 0; x < length; x += 1 {
averageColor := getAverageColor(img, minX+x*scaleX, minX+(x+1)*scaleX, minY+y*scaleY, minY+(y+1)*scaleY)
resImg.Set(x, y, averageColor)
}
}
return resImg
}
func getAverageColor(img image.Image, minX int, maxX int, minY int, maxY int) color.Color {
var averageRed float64
var averageGreen float64
var averageBlue float64
var averageAlpha float64
scale := 1.0 / float64((maxX-minX)*(maxY-minY))
for i := minX; i < maxX; i++ {
for k := minY; k < maxY; k++ {
r, g, b, a := img.At(i, k).RGBA()
averageRed += float64(r) * scale
averageGreen += float64(g) * scale
averageBlue += float64(b) * scale
averageAlpha += float64(a) * scale
}
}
averageRed = math.Sqrt(averageRed)
averageGreen = math.Sqrt(averageGreen)
averageBlue = math.Sqrt(averageBlue)
averageAlpha = math.Sqrt(averageAlpha)
averageColor := color.RGBA{
R: uint8(averageRed),
G: uint8(averageGreen),
B: uint8(averageBlue),
A: uint8(averageAlpha)}
return averageColor
}
func imgToBytes(img image.Image) []byte {
var opt jpeg.Options
opt.Quality = 80
buff := bytes.NewBuffer(nil)
err := jpeg.Encode(buff, img, &opt)
if err != nil {
log.Fatal(err)
}
return buff.Bytes()
}
func loadImage(filename string) image.Image {
f, err := os.Open(filename)
if err != nil {
log.Fatalf("os.Open failed: %v", err)
}
defer f.Close()
img, _, err := image.Decode(f)
if err != nil {
log.Fatalf("image.Decode failed: %v", err)
}
return img
}
func saveImage(filename string, img image.Image) {
f, err := os.Create(filename)
if err != nil {
log.Fatalf("os.Create failed: %v", err)
}
defer f.Close()
err = jpeg.Encode(f, img, nil)
if err != nil {
log.Fatalf("png.Encode failed: %v", err)
}
}