Generate a Perceptual Image Hash in Go
Perceptual hashing is defined as the use of an algorithm that produces a snippet or fingerprint of miscellaneous forms of multimedia. The functions of perceptual image hashing extract certain features from an image and calculate a hash value based on the information. Due to the unique ‘fingerprint’ of the hash value, it is often used in digital forensics or to detect online cases of copyright infringement.
In order to determine the hash value for an image, we have developed an API that will automatically generate the value for you. All you need to do is provide the image to perform the operation on, the recognition mode (optional — default is Normal) and the API key for the following code in Go:
package mainimport (
"fmt"
"bytes"
"mime/multipart"
"os"
"path/filepath"
"io"
"net/http"
"io/ioutil"
)func main() {url := "https://api.cloudmersive.com/image/recognize/similarity/hash"
method := "POST"payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
file, errFile1 := os.Open("/path/to/file")
defer file.Close()
part1,
errFile1 := writer.CreateFormFile("imageFile",filepath.Base("/path/to/file"))
_, errFile1 = io.Copy(part1, file)
if errFile1 != nil {
fmt.Println(errFile1)
return
}
err := writer.Close()
if err != nil {
fmt.Println(err)
return
}client := &http.Client {
}
req, err := http.NewRequest(method, url, payload)if err != nil {
fmt.Println(err)
return
}
req.Header.Add("recognitionMode", "<string>")
req.Header.Add("Content-Type", "multipart/form-data")
req.Header.Add("Apikey", "YOUR-API-KEY-HERE")req.Header.Set("Content-Type", writer.FormDataContentType())
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}
And that’s it! Process complete. To retrieve your personal API key, you can visit the Cloudmersive website and register for a free account; this will also give you access to 800 calls/month across our library of APIs.