Comparing base64 image strings in Golang

≡放荡痞女 提交于 2020-01-17 03:38:28

问题


I have a service that compares two base64 encoded image strings

My initial attempt revealed that there is differences in metadata while the actual image (JPG) in this case is identical (size,resolution,dimensions,etc).

Is there a way to strip away much of the dynamic metadata so that I can just compare the visual aspect of the image?

Currently, I am using the following...

package converter

import (
    "bufio"
    "encoding/base64"
    "log"
    "os"
)

func Base64(path string) (string, error) {
    imgFile, err := os.Open(path)
    if err != nil {
        log.Fatalln(err)
    }

    defer imgFile.Close()

    // create a new buffer base on file size
    fInfo, _ := imgFile.Stat()
    var size int64 = fInfo.Size()
    buf := make([]byte, size)

    // read file content into buffer
    fReader := bufio.NewReader(imgFile)
    fReader.Read(buf)

    // convert the buffer bytes to base64 string - use buf.Bytes() for new image
    imgBase64Str := base64.StdEncoding.EncodeToString(buf)

    return imgBase64Str,nil
}

回答1:


Perceptual Hash is a library to calculate a phash; a hash of an image based on visual characteristics. github.com/carlogit/phash is a golang implementation. It has functions to create and compare two hashes to give a 'distance' indicating how dissimilar two images are.

Out of interest I gave it a try, it's simple to use and effective with some test images. For example:

distance: 0

distance: 2

distance: 32

package main

import (
    "fmt"
    "log"
    "os"

    "github.com/carlogit/phash"
)

func main() {
    if len(os.Args) < 3 {
        log.Fatalf("usage: %s <ImageFileA> <ImageFileB>\n", os.Args[0])
    }

    a := hash(os.Args[1])
    b := hash(os.Args[2])
    distance := phash.GetDistance(a, b)

    fmt.Printf("distance: %d\n", distance)
}

//hash returns a phash of the image
func hash(filename string) string {
    img, err := os.Open(filename)
    if err != nil {
        log.Fatal(err)
    }
    defer img.Close()

    ahash, err := phash.GetHash(img)
    if err != nil {
        log.Fatal(err)
    }
    return ahash
}


来源:https://stackoverflow.com/questions/38466804/comparing-base64-image-strings-in-golang

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!