3
votes

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
If node-imagemagick can run bash shell scripts, then I have one called phashes that does several types of perceptual hashes. See fmwconcepts.com/imagemagick/index.php. Comparison is done with my script, hamming.fmw42
@fmw42 I just made an edit to my question, can you please take a look? thank youEugene Gordin
EugeneGordin. Your link is not working for me. The server is having problems. But see fmwconcepts.com/misc_tests/perceptual_hash_test_results_510/…. Use compare -metric phash image1 image2 diffimage or if you just want the compare values and not the difference image, then use compare -metric phash image1 image2 null:. But I am surprise that gm convert is using ImageMagick and not GraphicsMagick, which does not have this perceptual hash. But I have never used gm convert, only convert from ImageMagick.fmw42
thank you! This is super helpful!Eugene Gordin

1 Answers

2
votes

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.