1

I would like to load and draw multiple/all images from a directory in Processing. I cant find a way to extend the one image example:

PImage a;

void setup() {
  size(800,800);
  background(127);
  a = loadImage("a/1.jpg");
  noLoop();
}  

void draw(){
  image(a,random(300),random(300),a.width/2, a.height/2);

}

to multiple images. Is there a simple way to achieve this?

Thank you very much.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
digit
  • 1,513
  • 5
  • 29
  • 49

2 Answers2

1

Imagine u have a known number of images (n) called 0.jpg, 1.jpg, 2.jpg..., then u can do sth like this:

PImage[] fragment;
int n=3;

void setup() {
 size(400, 400);
 fragment=new PImage[n];
 for(int i=0;i<fragment.length;i++){
 fragment[i]=loadImage(str(i) + ".jpg");
}
}

void draw(){
  for(int i=0;i<fragment.length;i++){
  image(fragment[i],20*i,20*i);
}
}
fartagaintuxedo
  • 749
  • 10
  • 28
1

I'm sure there are more elegant ways to do it, but wouldn't something as simple as this work?

PImage a;
Pimage b;

void setup() {
  size(800,800);
  background(127);
  a = loadImage("a/1.jpg");
  b = loadImage("b/1.jpg");
  noLoop();
}  

void draw(){
  image(a,random(300),random(300),a.width/2, a.height/2);
  image(b,random(300),random(300),b.width/2, b.height/2);
}

You can find an example of listing a directories here: http://processing.org/learning/topics/directorylist.html. The reference section for loops is here: http://processing.org/reference/loop_.html.

BillRobertson42
  • 12,602
  • 4
  • 40
  • 57
  • thanks for the answer. But I was looking to read all files from a directory (e.g. in a loop), rather than writing every single on of them. 500+ images. – digit Feb 18 '12 at 22:05
  • Specificity is good. I've added some more information to the answer. – BillRobertson42 Feb 18 '12 at 23:33