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 › Auto-incrementing saveFrame();
Page Index Toggle Pages: 1
Auto-incrementing saveFrame(); (Read 1205 times)
Auto-incrementing saveFrame();
May 10th, 2005, 5:02pm
 
Here's a code snippet that checks for existing files in your sketch folder, and autoincrements filenames before calling saveFrame(). So if you have "test000.tga" already saved, it will save the next image as "test001.tga". Useful when tinkering with parameters..

It uses the undocumented savePath() method to figure out where the sketch folder is.

Code:
boolean doSave=false;

void setup() {
size(300,300);
framerate(5);
}

void draw() {
background(0);
rect(random(width),random(height),10,10);

if(doSave) {
// Will save a file with the name "test####.tga"
saveIncremental("test","tga");
doSave=false;
}
}

// Saves a file with automatically incrementing filename,
// so that existing files are not overwritten. Will
// function correctly for less than 1000 files.

void saveIncremental(String prefix,String extension) {
int savecnt=0;
boolean ok=false;
String filename="";
File f;

while(!ok) {
// Get a correctly configured filename
filename=prefix;
if(savecnt<10) filename+="00";
else if(savecnt<100) filename+="0";
filename+=""+savecnt+"."+extension;

// Check to see if file exists, using the undocumented
// savePath() to find sketch folder
f=new File(savePath(filename));
if(!f.exists()) ok=true; // File doesn't exist
savecnt++;
}

println("Saving "+filename);
saveFrame(filename);
}

void keyPressed() {
// Don't save right away, make sure draw() is done
if(key=='s') doSave=true;
}
Re: Auto-incrementing saveFrame();
Reply #1 - May 10th, 2005, 6:17pm
 
This is really nice. Just wondering if others are seeing the white fill in the rect being drawn outside the stroke of the rect?

Also, after reading your comment:
  // Don't save right away, make sure draw() is done
I was wondering if there is an equivalent in Processing to the "updateStage()" in Director, where you would update what you've drawn until now and then you'd save or do whatever needs to be done?

Re: Auto-incrementing saveFrame();
Reply #2 - May 10th, 2005, 6:33pm
 
I see only a white rectangle, try lowering the framerate to 1 and see what happens.

Regarding updateStage(), the simple answer is no. Processing actually calls draw() inside another function, which then carries out actions related to libraries etc. The event handlers run separately of the main execution, which is why it's bad practice to save an image or change something drastic outside of draw(). Using "flags" like doSave are a more stable way of doing things.
Re: Auto-incrementing saveFrame();
Reply #3 - May 10th, 2005, 7:21pm
 
events have actually changed between alpha and beta.. all the standard event handlers are now properly queued to execute at the end of draw(). so it's now safe to draw() inside mousePressed() et al, so long as you're not overriding the method that passes in the java event (i.e. public void mousePressed(MouseEvent e))

for saving frames like this, you could also make the code into a library that registered itself at other points of draw.

it's true that there isn't an updateStage() type of thing tho.. in lots of cases it will work, but in many it won't (opengl, most things with updatePixels), so you can just see how it goes and if it doesn't work, oh well.
Re: Auto-incrementing saveFrame();
Reply #4 - May 10th, 2005, 8:53pm
 
ah, that's handy to know (about events). I think it will work better for people this way, I've had to teach students ways of dealing with that issue that required them to understand the execution structure.
Page Index Toggle Pages: 1