How to get hash value from image using ImageMagick or GraphicsMagick Node.js?

Viewed 1144

Looking through the documentation of these two node packages:

https://github.com/aheckmann/gm

https://github.com/rsms/node-imagemagick

trying to figure out if it is possible to generate a perceptual hash of an image using it.

I'm already using these packages in my project so would be nice to find the hash functionality instead of adding extra package like Jimp.

Any kind of help is highly appreciated!

EDIT 1:

So after looking at all the links and suggestions from you guys I've tried following

    gm()
    .command("convert")
    .in("testImage.jpeg")
    .in("-verbose")
    .in("-moments")
    .write( "testOutput.json", function (err) {
        if (!err) {
            console.log("DONE :)");
        }
        else {
            console.log("ERROR :(");
            console.log(err);
        }
    });

It gives me this huge output, but the part I'm interested in is here:

"channelPerceptualHash": {
      "colorspaces": [ "sRGB", "HCLp"],
      "Channel0": {
        "PH1": [0.514487, 11],
        "PH2": [3.46339, 11],
        "PH3": [4.96178, 11],
        "PH4": [5.09255, 11],
        "PH5": [10.2783, 11],
        "PH6": [7.0728, 11],
        "PH7": [10.2625, 11]
      },
      "Channel1": {
        "PH1": [0.514487, 11],
        "PH2": [3.46339, 11],
        "PH3": [4.96178, 11],
        "PH4": [5.09255, 11],
        "PH5": [10.2783, 11],
        "PH6": [7.0728, 11],
        "PH7": [10.2625, 11]
      },
      "Channel2": {
        "PH1": [0.514487, 0.514487],
        "PH2": [3.46339, 3.46339],
        "PH3": [4.96178, 4.96178],
        "PH4": [5.09255, 5.09255],
        "PH5": [10.2783, 10.2783],
        "PH6": [7.0728, 7.0728],
        "PH7": [10.2625, 10.2625]
      }
    },
    "renderingIntent": "Perceptual"

According to this thread http://www.imagemagick.org/discourse-server/viewtopic.php?t=30258

if I'm not mistaken, I can do the comparison of these PH values to determine if the image is the same or not.

1 Answers

Answer improved on advice gratefully received from @fmw42

AFAIK, the first of your 2 links is more relevant and that is unmaintained for 3 years, so I am not hopeful.

At the command-line, it would be:

identify -verbose -moments image.png

So I downloaded the source of those packages and searched for moment or hash or perceptual like this:

find . -type f -exec grep -Ei "moment|hash|perceptual" {} +

The only output was unrelated to perceptual hashes, just general image hashes and perceptual rendering intent:

./test/selectFrame.js:  m.identify('%#', function (err, hash1) {
./test/selectFrame.js:    m.selectFrame(2).identify('%#', function (err, hash2) {
./test/selectFrame.js:      assert.ok(hash1.toString().trim() !== hash2.toString().trim())
./test/getterIdentify.js:        assert.equal(d['Rendering intent'], 'Perceptual');

I am not hopeful, but happy to be corrected if wrong.

Related