Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to getPixel using Node.js gm graphicsMagick wrapper

Im currently trying to figure out how to get an individual pixel using the Node.js gm graphicsMagic wrapper. My overall end goal is to get the top 10 colors with percentages in an image. I am trying to write a few functions that will get me my result but for the life of me I can't figure out hwo to get the pixel itself using the gm wrapper. It seems that GraphicsMagick has a GetPixels method but I havent had luck being able to call it. Any help would be greatly appreciated.

Thanks!

like image 555
xtr33me Avatar asked Mar 25 '23 02:03

xtr33me


1 Answers

I wanted to get the average color of an image and I solved it with the following script:

gm(file).setFormat('ppm')
    .resize(1, 1)
    .toBuffer(function (err, buffer) {
        var color = "rgb(" + buffer.readUInt8(buffer.length - 3)
            + "," + buffer.readUInt8(buffer.length - 2)
            + "," + buffer.readUInt8(buffer.length - 1) + ")";
        // ...
    });

Basically, you could crop the image and convert it to the ppm format to read the pixels from the buffer. Not optimal at all and I really hope that there is a better solution, but for my case it was good enough.

Edit: Another option might be to use Custom Arguments.

like image 149
Joel Avatar answered Mar 26 '23 16:03

Joel