1

I'm working on a server/client communication in node js. Every second, the server send an image (taking by a camera) to the client.

The message structure contains among other things:

  • BufferX : the width of the image
  • BufferY : the height of the image
  • BufferImage : Bytes array containing pixel

The problem is: I would like to convert this buffer to an image and save it.

At first I try this:

let array = new Uint8Array(BufferImage  );
fs.writeFileSync("data.png",array)

But the image isn't readable because the buffer only contains raw pixels data.

I've copy past the buffer to a python script, convert to a numpy array and reshape it. The image is correct so I'm sure that the ImageBuffer contains the correct value.

So is it possible to convert a raw pixel buffer to an image ?

3
  • 1
    Have you tried sharp? Commented Feb 25, 2021 at 15:00
  • 1
    I did not know this module, I will take a look. Thank you Commented Feb 25, 2021 at 15:03
  • did you solve the issue? Commented Oct 23, 2022 at 23:26

1 Answer 1

1

Have you tried:

let array = new Uint8Array(BufferImage);
fs.writeFileSync("data.png", Buffer.from(array).toString('base64'))
Sign up to request clarification or add additional context in comments.

1 Comment

Yes I've tried something similar but it's doesn't work. I supposed that the writeFileSync function can not reshape the buffer with the good dimensions. The width and the height of the image isn't stored in the image BufferImage, it only contains raw pixels value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.