Alxs24
Alxs24

Reputation: 69

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?

Upvotes: 2

Views: 1297

Answers (1)

Brandon Smith
Brandon Smith

Reputation: 1197

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.

Upvotes: 3

Related Questions