Skip to content

Instantly share code, notes, and snippets.

@cynthia
Created August 4, 2017 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cynthia/9b7ba6fba93264edb0c8d8732b3cede4 to your computer and use it in GitHub Desktop.
Save cynthia/9b7ba6fba93264edb0c8d8732b3cede4 to your computer and use it in GitHub Desktop.
Quick and dirty hack CLI for goimagehash library. https://github.com/corona10/goimagehash
package main
import (
"github.com/corona10/goimagehash"
"fmt"
"image"
_ "image/jpeg"
_ "image/png"
"os"
"strconv"
)
func showUsage() {
fmt.Println("goimagehash [ahash|phash|dhash] image1 (image2)")
}
func cp(e error) {
if e != nil {
panic(e)
}
}
func main() {
if len(os.Args) >= 3 {
var f2 *os.File = nil
var i2 image.Image = nil
var ih1 *goimagehash.ImageHash = nil
var ih2 *goimagehash.ImageHash = nil
f1, err := os.Open(os.Args[2])
cp(err)
if len(os.Args) >= 4 {
f2, err = os.Open(os.Args[3])
cp(err)
}
i1, _, err := image.Decode(f1)
cp(err)
if f2 != nil {
i2, _, err = image.Decode(f2)
cp(err)
}
if os.Args[1] == "ahash" {
ih1, _ = goimagehash.AverageHash(i1)
if i2 != nil {
ih2, _ = goimagehash.AverageHash(i2)
}
} else if os.Args[1] == "phash" {
ih1, _ = goimagehash.DifferenceHash(i1)
if i2 != nil {
ih2, _ = goimagehash.DifferenceHash(i2)
}
} else if os.Args[1] == "dhash" {
ih1, _ = goimagehash.PerceptionHash(i1)
if i2 != nil {
ih2, _ = goimagehash.PerceptionHash(i2)
}
} else {
showUsage()
return
}
fmt.Println("Image hash for " + os.Args[2] + " is " + strconv.FormatUint(ih1.GetHash(), 10))
if ih2 != nil {
fmt.Println("Image hash for " + os.Args[3] + " is " + strconv.FormatUint(ih2.GetHash(), 10))
d, _ := ih1.Distance(ih2)
fmt.Println("Distance is " + strconv.FormatInt(int64(d), 10))
}
} else {
showUsage()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment