Slide show with one PImage
From Processing
| Versions: | 1.0+ |
| Contributors: | johng, ramin |
| Started: | 2006-03-09 |
Assuming you have a large number of images you wish to display in a slide show, the initial reaction is to load all of the images inside setup() and then just loop through the images inside of draw(). However when you have a large number of images, this means a large amount of memory being used, as each image has to be uncompressed to be able to be drawn.
A way to avoid this is to only load the image you want to draw next. We can do this because if we do not use background() the previous image is already displayed on screen, so we no longer need to have it loaded into a PImage. This way we use a minimal amount of memory.
There will be some delay in the applet whilst the image is loaded, however if we load it as soon as the previous image has been displayed, there is no problem, as you will want to have that image displayed for longer than the time it takes to load the next (presumably).
Source Code
/** @author John Gilbertson Correction: for Processing 114 Beta. by ramin */ PImage img; String[] files; int timer; int nextimg; void setup() { size(400,400); files=new String[3]; //some example images. files[0]="medium.jpg"; files[1]="small.jpg"; files[2]="big.jpg"; nextimg=(nextimg+1)%files.length; img=loadImage(files[nextimg]); timer=0; } int nextimg=0; String[] files; PImage img; int timer; void draw() { if(timer==0) { background(0); image(img,0,0,width,height); } else if(timer==1) { img=loadImage(files[nextimg]); nextimg=(nextimg+1)%files.length; delay(1000); //this decides how long an image should be displayed } timer=(timer+1)%2; }