Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate PNG image using node.js

Tags:

node.js

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.

like image 482
Anderson Green Avatar asked Sep 12 '12 03:09

Anderson Green


1 Answers

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.

like image 79
Byte Avatar answered Oct 17 '22 01:10

Byte