Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Processing, how can I iterate across the files/images in my sketch's data directory?

I want to load a bunch of images (or files but there doesn't seem to be an object/type for that) -- not by name, but just whichever ones are in the data directory, into an array or something. Is there some method for doing that? The docs make it look impossible and searching the Processing forums doesn't turn anything up. But it's kind of hard to believe that it's an omission.

Any hints? Thanks!

like image 394
clweeks Avatar asked Dec 02 '25 18:12

clweeks


1 Answers

One of the neat things about Processing is that if the language is missing something, you can just use Java instead. This is an old question, so maybe you've already figured this out, but there actually is an example of using Java to list files doing this on the Processing website. Also, here's an example modified from one on the old Processing forums. It does some file extension checks that might be helpful:

import java.io.File;

 File dir = new File("folder-with-images");

 File[] files = dir.listFiles();

 for( int i=0; i < files.length; i++ ){ 
   String path = files[i].getAbsolutePath();

   // check the file type and work with jpg/png files
   if( path.toLowerCase().endsWith(".jpg") || path.toLowerCase().endsWith(".png") ) {

     PImage image = loadImage( path );

     //
     // do stuff with your images
     //

   }
 }
}
like image 101
muffinista Avatar answered Dec 05 '25 09:12

muffinista



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!