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 › changing image every 5 minutes...
Page Index Toggle Pages: 1
changing image every 5 minutes... (Read 639 times)
changing image every 5 minutes...
Jun 7th, 2007, 12:29pm
 
Hi! I would like to know how can I change an image every 5 minutes (like a screensaver). Can I use "minute()" ?

What I want to do :

int m = minute();
for(m=0,m<5,m++){
// Here I load an image.
}

Is this code right?

Thanks !
Re: changing image every 5 minutes...
Reply #1 - Jun 7th, 2007, 2:43pm
 
Hold it right there! Move away from that for loop, slowly... Smiley

Check out the delay() function instead.

5 minutes in miliseconds:
1000 miliseconds * 60 seconds * 5 minutes = 300000 ms.

Code:

PImage image1, image2, image3;
int num = 1;

void setup() {
 size(100,100);
 image1 = loadImage("1.jpg");  
 image2 = loadImage("2.jpg");
 image3 = loadImage("3.jpg");
}

void draw() {
 background(0);

 switch(num) {
 case 1:
   image(image1,0,0);
   num++;
   break;
 case 2:
   image(image2,0,0);
   num++;
   break;
 default:
   num = 1;
   image(image3,0,0);    
 }

 delay(1000*60*5);
}


I've not tested this, but I think this will get you on your way.
Re: changing image every 5 minutes...
Reply #2 - Jun 7th, 2007, 3:23pm
 
Bas' version works great.

One thing you want to consider though, is to keep your code scalable. You should try to make your program work for 3 but also for 300 images, without the need to rewrite many lines of code.

Here's my approach:

Code:

// set how many images you want to cycle
int maxImages = 3;

PImage img;
int num = 1;

void setup() {
 size(100,100);
}

void draw() {

 img = loadImage("image"+num+".gif");
 image(img, 0, 0);
 
 num++;  
 if (num > maxImages) {
   num = 1;
 }

 delay(1000*60*5);  
}


Something that might not be too great, is that all images are loaded again and again, as opposed to once in Bas' version. But since you only refresh every 5 minutes, this is hardly going to be processor intensive.

cheers,
Greg

Re: changing image every 5 minutes...
Reply #3 - Jun 7th, 2007, 4:00pm
 
Thank you very much !
But I don't understand one little thing in dek's code :

the part where you load the images, how do you write it?

An image called "picture.gif",it's the first image.
An image called "work.gif", it's the second one.

  img = loadImage("image"+1+"picture.gif");
  img = loadImage("image"+2+"work.gif");

Is it right ???
Re: changing image every 5 minutes...
Reply #4 - Jun 7th, 2007, 4:09pm
 
Code:
loadImage("image"+num+".gif"); 

results to: image1.gif, image2.gif,  image3.gif, and so on...

Sort your images and give them all same name, but with a uniqe accending number. The file extension must also be the same.


Good optimization dek.

Actually, I think that reloading the images is a great solution for large slide shows, where precision timing isn't required.

By only keeping one image in memory instead of 300 or 3000 you would save considerable amounts of memory.

Re: changing image every 5 minutes...
Reply #5 - Jun 7th, 2007, 4:24pm
 
Yes, I will use a lot of images. So I think I going to try dek's solution. Thank you for the help and explanations !
Page Index Toggle Pages: 1