We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Working with files, loading folders of images
Page Index Toggle Pages: 1
Working with files, loading folders of images (Read 768 times)
Working with files, loading folders of images
Jul 8th, 2009, 5:51pm
 
Hi, I'm looking for the simplest solution for automatically loading a set of images from a directory.

I'm happy to put them into a PImage array, they don't need to be named or have any unique treatments. I've been playing with this (http://processing.org/learning/topics/directorylist.html) tutorial on looking at files and folders, but I don't know how to use loadImage() in conjunction with the file[] array that holds the names of the images.

Can anyone help?

Quote:
PImage[] pics; //i want to put all of the files in data/ in here

void setup() {

String path = sketchPath+"/data/";
File[] files = listFiles(path);

print(path+"\n");
print(files.length+"\n"); //how many files are here
println();

pics=new PImage[files.length];


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


// I really want to be able to:
pics[i]=loadImage(files[i]);
//but there is a type mismatch, as files is an array of File, and load image wants a string
//can I do a simple conversion?

}

}


//taken from http://processing.org/learning/topics/directorylist.html
File[] listFiles(String dir) {
 File file = new File(dir);
 if (file.isDirectory()) {
   File[] files = file.listFiles();
   return files;
 } else {
   // If it's not a directory
   return null;
 }
}

Re: Working with files, loading folders of images
Reply #1 - Jul 8th, 2009, 11:28pm
 
pics[i]=loadImage(files[i].getName());
or
pics[i]=loadImage(files[i].getAbsolutePath()); // Works outside of data folder

or change File[] to String[] and use files = file.list(); instead (but less flexible afterward).
Re: Working with files, loading folders of images
Reply #2 - Jul 9th, 2009, 2:38am
 
Thanks, I didn't realize it was as simple as calling a method on the files array.
Re: Working with files, loading folders of images
Reply #3 - Jul 9th, 2009, 3:34am
 
I forgot the "mandatory" link: File
There you can see what you can do with a File object.
And to be picky: the methods doesn't act on the file array but on each element of the array...  Cool
Re: Working with files, loading folders of images
Reply #4 - Jul 9th, 2009, 1:30pm
 
Haha, I appreciate the exactness.

Thanks for linking the reference, I dunno why I couldn't find it earlier.
Page Index Toggle Pages: 1