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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With