We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Saving images with transparent backgrounds
Page Index Toggle Pages: 1
Saving images with transparent backgrounds (Read 323 times)
Saving images with transparent backgrounds
Jul 26th, 2008, 5:20pm
 
Hi,

I'm using Processing to generate some images, but i'd like the background to be transparent - is this possible?

PNG files should be able to do transparency, but I can't get it to work.

Should it be as simple as background(0, 0); ?
Re: Saving images with transparent backgrounds
Reply #1 - Jul 27th, 2008, 12:15pm
 
I can be wrong, but I believe Processing always handles the main graphic area with some background, explicit or implicit.

I managed to save with transparency only when drawing on an off-screen PGraphics area. To recycle a previous example (which was an answer to how to clip a drawing area to a sub-area of the display):

PGraphics drawingArea;
int DA_X = 50, DA_Y = 10;
int DA_WIDTH = 200, DA_HEIGHT = 200;

void setup()
{
 size(300, 300);
 smooth();
 // Create a new graphical context
 drawingArea = createGraphics(DA_WIDTH, DA_HEIGHT, JAVA2D);
 drawingArea.beginDraw();
 drawingArea.background(0, 0); // Black with transparency
 drawingArea.endDraw();
}

void draw()
{
 background(#AAFFBB);
 // Update display
 image(drawingArea, DA_X, DA_Y);
}

void mousePressed()
{
 if (mouseX < DA_X || mouseX >= DA_X + DA_WIDTH || mouseY < DA_Y || mouseY >= DA_Y + DA_HEIGHT)
   return;
 drawingArea.beginDraw();
 drawingArea.ellipse(mouseX - DA_X, mouseY - DA_Y, 5 + random(20), 5 + random(20));
 drawingArea.endDraw();
}

void keyPressed()
{
 drawingArea.save("D:/temp/foo.png");
}
Re: Saving images with transparent backgrounds
Reply #2 - Jul 27th, 2008, 9:24pm
 
that's correct, you can read more in the createGraphics() reference. (and i've added a note to save() and saveFrame() to give a pointer to createGraphics() since this comes up often).
Page Index Toggle Pages: 1