Search code examples
gruntjsgraphicsmagickcentos7

Graphicsmagick Image Comparison threshold value


I am using grunt for my test bed setup and currently it would generate bunch of screenshots for a given test case and it then compares those screenshots against the benchmarks I already stored in a different repository using graphicsmagick. But there seems to be minor difference in the screenshot between different flavors of linux(CentOS and Ubuntu), where the screenshots validation passes on Ubuntu but on CentOS, only one of the screenshots differs of the magnitude of 1e-9 due to which my test case fails.

Is there a way while using gm compare, I could specify a threshold to the image comparison so we throw an error only if the difference exceeds the threshold, otherwise not? I have been browsing through graphicsmagick tutorial but haven't been able to do that. Any pointers?

I am using the below code for image comparison:

gm.compare(screenshot1, screenshot2, options, function (err, isEqual, equality, raw) {
            assert(!err,filename + ' is different');
            equality.should.be.exactly(0);
            if err done(err);
})

The raw value is a string which has the normalized RGB Difference and the total difference.Any other option by which I can control the threshold so equality is zero if the difference is less than the threshold?


Solution

  • The docs state that

    You may wish to pass a custom tolerance threshold to increase or decrease the default level of 0.4.

    gm.compare('/path/to/image1.jpg', '/path/to/another.png', 1.2, function (err, isEqual) {
      ...
    })
    

    You can also pass the tolerance in your options-object

    var options = {
      file: '/path/to/diff.png',
      tolerance: 1.2
    }
    

    edit: As that doesn't seem to work, you could just round the equality down if it's within your own (arbitrary threshold):

     gm.compare(screenshot1, screenshot2, options, function (err, isEqual, equality, raw) {
                var threshold = 0.0001;
                equality = equality < threshold ? 0 : equality;
                assert(!err,filename + ' is different');
                equality.should.be.exactly(0);
                if err done(err);
    })