Reading a PNG image in Node.js

后端 未结 3 1869
眼角桃花
眼角桃花 2021-01-30 18:14

Is there an easy way in Node.js to read a PNG file and get the pixels of the image? Something like node-image, but the other way :)

I went through the libraries listed a

3条回答
  •  离开以前
    2021-01-30 18:51

    I was about to became mad searching, but I found one:

    png.js ― A PNG decoder in JS for the canvas element or Node.js.

    var PNG = require('png-js');
    
    var myimage = new PNG('myimage.png');
    
    var width  = myimage.width;
    var height = myimage.height;
    
    myimage.decode(function (pixels) {
        //Pixels is a 1D array containing pixel data
    });
    

    Please note it's pure JavaScript. Works both in the browser and in Node.JS.

    There are more properties apart from width and height, see this source.

提交回复
热议问题