forked from EdlinOrg/prominentcolor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example2.go
64 lines (52 loc) · 1.36 KB
/
example2.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
package main
import (
"image"
_ "image/jpeg"
"log"
"os"
"path/filepath"
"fmt"
prominentcolor ".."
)
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
}
// Process images in a directory, for each image it picks out the dominant color and
// prints out an imagemagick call to resize image and use the dominant color as padding for the background
// it saves tmp files in /tmp/ with the masked bit marked as pink
func main() {
inputPattern := "../example/*.jpg"
outputDirectory := "/tmp/"
files, err := filepath.Glob(inputPattern)
if nil != err {
log.Println(err)
log.Println("Error: failed glob")
return
}
for _, file := range files {
img, err := loadImage(file)
if nil != err {
log.Println(err)
log.Printf("Error: failed loading %s\n", file)
continue
}
cols, err := prominentcolor.KmeansWithArgs(prominentcolor.ArgumentNoCropping|prominentcolor.ArgumentDebugImage, img)
if err != nil {
log.Println(err)
continue
}
col := cols[0].AsString()
base := filepath.Base(file)
fmt.Printf("convert %s -resize 800x356 -background '#%s' -gravity center -extent 800x356 %s%s\n", base, col, outputDirectory, base)
}
}