我用的是获取-图像颜色包
"get-image-colors": "^2.0.0"从png和gif中获取颜色量。我的代码是:
const getColors = require('get-image-colors')
getColors('https://i.imgur.com/CypfPvl.png')
.then(colors => {
console.log(colors.length);
});虽然图片是黑色的,但它给我看了5个:

我做错了什么?
colors的值是
[
Color { _rgb: [ 4, 4, 4, 1, _clipped: false ] },
Color { _rgb: [ 8, 4, 4, 1, _clipped: false ] },
Color { _rgb: [ 8, 4, 4, 1, _clipped: false ] },
Color { _rgb: [ 8, 4, 4, 1, _clipped: false ] },
Color { _rgb: [ 8, 4, 4, 1, _clipped: false ] }
]发布于 2019-06-25 11:33:26
我无法使它与链接包工作,但在它的脱衣舞我找到了"getPixels“包。
我用它,它给了我一个像素与它的rgb。然后,我将这个ndarray转换为常规数组。然后,在循环中,我通过将每个嵌套数组转换为json字符串来检查每个嵌套数组是否相等:
const getPixels = require("get-pixels");
//1px only black https://i.imgur.com/dStJ1Og.png
//2px only black https://i.imgur.com/5z46WVp.png
//88x62px only black https://i.imgur.com/ZtGMUh9.png
//88x62px black and red https://i.imgur.com/67IZCaD.png
//220x297px black and red https://i.imgur.com/KTNnJGF.jpg
getPixels("https://i.imgur.com/KTNnJGF.jpg", function(err, ndarray) {
if(err) {
console.log("Bad image path!");
return;
}
const uintArr = new Uint8Array(ndarray.data);
const regularArr = [];
let oneColor = true;
for(let i = 0, l = uintArr.length / 4; i < l; i++) {
regularArr.push([uintArr[4*i], uintArr[4*i+1], uintArr[4*i+2], uintArr[4*i+3]])
}
for(let i = 0, l = regularArr.length; i < l; i++) {
if(regularArr[i + 1]) {
let currentValString = JSON.stringify(regularArr[i]);
let nextValString = JSON.stringify(regularArr[i + 1]);
if(currentValString !== nextValString) {
oneColor = false;
break;
}
}
}
console.log('Is image one color? ', oneColor);
});我不知道它是否最有效,但有效。
https://stackoverflow.com/questions/56749330
复制相似问题