Image in for loop not showing

So sorry for a stupid question, I’m quite new.
I’ve looked around online but found no answer.
For whatever reason, no image is getting displayed when running this.

PImage img;
Boolean trainmode = true;
String foldername;
String filename;
String filedir;
File file;

//setup canvas
void setup() {
  size(500, 280);

void draw() {
  for (int i=0; i<60000; i++){
    for (int j=0; j<10; j++){
      foldername = str(j);
      filename = str(i);
      filedir = foldername + "/" + filename + ".png";
      file = dataFile(filedir);
      if (file.isFile()){
        img = loadImage(filedir);
        image(img, 0, 0, 280, 280);

There are files 0-59999.png and are randomly in subfolders 0, 1, 2, 3 . . . ,8,9 inside the data folder.
For example, file 0.png is in subfolder 5, file 1.png is in subfolder 1 and file 59999.png is in subfolder 8.

When removing the second for loop with int j, the image does show.

Thank you for your help.

1 Like

Maybe your last image is blank and it actually is being displayed. The way the draw() loop works is that it only displays to the screen once the loop has been completed. In this case maybe think of the screen as a collage. Basically what you’re doing is layering one cutout image on top of the other and then then only displaying it after you’ve layered all the images so only the last one is visible. Does that make sense?

Could you explain what you’re trying to accomplish? That might make it easier to make suggestions.

1 Like

I’m trying to draw the image located at filedir on the canvas, wait for an input (haven’t done that part yet so I’m just going to use delay() ) and draw the next image.

I forgot to include the clearing of the canvas each iteration of the for loop but I removed the for loop and drew the image normally and it worked all right. I have a feeling that it has something to do with the for loop.

What the code above (should do) does is it searches folder 0 for 1.png. If it isn’t there it searches folder 1 and so on until it finds. Once it finds it it draws it and waits for an input. After the input it then searches for file 2.png and so on.

Hopefully that helps solve my problem.
Thank you.

if your above program would work it would try
framerate: 60 times per second
for loop : 60000 files ( if all exist )
that would be 3.600.000 times file load and draw per second.
you would need a much better computer…

your last info sounds more like:
show one ( of 60000 ) pictures
until keypressed select a other one. ( next or random? )

pls not use delay() inside draw…

1 Like

Does that mean it draws after the whole for loop is done?

it would do all the work ( file loading and draw )
but the output to screen would be only the last found picture…
yes, after draw is finished the screen is shown.

your program structure should start with
-a- a keyboard input detect what sets a nEXT variable
-b- a function ( outside of draw ) what
if nEXT is set search and load a new picture ( but not show )
-c- the draw should only do the show

image(img, 0, 0, 280, 280);

what can only work if in setup already called that picture search function once
and the first “img” is loaded already.

1 Like

Thanks for your help. if it’s only drawn on the canvas after draw() is finished I think I know a solution. Will use your advice too. What I plan to do is to

Run a function in draw. In that function run the for loop
After checking the file exists set variable a to i and variable b to j and Boolean c to true.
While c is true stop the function

If draw check if c is true
If true load image b/a.png
Wait for key input
Set c to false

You reckon this will work?

possibly only a wording thing, but NO
draw show the image NOT LOAD
as i say, one line in draw
( ok, if picture size is different the background would be ugly so could do a

void draw () {
  image(img, 0, 0);


image(img, 0, 0, 280, 280);

might damage the aspect ratio??

1 Like

Thank you. Yeah forgot it’s not load that’s already done. In fact I don’t need variable a and b I can load in the function.
Also I meant in* draw check if c is true. Sorry for the typo.

In terms of aspect ratio every single image is a 28x28 square image so it should be alright.

Thank you!
I’ll fix it once I get home