/
main.go
65 lines (51 loc) · 1.26 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
package main
import (
"fmt"
"image"
"math"
"os"
"strings"
_ "golang.org/x/image/bmp"
_ "image/jpeg"
_ "image/png"
"github.com/dsoprea/go-logging"
"github.com/jessevdk/go-flags"
"github.com/dsoprea/go-perceptualhash"
)
var (
mLog = log.NewLogger("main")
)
type options struct {
Hashbits int `long:"bits" short:"b" default:"16" description:"Hash bit length (N^2)"`
Filepaths []string `long:"filepath" short:"f" required:"true" description:"Image file-path (provide at least once)"`
Digest bool `long:"digest" short:"d" description:"Just print digest (no filenames)"`
}
func main() {
defer func() {
if state := recover(); state != nil {
log.Panic(state)
}
}()
o := new(options)
if _, err := flags.Parse(o); err != nil {
os.Exit(1)
}
len_ := 0
for _, filepath := range o.Filepaths {
len_ = int(math.Max(float64(len_), float64(len(filepath))))
}
for _, filepath := range o.Filepaths {
f, err := os.Open(filepath)
log.PanicIf(err)
defer f.Close()
image, _, err := image.Decode(f)
log.PanicIf(err)
bh := blockhash.NewBlockhash(image, o.Hashbits)
hexdigest := bh.Hexdigest()
if o.Digest {
fmt.Println(hexdigest)
} else {
fmt.Printf("%s%s %s\n", filepath, strings.Repeat(" ", len_-len(filepath)), hexdigest)
}
}
}