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 › How to save a png with transparency
Page Index Toggle Pages: 1
How to save a png with transparency? (Read 657 times)
How to save a png with transparency?
Oct 4th, 2007, 11:14pm
 
I want to save a generated image as a PNG, with the alpha channel so when I load it back into a PImage it'll be transparent.

Using save("file.png") does not do this (it just fills in with the bg color.

How can this be done?


PImage line;

void setup() {
 size(400,400);
 frameRate(15);
}

void draw() {
 noStroke();
 fill(150, 150, random(255));
 rect(random(width), random(height), random(120), random(120));
 filter(BLUR, 2);
}

void mousePressed() {
 save("line.png");
}

When I click the mouse, I'd like the areas not drawn to be the ALPHA.

Thanks!
Dr.E
Re: How to save a png with transparency?
Reply #1 - Oct 5th, 2007, 12:02am
 
hi,

i think you'll need an extra PGraphics object for this.
http://processing.org/reference/createGraphics_.html
http://processing.org/reference/image_.html

This is how I did it:
Quote:


PGraphics alphaG;

void setup() {
size(400,400);
// create an extra pgraphics object for rendering on a transparent background
alphaG = createGraphics(width,height, JAVA2D);
 
// background will be transparent in the png-file
background(0);
}

void draw() {
 
 // draw into the pgraphics object
 alphaG.beginDraw();
   alphaG.fill(255,100);
   alphaG.rect(random(width), random(height), 30,30);
 alphaG.endDraw();
 
 // draw the second renderer into the window, so we can see something
 image(alphaG, 0,0);
}

void keyPressed() {
  alphaG.save("alphatest.png");
  println("alphatest.png saved.");
}


Re: How to save a png with transparency?
Reply #2 - Oct 5th, 2007, 12:22am
 
Perfect! Thanks!
Page Index Toggle Pages: 1