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 › How to Read a Folder-Full of Images
Page Index Toggle Pages: 1
How to Read a Folder-Full of Images? (Read 2291 times)
How to Read a Folder-Full of Images?
Sep 15th, 2009, 11:40pm
 
I'd like to write a Processing program that looks at a folder, finds image files in that folder, and runs various operations on those files as a group.

The Processing reference, to my surprise, doesn't show anything like a folder.listfiles() or similar method -- how would I get a list of the image files in a directory?

thanks!
Re: How to Read a Folder-Full of Images?
Reply #1 - Sep 15th, 2009, 11:51pm
 
there are standard java methods on the File class that'll do this

http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html

File[] listFiles()
Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname.

or

String[] list()
Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname.

both can also use a name filter so you can select only PNGs say.
Re: How to Read a Folder-Full of Images?
Reply #2 - Oct 2nd, 2009, 5:22am
 
can someone help on this, I can't get it to work.

I tried serveral things.

Code:
File dir = new File("data");
String[] list = dir.list();
if (list == null) {
 println("Folder does not exist or cannot be accessed.");
} else {
 println(list);
}


Code:
File dir = new File("sketchbook/default/fman2/data");
   
String[] children = dir.list();
if (children == null) {
// Either dir does not exist or is not a directory
} else {
for (int i=0; i<children.length; i++) {
// Get filename of file or directory
String filename = children[i];
println(children[i]);
}
}


I would like to be able to read the path that i choose, this code works but all things i tried to report the folder names in it failed.

Code:
String folderPath = selectFolder("choose a folder containing images");  // Opens file chooser


void setup(){
 size(100, 100);
 noLoop();

 if (folderPath == null) { // If a folder was not selected
   println("No folder was selected...");
 }
 else { // If a folder was selected, print path to folder
   println(folderPath);
 }

}


void draw(){

}

Re: How to Read a Folder-Full of Images?
Reply #3 - Oct 2nd, 2009, 7:44am
 
Quote:
File dir = new File("data");
[...]
File dir = new File("sketchbook/default/fman2/data");

You use relative paths. In Processing, these paths will be relative... to your tmp/temp dir! Use absolute paths, or dataPath("") to get the path of the data directory.
Re: How to Read a Folder-Full of Images?
Reply #4 - Oct 2nd, 2009, 9:38am
 
"sketchbook/default/fman2/data"

I dont have a folder called sketchbook, or default etc.
fman2 is what the sketch was called in the example right?

I ended up with this now and still same problem, "Folder does not...".
In the data folder are tree maps called 1, 2, and 3

Code:
File dir = new File("sketchbook/default/readFolder/data");
String[] list = dir.list();
if (list == null) {
println("Folder does not exist or cannot be accessed.");
}
else {
println(list);
}
Re: How to Read a Folder-Full of Images?
Reply #5 - Oct 2nd, 2009, 9:52am
 
clankill3r wrote on Oct 2nd, 2009, 9:38am:
I dont have a folder called sketchbook, or default etc.
fman2 is what the sketch was called in the example right

You should know, you provided the examples...
Have you read my answer
Re: How to Read a Folder-Full of Images?
Reply #6 - Oct 2nd, 2009, 10:23am
 
I've read your answer serveral times but i have trouble understanding.

with tmp/temp dir, you mean the macintosch/windows temp directory?
Like
C:\WINDOWS\Temp?

Or the temp for unsaved files like:
C:\Documents and Settings\Doeke\Local Settings\Temp\untitled3116670275362554839.tmp\sketch_oct02b

Also how do i use a dataPath, it doesn't show a "show in reference".
this cause it's java instand of processing?
Re: How to Read a Folder-Full of Images?
Reply #7 - Oct 2nd, 2009, 11:38am
 
i havent read the whole thread but i guess the main missundertsanding is, that when running your processing sketch, it is actually not running from your folder where you save it. It is temporarily created in your windows temp folder. Probably C:\WINDOWS\Temp\XXXX
So thats where your sketch is looking for your images because you didnt use an absolut path.

So here : File dir = new File("sketchbook/default/fman2/data");
enter the whole path to your images, starting with C:\Documents and Settings\Doeke\sketchbook/default/readFolder/data...." and it should work.

Re: How to Read a Folder-Full of Images?
Reply #8 - Oct 2nd, 2009, 11:40am
 
