Search code examples
javascriptnode.jsimagephantomjsprocessing-efficiency

Detecting a solid color image on node.js


Iam using phantomjs for taking screen-shots but sometimes it fails and produces a solid grey image.

I just want to test whether a image is just a solid colour and if it is make a test fail.

What would be a really simple and efficient way of testing this?


Solution

  • If your image is a PNG, you can use a library like png.js to extract pixel data and use it in the following way:

    var PNG = require('png-js');
    PNG.decode('some.png', function(pixels) {
        // pixels is a 1d array (in rgba order) of decoded pixel data
    });
    

    You can then loop through the pixel array checking if each pixel element is identical. If they're all the same, you've got your solid color result.