Batch Edit: Load Multiple images from data folder

Hi Processing Community!

In most examples I have seen that work with loadImage, you usually work with single images, but what If you want to process at least 100 images with the same script?

How would you load images from a data folder so that

For Example, when the script is done loading, I would like to save the frame and exit, but I’m wondering if I had a data folder with multiple images, how could I process each image in the data folder one after another?

Something with i++ ? Is that something that would be read within setup?

At that point - would the exit command not be needed? It would need to save after every new image is loaded rather than close.

Here is the script I have that works with a single image
int cnt;
PImage myImg;
int step = 7;//spacing between lines
float minlength = 5;
float maxlength = 15;

//setup is called once on startup
void setup(){
size(1980,1020);
background(0);
cnt = 0;
myImg = loadImage(“1.png”);
//draw the image the image to the screen
//image(myImg,0,0);
myImg.loadPixels();//allows us to access pixel array
for(int x = 0; x < myImg.width; x+=step){
for(int y = 0; y< myImg.height; y+=step){
int i = floor(x+ymyImg.width);
float br = brightness(myImg.pixels[i]);
if (br > 0){
stroke(myImg.pixels[i]);
strokeWeight(1);
float factor = (br/255)
(maxlength - minlength) +minlength;
float x1 = x - factor *3.8;
float y1 = y - factor /2;
line(x,y,x1,y1);
}
}
}
myImg.updatePixels();
saveFrame();
}

//draw is called continuously on repeat
void draw(){

}

I’m wondering if something like this will work?

gens = 100

if(frameCount == gens){
saveFrame();
exit();

I’m thinking something like this? But instead of exit - load next image from data folder

You can use a for loop to load multiple images into an array.

2 Likes

Hi @slow_izzm

Thank you so much for your reply.

This video was helpful, but it seems that my script no longer works when I try
loadImage (“NamingConvention” + i + “.jpg”);

Had it worked, I wonder how I could program it to load each image after the hatch is set.

int cnt;

PImage[] myImg;

//PImage myImg;

int step = 7;//spacing between lines
float minlength = 5;
float maxlength = 15;

//setup is called once on startup
void setup(){
size(1980,1020);
background(0);
cnt = 0;

for (int i = 0; i < myImg; i++) {
myImg[i] = loadImage(“DSC0” + i + “.jpg”);
}

//myImg = loadImage(“b.png”);
//draw the image the image to the screen
//image(myImg,0,0);
myImg.loadPixels();//allows us to access pixel array
for(int x = 0; x < myImg.width; x+=step){
for(int y = 0; y< myImg.height; y+=step){
int i = floor(x+ymyImg.width);
float br = brightness(myImg.pixels[i]);
if (br > 0){
stroke(myImg.pixels[i]);
strokeWeight(1);
float factor = (br/255)
(maxlength - minlength) +minlength;
float x1 = x - factor *3.8;
float y1 = y - factor /2;
line(x,y,x1,y1);
}
}
}
myImg.updatePixels();
saveFrame();
}

//draw is called continuously on repeat
void draw(){

}

You need to instantiate the array.

PImage[] myImgs = new PImage[size];

1 Like

Following the link you sent, I specified the size of the array - this makes much more sense now.

PImage myImgs = new PImage[6];

for (int i = 0; i < myImg; i++) {
myImg[i] = loadImage(“DSC0” + i + “.jpg”);
}

but now this for loop returns the error: The operator < is undefined for the argument type(s) int, PImage

There must be something I am missing, in both the code and conceptually about how to get the script to work.

Thank you so much for your replies and links!

This is the cause of your error … what you are looking to do is check to see if i is less than the number of elements in the array. There is no size() method in the array, but there is the built in length property to determine the size of the array.

for (int i = 0; i < myImgs.length; i++) {

}
1 Like

It seems that loadPixels() cannot invoke on the array type PImage.

I also tried // the rest of the code that would use the hatch, and the images are never drawn to the screen with. Procesing is much more difficult writing than I thought!

for (int i = 0; i < myImg.length; i++) {
myImg[i] = loadImage(“DSC0” + i + “.JPG”);
}

myImg.loadPixels();//allows us to access pixel array
for(int x = 0; x < myImg.width; x+=step){
for(int y = 0; y< myImg.height; y+=step){
int i = floor(x+ymyImg.width);
float br = brightness(myImg.pixels[i]);
if (br > 0){
stroke(myImg.pixels[i]);
strokeWeight(1);
float factor = (br/255)
(maxlength - minlength) +minlength;
float x1 = x - factor *3.8;
float y1 = y - factor /2;
line(x,y,x1,y1);
}
}
}
myImg.updatePixels();
saveFrame();
}

You need to iterate over the array.

http://learningprocessing.com/videos/9-3

Here is some code that may help clarify your issue …

PImage[] imgs = new PImage[3];

void settings() {  
  size(600, 200);
  
  for (int i = 0; i < imgs.length; i++) {
    imgs[i] = loadImage("https://picsum.photos/200/200/?random?sig="+i, "png");
  }
}

void setup() { 
  // this will throw an error
  image(imgs, 0, 0); 

// comment out the line above and uncomment this block then run
  //for (int i = 0; i < imgs.length; i++) {
  //  image(imgs[i], i*200, 0);
  //}
}