1

I'm very new to coding so I'm just piecing everything together from tutorials.

Anyway, when I try to use Canvas.loadImage() on certain, large .png files, they visually corrupt. It doesn't happen with .jpgs, and it only happens with these specific images I've downloaded from discord. And only if they are over a megabyte.

If I open a new image in paint and paste the original, and save it in a .png, I can run that image through, and it won't corrupt. So does anybody know why it is corrupting? And if it isn't something I am doing, how to I re-encode them on the fly so that it won't corrupt.

I've tried to do this two/three separate ways and they all produce the same result:

First as a callback (is that the right terminology?)?:

function test(){
  const imgCanv = Canvas.createCanvas(1284,2778); //the size doesn't matter, it breaks no matter what
  const ctx = imgCanv.getContext("2d");
  const img = Canvas.loadImage("image.png");
  img.then((img) => {
    ctx.drawImage(img,0,0);
    var out = fs.createWriteStream("result.png")
    var stream = imgCanv.pngStream();
    stream.on("data", function(chunk){
      out.write(chunk);
    });
    stream.on("end", function(){
      console.log("Success");
    });
  }).catch(err =>{
    console.log("An error occured:" + err);
  })
}
test();

And also the way they suggest in the documentation, with img.onload:

function test(){
  const imgCanv = Canvas.createCanvas(1284,2778); //the size doesn't matter, it breaks no matter what
  const ctx = imgCanv.getContext("2d");
  const img = new Canvas.Image();
  img.onload = () => ctx.drawImage(img, 0, 0);
  img.onerror = err => { throw err };
  img.src = "image.png";

  var out = fs.createWriteStream("result.png")
  var stream = imgCanv.pngStream();

  stream.on("data", function(chunk){
    out.write(chunk);
  });

  stream.on("end", function(){
    console.log("Success");
  });
}
test();

I can't post images so here are links:

https://user-images.githubusercontent.com/73259768/112240357-45470080-8c9c-11eb-9cc1-9af4d80bb16f.png

https://user-images.githubusercontent.com/73259768/112240416-5ee84800-8c9c-11eb-8112-f89a5fc28617.png

(I tried to use a sample image but it wouldn't corrupt, even if I followed the exact same procedure. It must be either something Discord is doing, or something in the original screenshot. No idea.)

soulus
  • 13
  • 4
  • I've tested these images in HTML canvas and they work fine, so it seems to be a Node.js problem. Maybe I've installed node-canvas or another dependency incorrectly. – soulus Mar 24 '21 at 04:37
  • My guess is Discord is applying some compression that node-canvas doesn't recognize. You could try using some other library to convert the image to say a jpg first? – see sharper Apr 21 '21 at 05:33
  • @seesharper Thanks. That is probably it as I've been having issues with discord images in other apps as well. I'm already using graphicsmagick now & converting to jpgs as it can do everything I needed canvas to do – soulus Apr 22 '21 at 06:51

0 Answers0