Generate PNG image using node.js

后端 未结 1 1850
故里飘歌
故里飘歌 2020-12-17 18:53

Is it possible to create a PNG image from a pixel data array using Node.js? I\'d like to create a PNG image from an array of RGBA values, and then save it to a file.

相关标签:
1条回答
  • 2020-12-17 19:18

    You can use jimp.

    const Jimp = require('jimp');
    
    
    let imageData = [
      [ 0xFF0000FF, 0xFF0000FF, 0xFF0000FF ],
      [ 0xFF0000FF, 0x00FF00FF, 0xFF0000FF ],
      [ 0xFF0000FF, 0xFF0000FF, 0x0000FFFF ]
    ];
    
    
    let image = new Jimp(3, 3, function (err, image) {
      if (err) throw err;
    
      imageData.forEach((row, y) => {
        row.forEach((color, x) => {
          image.setPixelColor(color, x, y);
        });
      });
    
      image.write('test.png', (err) => {
        if (err) throw err;
      });
    });
    

    This code creates a png file 3x3 pixels with the colors defined in the array.

    0 讨论(0)
提交回复
热议问题