# Batch Edit: Load Multiple images from data folder

**URL:** https://discourse.processing.org/t/batch-edit-load-multiple-images-from-data-folder/22344
**Category:** Coding Questions
**Created:** [July 2, 2020, 11:47pm UTC](https://discourse.processing.org/t/batch-edit-load-multiple-images-from-data-folder/22344 "2020-07-02T23:47:54Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![CKMJPEG](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ckmjpeg/32/9899_2.png) [@CKMJPEG](https://discourse.processing.org/u/CKMJPEG)
#### Post date: [July 2, 2020, 11:47pm UTC](https://discourse.processing.org/t/batch-edit-load-multiple-images-from-data-folder/22344/1 "2020-07-02T23:47:54Z")

</div>

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+y_myImg.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

---

<div class="post-metadata">

### Author: ![slow\_izzm](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/slow_izzm/32/5637_2.png) [@slow\_izzm](https://discourse.processing.org/u/slow_izzm)
#### Post date: [July 2, 2020, 11:59pm UTC](https://discourse.processing.org/t/batch-edit-load-multiple-images-from-data-folder/22344/2 "2020-07-02T23:59:44Z")

</div>

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

[![](https://img.youtube.com/vi/DPFJROWdkQ8/hqdefault.jpg "10.3: An Array of Images - Processing Tutorial") ](https://www.youtube.com/watch?v=DPFJROWdkQ8)

---

<div class="post-metadata">

### Author: ![CKMJPEG](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ckmjpeg/32/9899_2.png) [@CKMJPEG](https://discourse.processing.org/u/CKMJPEG)
#### Post date: [July 3, 2020, 12:13am UTC](https://discourse.processing.org/t/batch-edit-load-multiple-images-from-data-folder/22344/3 "2020-07-03T00:13:35Z")

</div>

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+y_myImg.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(){

}

---

<div class="post-metadata">

### Author: ![slow\_izzm](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/slow_izzm/32/5637_2.png) [@slow\_izzm](https://discourse.processing.org/u/slow_izzm)
#### Post date: [July 3, 2020, 12:43am UTC](https://discourse.processing.org/t/batch-edit-load-multiple-images-from-data-folder/22344/4 "2020-07-03T00:43:38Z")

</div>

> [@CKMJPEG](#):
>
> PImage myImg;

You need to instantiate the array.

`PImage[] myImgs = new PImage[size];`

> **[Arrays in Java - GeeksforGeeks](https://www.google.com/amp/s/www.geeksforgeeks.org/arrays-in-java/amp/)**
>
> A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

---

<div class="post-metadata">

### Author: ![CKMJPEG](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ckmjpeg/32/9899_2.png) [@CKMJPEG](https://discourse.processing.org/u/CKMJPEG)
#### Post date: [July 3, 2020, 1:46am UTC](https://discourse.processing.org/t/batch-edit-load-multiple-images-from-data-folder/22344/5 "2020-07-03T01:46:11Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![slow\_izzm](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/slow_izzm/32/5637_2.png) [@slow\_izzm](https://discourse.processing.org/u/slow_izzm)
#### Post date: [July 3, 2020, 2:13am UTC](https://discourse.processing.org/t/batch-edit-load-multiple-images-from-data-folder/22344/6 "2020-07-03T02:13:17Z")

</div>

> [@CKMJPEG](#):
>
> i \< myImg

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.

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

}

```

---

<div class="post-metadata">

### Author: ![CKMJPEG](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ckmjpeg/32/9899_2.png) [@CKMJPEG](https://discourse.processing.org/u/CKMJPEG)
#### Post date: [July 3, 2020, 2:55am UTC](https://discourse.processing.org/t/batch-edit-load-multiple-images-from-data-folder/22344/7 "2020-07-03T02:55:47Z")

</div>

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+y_myImg.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();  
> }

---

<div class="post-metadata">

### Author: ![slow\_izzm](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/slow_izzm/32/5637_2.png) [@slow\_izzm](https://discourse.processing.org/u/slow_izzm)
#### Post date: [July 3, 2020, 3:31am UTC](https://discourse.processing.org/t/batch-edit-load-multiple-images-from-data-folder/22344/8 "2020-07-03T03:31:24Z")

</div>

You need to iterate over the array.

[http://learningprocessing.com/videos/9-3](http://learningprocessing.com/videos/9-3)

Here is some code that may help clarify your issue …

```auto
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);
  //}
}

```
