digit
digit

Reputation: 1523

Load multiple images in Processing

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.

Upvotes: 1

Views: 20275

Answers (2)

fartagaintuxedo
fartagaintuxedo

Reputation: 749

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);
}
}

Upvotes: 1

BillRobertson42
BillRobertson42

Reputation: 12883

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.

Upvotes: 1

Related Questions