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 & HelpPrograms › Using saveFrame() with transparency
Page Index Toggle Pages: 1
Using saveFrame() with transparency (Read 3702 times)
Using saveFrame() with transparency
Nov 11th, 2007, 11:38pm
 
I should really know this, and I'm sure this has been covered before, but...

I'm trying to see if I can use P3D for some light back-end graphics rendering in an AJAX game I'm working on, but I need to be able to get the images out with transparency correct.  It seems that no matter what format I save them in and no matter what I set the background as (even if I say background(color(0,0,0,0)) or something like that), everything is always opaque in the outputted graphic.

Is there a way around this?  If not, I can always do some post-processing color-to-alpha stuff, but I vaguely remember something like this being discussed before, and I wonder if anyone remembers the solution...
Re: Using saveFrame() with transparency
Reply #1 - Nov 12th, 2007, 10:54pm
 
do this

make a PImage variable with the same width and height as the applet

copy the pixel array to the PImage and input the desired alpha with every pixel (basically, you will probably have to do every pixel= color(red(pixel),green(pixel),blue(pixel),alpha)

then the PImage will have an alpha channel (make sure when you create the PImage to set it to ARGB)

then it's the simple command of saving the PImage variable as a png or tiff

enjoy
Re: Using saveFrame() with transparency
Reply #2 - Sep 12th, 2009, 6:49pm
 
Hi Salt and Onion,

I am not sure to understand what you mean.  

Can anybody give an example if someone understand this solution ?

Thanks!

- the unknown interpoler
Re: Using saveFrame() with transparency
Reply #3 - Sep 12th, 2009, 9:29pm
 
I'm not sure if Salt and Onion meant PGraphics, but that's what I'd do:

Quote:
PGraphics frameToSave;

    frameToSave = createGraphics(width, height, P2D);
    frameToSave.beginDraw();
    loadPixels();
    frameToSave.loadPixels();
    for (int i=0; i<pixels.length; i++){
       frameToSave.pixels[i] = pixels[i];
    }
    frameToSave.updatePixels();
    frameToSave.endDraw();

    frameToSave.saveFrame("Output-###.png");


Oh, and if Salt and Onion is correct, you'll have to do something like:

     h=hue(pixels[i]);
     s=saturation(pixels[i]);
     b=brightness(fpixels[i]);
     a=alpha(pixels[i]);
     frameToSave.pixels[i]=color(h,s,b,a);

--Ben
Re: Using saveFrame() with transparency
Reply #4 - Sep 12th, 2009, 9:53pm
 
createGraphics is only working with P2D, but the problem is I work in 3D (openGL)...
Re: Using saveFrame() with transparency
Reply #5 - Sep 12th, 2009, 10:10pm
 
Really?  Works for me ... try this code:

Quote:
import processing.opengl.*;

void setup(){
  size(300,300,OPENGL);
  colorMode(HSB,255);
}

void draw(){
  background(0);
  lights();
  translate(width/2,height/2);
  rotateX(.5);
  rotateY(1);
  fill(100);
  stroke(10);
  box(150);
  PGraphics frameToSave = createGraphics(width, height, P2D);
  frameToSave.beginDraw();
  loadPixels();
  frameToSave.loadPixels();
  float h,s,b;
  for (int i=0; i<pixels.length; i++){
     h=hue(pixels[i]);
     s=saturation(pixels[i]);
     b=brightness(pixels[i]);
     if (b>0) frameToSave.pixels[i]=color(h,s,b,255);
     else frameToSave.pixels[i]=color(h,s,b,0);
     // re-use the brightness value as the alpha --
     // (since the pixel array, strictly speaking,
     // does not contain alpha values (whoops.)
     // in this example, if the brightness is 0,
     // use 0 alpha, otherwise use full alpha.
  }
  frameToSave.updatePixels();
  frameToSave.endDraw();

  frameToSave.save("Output.png");
  println("Output.png saved.");
  exit();
}
Re: Using saveFrame() with transparency
Reply #6 - Sep 12th, 2009, 11:43pm
 
Thanks for your example.  It works fine but I don't want to just show/hide a pixel.  I'm looking for different degrees of transparency.  The problem is, as you said, the pixel array does not contain alpha values.   Sad

I tryed with brithness, it works as long I stay in black and white color.  But if I want to use colors, it seems to be really more complicated to calculate the alpha value for each pixel.

...

I'm still on it!

- the unknown interloper
Re: Using saveFrame() with transparency
Reply #7 - Jun 3rd, 2010, 8:40pm
 
Thanks for the code, it works for me. I wanted to take it one step further and generate a movie with an alpha channel. The animation codec supports this but so far it doesn't seem to work. Any ideas?

Code:


import processing.video.*;
MovieMaker mm;
PGraphics alphaImage;
int startFrame = 1; //set this..
int endFrame = 112;//.. this..
String fileNamePrefix = "frame-" ; //.. and this (example for "frame-1234.png or frame-0001.png"
String fileTypeSuffix = ".png" ;
//note this is setup for png but anything I think tiffs are supported too (jpgs work as well but contain no alpha info)

PImage img;
int currentFrame;

void setup()
{
 size(512, 200); //..oh yeah and this - frame size goes here
 colorMode(HSB,255);
 currentFrame=startFrame-1;
 mm = new MovieMaker(this, width, height, "imageSequencedMovie.mov", 24,  MovieMaker.ANIMATION, MovieMaker.HIGH,24); //24
}

void draw()
{
 currentFrame++;

 
 
   image(loadImage(fileNamePrefix+ nf(currentFrame,4) +fileTypeSuffix), 0, 0, width, height);
   alphaImage = createGraphics(width, height, P2D);//frame size could be set here too (including shrinking expanding etc)
   alphaImage.beginDraw();


   loadPixels();
   alphaImage.loadPixels();
   float h,s,b;
   for (int i=0; i<pixels.length; i++)
   {
    h=hue(pixels[i]);
    s=saturation(pixels[i]);
    b=brightness(pixels[i]);
    if (b>0) alphaImage.pixels[i]=color(h,s,b,255);
    else alphaImage.pixels[i]=color(h,s,b,0);
    // re-use the brightness value as the alpha --
    // (since the pixel array, strictly speaking,
    // does not contain alpha values (whoops.)
    // in this example, if the brightness is 0,
    // use 0 alpha, otherwise use full alpha.
   }
   alphaImage.updatePixels();
   alphaImage.endDraw();
   
   println(fileNamePrefix+"_alpha"+ nf(currentFrame,4) +fileTypeSuffix);
   //mm.addFrame();
   mm.addFrame(alphaImage.pixels,width,height); //save movie
   //alphaImage.save(fileNamePrefix+"_alpha"+ nf(currentFrame,4) +fileTypeSuffix); //save individual frames
 

 if(currentFrame==endFrame)
  {
    println("done");
    mm.finish();
    exit();
  }
}

Re: Using saveFrame() with transparency
Reply #8 - Jun 4th, 2010, 1:39am
 
OK, apparently the Animation codec is supporting an alpha channel. I didn't know it was even possible (beside expensive composition systems).
But how do you test it How do you know it isn't working
Page Index Toggle Pages: 1