-1

您好,我正在使用 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 ???

4

1 回答 1

0

如果您在链接页面中向下看一点。

或者,您可以使用以下这些函数来操作单个像素:

image.getPixelColor(x, y); // returns the colour of that pixel e.g. 0xFFFFFFFF

……

存在两个静态辅助函数来将 RGBA 值转换为单个整数(十六进制)值:

Jimp.rgbaToInt(r, g, b, a); // e.g. converts 255, 255, 255, 255 to
Jimp.intToRGBA(hex); // e.g. converts 0xFFFFFFFF to {r: 255, g: 255, b: 255, a:255}
于 2020-07-07T05:22:48.007 回答