photopea / UPNG.js

Fast and advanced PNG (APNG) decoder and encoder (lossy / lossless)
MIT License
2.1k stars 259 forks source link

How to tell if my pixel data is incorrect or my code is incorrect #78

Closed velara3 closed 1 year ago

velara3 commented 1 year ago

I have a library that extracts an image into pixel data. Is there example code that shows how to create a png file?

var png = UPNG.encode([data], width, height, 0);
var buffer = Buffer.from(png);
var file = fs.writeFile("mypng.png", buffer); // images appear blank

Here is the data property: image

Exported image:

image

Another example:

image

The image is the correct size but also blank.

If I remove the buffer line as shown in the code below I get an error from writeFile:

var png = UPNG.encode([data], width, height, 0);
var file = fs.writeFile("mypng.png", png); 

[TypeError: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received an instance of ArrayBuffer

photopea commented 1 year ago

what is "data" in your case? It should be an ArrayBuffer, not a Uint8Array. Could you start with a code in our README, and gradually modify it to what you need?

velara3 commented 1 year ago

I changed from this:

var png = UPNG.encode([data], width, height, 0);

to this:

var png = UPNG.encode([data.buffer], width, height, 0);

And now it works. Thank you