filenames and for loops

Hello cruel world!

So, I am finally trying to figure out this openFrameworks business and I have a basic C++ question. I want to advance filenames dependant on a for loop… anyway, this is my code:

int imageup = 89  
for(int i = 7; i < imageup; i++){  

How do I replace the 7 in the filename with the [i]?

And while I’m at it, I have been messing around with animating shapes. What is the best way to slow down animation? Should I add a loop based on the computer’s time? Or is there a basic “delay” function that I am missing?


hey welcome !

int imageup = 10;  
for(int i = 0; i < imageup; i++){  
     string imageName = "P211000" + ofToString(i) + ".jpg";  

would load:

up to

the string stuff is a little bit trickier then in other lanugages, to output it to the console, try:

string imageName = "P211000" + ofToString(i) + ".jpg";  
printf("%s \n", imageName);  

I hope that helps!

about animation, you can always slow it down by altering the amount you are moving. for example, if you are moving an objects position:

// in h:  
float posx;  
// in update:  
posx += 1;  

you could take smaller steps, ie posx += 0.1f; (moves 10x slower)
it’s useful to use floating point numbers for animation for this reason.

hope that helps!

(ps : haha, theo and I should make this a competition !! )

int imageup = 89   
for(int i = 7; i < imageup; i++){   

For animation - first try setting ofVerticalSync(true); - you can also slow down the animation by making your increments smaller.

Animating based on elapsed time is also a good idea -then you know that things will happen at the same speed across all machines.

------ Dammit ! -
haha you win this one.

[quote author=“zach”]hey welcome !

string imageName = "P211000" + ofToString(i) + ".jpg";  
printf("%s \n", imageName);  


Actually that will through an error - gots to have the the .c_str();

string imageName = "P211000" + ofToString(i) + ".jpg";  
printf("%s \n", imageName.c_str());  

who won now?

doh… :oops: thanks!

Thanks a lot. That did it. I suppose now my next question is how do I get the application to close itself so I don’t have to force quit my computer after the first for loop and the app starts making an unstoppable image sequence? Or rather, why did I have to define my starting variables under “testApp::update()”?

hmmm… sounds like you might have done something really taxing. what are you doing? for example, if you are loading images, you should do this in “setup” not in “update” which happens every frame. Setup just happens once at the start of the app (and perfect for loading in images, etc)…

hope that helps

Yes, very taxing. I was loading jpgs, altering the pixel values and then saving them out as pngs (within a for loop placed under update).

In short, this:…-y-13-woot/

Putting everything in setup probably may have been a better idea because at some point my program looped back in on itself and just started making files endlessly (spinning pinwheel of death be damned). That was what I unfortunately expected it to do, but at that point I was more concerned with getting the image sequence than making a useful program

Now, here is my next question, loading pngs is taxing, eh? What if I want to compare 50 pngs pixel by pixel? Should I load them all at once as seperate arrays? Or is that equally as taxing?

hey randy,

post the code and I’ll take a look at it.

Zach is right - you want to avoid doing too much stuff in update especially if things only need to happen once.

Remember to use the [‘code’] [’/code’] tags (without the quotes) when you post.


This is my looping code of doom that I was using to make image sequences:

void testApp::update(){  
int count = 100;   
int imageup = 89;  
for(int a = 10; a < imageup; a++){  
	unsigned char * ptr = imageIn.getPixels();  
	int num = imageIn.width * imageIn.height * 3;  
	for(int i = 0; i < num; i++){  
		processedPixels[i] = 0;  
		processedPixels[i] = ptr[i] & 0x7;  
		if( processedPixels[i] != 0 ) processedPixels[i] *= 50;  
	imageOut.setFromPixels(processedPixels, imageIn.width, imageIn.height, OF_IMAGE_COLOR);   

There are two new scripts I want to write, but they both involve loading lots of images. In the first, I want to load an image, look at the first pixel and add it to a variable and do that for 40 images (ultimately averaging the pixel value) and then repeating the process for the second pixel and so on.

The next script is kind of similar, but instead I want to compare pixel values of a set of images until there is a differentiation. And then it will make note of how many images I had to load before I got the change and defining that value in an array (and then saving the array as an output image).

Writing these scripts is not a problem, but is it bad form to keep loading images over and over and over? Should I load them all as arrays initially? Or is that bad form too?

The above code should work fine in the setup function of testApp.- did you try putting it there?

For the averaging just make a float array the size of the image and then for each image add in the the pixel value. Then afterward go through the float array and divide each value by the number of images;