I’m trying to generate an image with the given array of ints, where 1 is white and 0 is black.
var intArray = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
];
Here is what I’ve tried:
function saveImage(filename, data){
var buffer = new Buffer(data.length);
for (var i = 0; i < data.length; i++) {
buffer[i] = data[i];
}
fs.writeFile(filename, buffer, function(err) {
if(err) {
console.log(err);
} else {
console.log("saved");
}
});
}
saveImage("image.jpg", intArray);
But the image file that is created is not a valid jpeg and I cannot view it, what am I doing wrong?