File.open() argument issues

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

File.open() argument issues

atloo1
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: File.open() argument issues

Rasband, Wayne (NIH/NIMH) [E]
> On Jul 14, 2015, at 5:27 PM, atloo1 <[hidden email]> wrote:
>
> Background- I need to iterate over files in a folder:
> x\y\folder\files here
>
> I navigate to one file in this location to yield
> x\y\folder\a file
> path = File.openDialog("Welcome!");
> dir = File.getParent(path);
> list = getFileList(dir);
>
> I can create the proper string and print it:
> print(dir+"\\" + list[#]);   // looks like 'x\y\folder\a file'
>
> However, this somehow isn't a string:
> a = dir+"\\" + list[#];
> print(a)   //prints '0'
>
> and it can't be passed to File.open():
> File.open(dir+"\\" + list[#]);   //file exists and suffix is not '.txt'...
>
> The file in question is a JPEG that I can open by hand.
> When doing so and using the macro recorder all '\' are replaced with '\\'in
> the filename.

With ImageJ 1.49u and later, you can use the Help>Examples>Macro>Process Folder command to open a macro that iterates over the images in a folder. The following is what it looks like, modified it to work with JPEGs instead of TIFFs.

-wayne
 
  extension = “.jpg";
  dir1 = getDirectory("Choose Source Directory ");
  //dir2 = getDirectory("Choose Destination Directory ");
  setBatchMode(true);
  n = 0;
  processFolder(dir1);

  function processFolder(dir1) {
     list = getFileList(dir1);
     for (i=0; i<list.length; i++) {
          if (endsWith(list[i], "/"))
              processFolder(dir1+list[i]);
          else if (endsWith(list[i], extension))
             processImage(dir1, list[i]);
      }
  }

  function processImage(dir1, name) {
     open(dir1+name);
     print(n++, name);
     // add code here to analyze or process the image
     //saveAs(extension, dir2+name);
     close();
  }


 


--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html