Oh and be careful that you use shlashs : "/" not  backslashes "\" like windows does in its paths. so it must be C:/ not C:\ etc.
Re: How to Read a Folder-Full of Images?
Reply #9 - Oct 3rd, 2009, 3:48am
 
yeah the absolute path works,
thx PhilHo and Credic!

i'm on a pc atm but do you guys have any idea where the temp dir is on a mac?

also for the relative path for some reason i can't get that to work.
I have a map called data in my C:/WINDOWS/TEMP
in the map data is another map.
if i use File dir = new File("data");
then it reports
Folder does not exist or cannot be accessed.

if i use "/" then it reports all files on C:/
if i use "/WINDOWS" then it reports the files in there so doesn't it always start at the beginning of the local drive? Or do i have to use other semicolons or something for a relative path?

Code:
File dir = new File("/");
String[] list = dir.list();

if (list == null) {
println("Folder does not exist or cannot be accessed.");
}
else {
println(list);
}



Quote:
[0] "$AVG8.VAULT$"
[1] "ATI"
[2] "Autodesk"
[3] "AUTOEXEC.BAT"
[4] "boot.ini"
[5] "CONFIG.SYS"
[6] "data"
[7] "df2cfd0d650df4800c6199458211fdc0"
[8] "Documents and Settings"
[9] "HammerAutosave"
[10] "Intel"
[11] "IO.SYS"
[12] "MSDOS.SYS"
[13] "MSOCache"
[14] "NTDETECT.COM"
[15] "ntldr"
[16] "OkiDriver"
[17] "pagefile.sys"
[18] "Program Files"
[19] "RaidTool"
[20] "RECYCLER"
[21] "Softimage"
[22] "spm"
[23] "sqmdata00.sqm"
[24] "sqmdata01.sqm"
[25] "sqmdata02.sqm"
[26] "sqmdata03.sqm"
[27] "sqmdata04.sqm"
[28] "sqmdata05.sqm"
[29] "sqmdata06.sqm"
[30] "sqmdata07.sqm"
[31] "sqmdata08.sqm"
[32] "sqmdata09.sqm"
[33] "sqmdata10.sqm"
[34] "sqmdata11.sqm"
[35] "sqmdata12.sqm"
[36] "sqmdata13.sqm"
[37] "sqmdata14.sqm"
[38] "sqmdata15.sqm"
[39] "sqmdata16.sqm"
[40] "sqmdata17.sqm"
[41] "sqmdata18.sqm"
[42] "sqmdata19.sqm"
[43] "sqmnoopt00.sqm"
[44] "sqmnoopt01.sqm"
[45] "sqmnoopt02.sqm"
[46] "sqmnoopt03.sqm"
[47] "sqmnoopt04.sqm"
[48] "sqmnoopt05.sqm"
[49] "sqmnoopt06.sqm"
[50] "sqmnoopt07.sqm"
[51] "sqmnoopt08.sqm"
[52] "sqmnoopt09.sqm"
[53] "sqmnoopt10.sqm"
[54] "sqmnoopt11.sqm"
[55] "sqmnoopt12.sqm"
[56] "sqmnoopt13.sqm"
[57] "sqmnoopt14.sqm"
[58] "sqmnoopt15.sqm"
[59] "sqmnoopt16.sqm"
[60] "sqmnoopt17.sqm"
[61] "sqmnoopt18.sqm"
[62] "sqmnoopt19.sqm"
[63] "System Volume Information"
[64] "temp.xsi"
[65] "users"
[66] "WINDOWS"
Re: How to Read a Folder-Full of Images?
Reply #10 - Oct 3rd, 2009, 7:01am
 
PhiLho  wrote on Oct 2nd, 2009, 7:44am:
Use [...] dataPath("") to get the path of the data directory.
means replace File dir = new File("data"); with
File dir = new File(dataPath(""));
dataPath isn't in the Extended Reference, but in PApplet reference.
I didn't specified where the tmp dir is, because it is system dependent (even among flavors of Windows), user dependent, etc. I don't advise to use that path, just explained why your examples didn't work. Smiley
Re: How to Read a Folder-Full of Images?
Reply #11 - Oct 4th, 2009, 4:50am
 
ok thx Smiley
Page Index Toggle Pages: 1