-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
59 lines (49 loc) · 1.06 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
// Image-Clustering is a cli tool to partition pixels in an image
// based on their color to a pre-defined number of clusters using
// the k-means algorithm.
//
package main
import (
"flag"
"fmt"
"image"
"image/png"
"log"
"os"
)
var inPath = flag.String("in", "", "Path to input image")
var outPath = flag.String("out", "${HOME}/img_clustered.png", "Path to save clustered image")
var k = flag.Int("k", 10, "Number of cluster")
func main() {
flag.Parse()
if *inPath == "" {
fmt.Println("require image to process")
os.Exit(0)
}
if *k < 1 {
fmt.Println("require positive amount of clusters")
}
imgFile, err := os.Open(*inPath)
if err != nil {
log.Fatal(err)
}
defer imgFile.Close()
defer imgFile.Seek(0, 0)
imgData, f, err := image.Decode(imgFile)
if err != nil {
log.Fatal(err)
}
fmt.Print(f)
//imgData, err := png.Decode(imgFile)
//if err != nil {
// log.Fatal(err)
//}
img := clusterImage(*k, imgData)
cImg, err := os.Create(*outPath)
if err != nil {
log.Fatal(err)
}
defer cImg.Close()
defer cImg.Seek(0, 0)
png.Encode(cImg, img)
}