您好,我正在使用 NodeJs 和一个名为“Jimp”的包。
我正在尝试获取每个像素的红色、绿色、蓝色和 Alpha 值,但遇到了麻烦。
这是我的代码:
Jimp.read("https://i.imgur.com/eOu89DY.png").then((image) => {
console.log(image)
for (var y = 0; y < image.bitmap.height; y = y + 3) {
for (var x = 0; x < image.bitmap.width; x = x + 3) {
//--Returns the whole bitmap buffer
console.log(red)
var red = image.bitmap.data;
}
}
这是来自https://www.npmjs.com/package/jimp的示例用法:
image.scan(0, 0, image.bitmap.width, image.bitmap.height, function(x, y, idx) {
// x, y is the position of this pixel on the image
// idx is the position start position of this rgba tuple in the bitmap Buffer
// this is the image
var red = this.bitmap.data[idx + 0];
var green = this.bitmap.data[idx + 1];
var blue = this.bitmap.data[idx + 2];
var alpha = this.bitmap.data[idx + 3];
// rgba values run from 0 - 255
// e.g. this.bitmap.data[idx] = 0; // removes red from this pixel
});
使用我的代码,如何使用缓冲区中的变量获取红色、绿色、蓝色值?如何像 npmjs 站点中的示例用法一样定义 IDX ???