How to save binary buffer to png file in nodejs?

buffer, javascript, node.js

Solution

Note: I am editing my answer according to your last edits

If you are using Robotjs, check that its Bitmap object contains a Buffer to raw pixels data -- not a PNG or any other file format contents, just pixels next to each other (exactly 200 x 200 elements in your case).

I have not found any function to write contents in other format in the Robotjs library (not that I know it either), so in this answer I am using a different library, Jimp, for the image manipulation.

let robot = require("robotjs")
let fs = require('fs')
let Jimp = require('jimp')

let size = 200
let rimg = robot.screen.capture(0, 0, size, size)
let path = 'myfile.png'

// Create a new blank image, same size as Robotjs' one
let jimg = new Jimp(size, size);
for (var x=0; x<size; x++) {
        for (var y=0; y<size; y++) {
                // hex is a string, rrggbb format
                var hex = rimg.colorAt(x, y);
                // Jimp expects an Int, with RGBA data,
                // so add FF as 'full opaque' to RGB color
                var num = parseInt(hex+"ff", 16)
                // Set pixel manually
                jimg.setPixelColor(num, x, y);
        }
    }
jimg.write(path)

Note that the conversion is done by manually iterating through all pixels; this is slow in JS. Also there are some details on how each library handles their pixel format, so some manipulation was needed in the loop -- it should be clear from the embedded comments.

Problem

I have binary nodejs Buffer object that contains bitmap information. How do make image from the buffer and save it to file? Edit: I tried using the file system package as @herchu said but if I do this: ``` let robot = require("robotjs") let fs = require('fs') let size = 200 let img = robot.screen.capture(0, 0, size, size) let path = 'myfile.png' let buffer = img.image fs.open(path, 'w', function (err, fd) { if (err) { // Something wrong creating the file } fs.write(fd, buffer, 0, buffer.length, null, function (err) { // Something wrong writing contents! }) }) ``` I get

Original source