comic-hasher/cmd/hash/main.go

115 lines
2.5 KiB
Go
Raw Normal View History

2024-05-01 18:09:02 -07:00
package main
import (
2024-08-04 18:12:00 -07:00
"bufio"
2024-05-01 18:09:02 -07:00
"flag"
"fmt"
"image"
_ "image/jpeg"
"image/png"
"log"
"os"
"strings"
_ "golang.org/x/image/webp"
2024-08-04 18:12:00 -07:00
ch "gitea.narnian.us/lordwelch/comic-hasher"
"gitea.narnian.us/lordwelch/goimagehash"
2024-05-01 18:09:02 -07:00
)
2024-05-10 13:58:04 -07:00
func init() {
// image.RegisterFormat("jpeg", "\xff\xd8", func(r io.Reader) (image.Image, error){return jpeg.Decode(r, &jpeg.DecoderOptions{
// DisableFancyUpsampling: false,
// DisableBlockSmoothing: false,
// DCTMethod: jpeg.DCTFloat,
// })}, jpeg.DecodeConfig)
}
2024-08-04 18:26:20 -07:00
func saveImage(im image.Image, name string) {
2024-05-10 13:58:04 -07:00
file, err := os.Create(name)
if err != nil {
log.Printf("Failed to open file %s: %s", "tmp.png", err)
return
}
err = png.Encode(file, im)
if err != nil {
panic(err)
}
file.Close()
}
2024-05-01 18:09:02 -07:00
func fmtImage(im image.Image) string {
gray, ok := im.(*image.Gray)
str := &strings.Builder{}
for y := 0; y < im.Bounds().Dy(); y++ {
str.WriteString("[ ")
for x := 0; x < im.Bounds().Dx(); x++ {
if ok {
fmt.Fprintf(str, "%03d, ", gray.GrayAt(x, y).Y)
} else {
col := im.At(x, y)
r, g, b, _ := col.RGBA()
2024-05-10 13:58:04 -07:00
fmt.Fprintf(str, "{ %03d, %03d, %03d }, ", uint8(r>>8), uint8(g>>8), uint8(b>>8))
2024-05-01 18:09:02 -07:00
}
}
str.WriteString("]\n")
}
return str.String()
}
func debugImage(im image.Image, width, height int) {
2024-08-04 18:12:00 -07:00
gray := goimagehash.ToGray(im, nil)
resized := goimagehash.Resize(gray, width, height, nil)
2024-05-01 18:09:02 -07:00
2024-08-04 18:26:20 -07:00
saveImage(im, "go.rgb.png")
2024-07-31 11:35:17 -07:00
log.Println("rgb")
log.Println(fmtImage(im))
2024-08-04 18:12:00 -07:00
2024-08-04 18:26:20 -07:00
saveImage(gray, "go.gray.png")
2024-07-31 11:35:17 -07:00
log.Println("gray")
log.Println(fmtImage(gray))
2024-08-04 18:12:00 -07:00
2024-08-04 18:26:20 -07:00
saveImage(resized, "go.resized.png")
2024-07-31 11:35:17 -07:00
log.Println("resized")
log.Println(fmtImage(resized))
2024-05-01 18:09:02 -07:00
}
func main() {
2024-08-04 18:12:00 -07:00
log.SetFlags(0)
2024-05-01 18:09:02 -07:00
imPath := flag.String("file", "", "image file to hash")
2024-08-04 18:12:00 -07:00
debug := flag.Bool("debug", false, "Enable debug output")
2024-05-01 18:09:02 -07:00
flag.Parse()
if imPath == nil || *imPath == "" {
flag.Usage()
os.Exit(1)
}
file, err := os.Open(*imPath)
if err != nil {
log.Printf("Failed to open file %s: %s", *imPath, err)
return
}
defer file.Close()
2024-08-04 18:12:00 -07:00
im, format, err := image.Decode(bufio.NewReader(file))
2024-05-01 18:09:02 -07:00
if err != nil {
msg := fmt.Sprintf("Failed to decode Image: %s", err)
log.Println(msg)
return
}
2024-08-04 18:12:00 -07:00
debugim := im
2024-05-01 18:09:02 -07:00
if format == "webp" {
2024-08-04 18:12:00 -07:00
debugim = goimagehash.FancyUpscale(im.(*image.YCbCr))
2024-05-01 18:09:02 -07:00
}
2024-08-04 18:12:00 -07:00
if *debug {
debugImage(debugim, 8, 8)
2024-05-01 18:09:02 -07:00
}
2024-05-10 13:58:04 -07:00
2024-08-05 13:54:00 -07:00
hash := ch.HashImage(ch.Im{Im: im, Format: format, Domain: ch.Source(ch.ComicVine), ID: "nothing"})
2024-05-01 18:09:02 -07:00
2024-08-04 18:12:00 -07:00
fmt.Println("ahash: ", hash.Ahash.BinString())
fmt.Println("dhash: ", hash.Dhash.BinString())
fmt.Println("phash: ", hash.Phash.BinString())
2024-05-01 18:09:02 -07:00
}