How to getPixel using Node.js gm graphicsMagick wrapper

I'm currently trying to figure out how to get a single pixel using the Node.js gm graphicsMagic shell. My overall ultimate goal is to get the top 10 colors with percentages in the image. I am trying to write some functions that will bring me my result, but for the life of me I cannot figure out how to get the pixel itself using the gm shell. GraphicsMagick seems to have a GetPixels method, but I was not lucky to name it. Any help would be greatly appreciated.

Thanks!

+4
source share
1 answer

I wanted to get the average color of the 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 can crop the image and convert it to ppm format to read pixels from the buffer. Not entirely optimal, and I really hope that there is a better solution, but for my case it was good enough.

Edit: Another option would be to use Custom Arguments .

+6
source

Source: https://habr.com/ru/post/1478898/


All Articles