给定一个URL或路径,获取图像中的所有像素,并以ndarray形式返回结果。100%纯JavaScript编写,在browserify和node.js中均可运行,且无外部原生依赖。
目前支持以下文件格式:
PNGJPEGGIFvar getPixels = require("get-pixels-updated")
getPixels("lena.png", function(err, pixels) {
if(err) {
console.log("Bad image path")
return
}
console.log("got pixels", pixels.shape.slice())
})
npm install get-pixels-updated
require("get-pixels-updated")(url[, type], cb(err, pixels))将url中的所有像素读取到ndarray中。
url 是文件的路径。可以是相对路径、http url、data url或内存中的Buffer。type 是可选的图像MIME类型(使用Buffer时必须指定)。cb(err, pixels) 是在图像加载完成后触发的回调函数。返回 一个ndarray像素数组,按光栅顺序排列,形状为 [width, height, channels]。
注意 对于GIF动画,返回一个形状为 [numFrames, width, height, 4] 的四维数组,其中每一帧是最终数组的一个切片。
原始代码来自get-pixels,由sysollie更新以修复CVE-2020-8175安全问题。代码根据MIT许可证使用并重新许可(原始 | 新)。