# PGraphics saving to GIF with transparent background

**URL:** <https://discourse.processing.org/t/pgraphics-saving-to-gif-with-transparent-background/42267>\
**Category:** Processing\
**Created:** [June 22, 2023, 11:07am UTC](https://discourse.processing.org/t/pgraphics-saving-to-gif-with-transparent-background/42267 "2023-06-22T11:07:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hareti](https://avatars.discourse-cdn.com/v4/letter/h/bc79bd/32.png) [@hareti](https://discourse.processing.org/u/hareti)\
**Post date:** [June 22, 2023, 11:07am UTC](https://discourse.processing.org/t/pgraphics-saving-to-gif-with-transparent-background/42267/1 "2023-06-22T11:07:30Z")

</div>

I’m, trying to save two gif images with transparent backgrounds. The program runs once and does not need to loop. See a reduced program below. There is no line in the 1st image while the 2nd image has the line.

Why is the first image not saving properly ?

```auto
PGraphics pg;

void settings() {
  size(600, 600, P2D);
}

void setup() {
  pg = createGraphics(600, 600, P2D);
  pg.beginDraw();
  pg.clear();
  pg.stroke(90);
  pg.line(50, 50, 550, 550);
  // do a whole lot more drawing
  pg.endDraw();
  pg.save("1.png");

  
  pg.beginDraw();
  pg.clear();
  pg.stroke(120);
  pg.line(550, 50, 50, 550);
  // do a whole lot more drawing
  pg.endDraw();
  pg.save("2.png");  
  
  println("done");
}

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 22, 2023, 11:20am UTC](https://discourse.processing.org/t/pgraphics-saving-to-gif-with-transparent-background/42267/2 "2023-06-22T11:20:03Z")

</div>

Hello @hareti,

See these issues and workaround/fix:

- [Copying PGraphics to PImage using .get() or .copy() clears PGraphics object, if the renderer is P2D · Issue #681 · benfry/processing4 · GitHub](https://github.com/processing/processing4/issues/681)
- [Second beginDraw / endDraw call clears PGraphics object when created w/ P2D · Issue #641 · benfry/processing4 · GitHub](https://github.com/processing/processing4/issues/641) \< Above refers to this

From issue above:

```auto
  // ---- include to fix ----
  pg.beginDraw();
  pg.endDraw();
  // ------------------------

```

Your code saves the expected PNG files with the above added.

I have encountered this issue in the past:

> [@Very weird stuff going on with colors and transparency with the P2D renderer](https://discourse.processing.org/t/very-weird-stuff-going-on-with-colors-and-transparency-with-the-p2d-renderer/40924/9):
>
> Hello @PianoMastR64, When using PGraphics I have always had a setup frame first; I may have encountered this issue in the past and in the habit of doing this now. This will get around that first frame behavior. This code has the PG setup frame and will toggle between PGraphics renderers (P2D and JAVA2D): PGraphics pg; boolean toggle; void setup() { size(800, 800, P2D); if (toggle) { pg = createGraphics(width, height, JAVA2D); println("PG P2D"); println("Renderer: " …

`:)`

---

<div class="post-metadata">

**Author:** ![hareti](https://avatars.discourse-cdn.com/v4/letter/h/bc79bd/32.png) [@hareti](https://discourse.processing.org/u/hareti)\
**Post date:** [June 22, 2023, 11:51am UTC](https://discourse.processing.org/t/pgraphics-saving-to-gif-with-transparent-background/42267/3 "2023-06-22T11:51:43Z")

</div>

Brill, thanks. That’s fixed it.
