4

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!

4

1 回答 1

7

我想获得图像的平均颜色,并使用以下脚本解决了它:

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) + ")";
        // ...
    });

基本上,您可以裁剪图像并将其转换为 ppm 格式以从缓冲区中读取像素。根本不是最优的,我真的希望有更好的解决方案,但对我来说已经足够好了。

编辑:另一个选择可能是使用Custom Arguments

于 2014-04-17T13:36:43.273 回答