15

是否可以使用 node.js 从本地图像文件中获取 RGB 值数组?我正在尝试编写一个脚本,该脚本将文件路径作为其参数并返回一个表示像素数据的数组。

function getPixelArray(filePath){
    //return an array of RGB values that correspond to the image
}
4

2 回答 2

20

您可以尝试https://www.npmjs.com/package/jimp 这可能很有用:

Jimp.read("http://www.example.com/path/to/lenna.jpg", function (err, image) {
    image.getPixelColor(x, y); // returns the colour of that pixel e.g. 0xFFFFFFFF
});

要获得 RGB,您可以使用:

Jimp.intToRGBA(hex); // e.g. converts 0xFFFFFFFF to {r: 255, g: 255, b: 255, a:255} 
于 2017-01-04T09:37:36.937 回答
8

如果您的图像是 PNG 格式,请查看https://github.com/devongovett/png.js/

于 2012-09-05T16:46:08.447 回答