开发者

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 开发者_如何学Can omission.

Any hints? Thanks!


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
     //

   }
 }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