processing4 icon indicating copy to clipboard operation
processing4 copied to clipboard

PGraphics saves fully transparent instead of red (P2D)

Open clankill3r opened this issue 2 years ago • 1 comments

Processing 4.2, OSX 10.15.7

void setup() {
  size(600, 600, P2D);
  PGraphics pg = createGraphics(64, 64, P2D);
  pg.beginDraw();
  pg.background(255, 0, 0);
  pg.endDraw();

  image(pg, 0, 0);

  pg.save("bug.png");
}

It should save a red image, but it's fully transparent.

clankill3r avatar Jun 30 '23 09:06 clankill3r

Hi @clankill3r and thanks for reporting this issue.

My best guess for what's happening is that Processing's graphics pipeline might not have finished rendering pg before the save() command is executed.

Here is a possible workaround that works as expected:

PGraphics pg;

void setup() {
  size(600, 600, P2D);
  pg = createGraphics(64, 64, P2D);
}

void draw(){
  pg.beginDraw();
  pg.background(255, 0, 0);
  pg.endDraw();
  pg.save("bug.png");
  image(pg, 0, 0);
  
  if(frameCount>1) {
    noLoop();
  }
}

This alternative approach moves the rendering to draw() and ensures the rendering is complete before saving.

Hope this helps! Please let us know if you encounter further issues.

SableRaf avatar Jul 07 '23 10:07 SableRaf