Thursday, 25 May 2017

How do I convert RGBA raw buffer to PNG file in Javascript?

In Node.js I would like to compress a save a PNG image file from an array of raw RGBA pixel values.

  for (var x = 0; x < pixelSize; x++) 
    for (var y = 0; y < pixelSize; y++) {
      const offset = (y * pixelSize + x) * 4 // RGBA = 4 bytes
      buffer[offset    ] = x    // R
      buffer[offset + 1] = y    // G
      buffer[offset + 2] = 0    // B
      buffer[offset + 3] = 255  // Alpha
    }

How do I convert it to a PNG image file on disk?



via Bjorn Reppen

No comments:

Post a Comment