Recusive operation on subfolders

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

Recusive operation on subfolders

Ludwig_Curie

Hello every one
 
I am sorry if my questions seems to be a bit silly, but I was not able to find any good solution for my problem.
 
What I whant to do is a simple image operation on several files with different names in different subfolders.
I am able to do it automatically but on only one folder, and I have to rename each file to fit the name in my macro which is not convenient at all.
 
This is the code I am using now :
 
dir1= getDirectory("Choose Destination Directory");
open(dir1+"rouge.tif");
open(dir1+"bleu.tif");
 
selectWindow("rouge.tif");
run("Multiply...")
saveAs("Tiff", dir1+"GFrouge.tif");
 
imageCalculator("Add create 32-bit", "bleu.tif","GFrouge.tif");
//run("Image Calculator...", "image1=bleu.tif operation=Add image2=rouge.tif create 32-bit");
saveAs("Tiff",dir1+"add.tif");
 
The structure of my directory and file is the following:
Folder A containing subfolders B,C,D,… Each sub folder contain only two images named XXX-NDD2.tif and XXX-NDD3.tif ( where XXX is different from one subfolder to the other)
 
What I want to do is the following : Select only the Folder A and allow imageJ going first to subdirectory B, read the name of the files and make the operation multiply XXX-NDD3.tif, save as GFXXX.tif in subfolder B then image calculator of adding GFXXX and  XXX-NDD2.tif  and save as : add-XXXX.tif still in subfolder B. Then do the same thing recursively for the other subdirectory.
 
Thank you for your help.
 
Ludwig
 
Reply | Threaded
Open this post in threaded view
|

Re: Recusive operation on subfolders

Michael Schmid
Hi Ludwig,

you can take Search.txt in the ImageJ sources macros as an example
of how to address a directory recursively:

  extensions = newArray(".tif", ".tiff");  //or what extensions you
want to address
 
  function find(dir) {
      list = getFileList(dir);
      for (i=0; i<list.length; i++) {
          showProgress(i, list.length);
          if (endsWith(list[i], "/"))
              find(""+dir+list[i]);
          else if (contents && valid(list[i])) {
              //whatever you want to do with the file list[i]
          }
      }
  }

  function valid(name) {  //check extension
      for (i=0; i<extensions.length; i++) {
         if (endsWith(name, extensions[i]))
             return true;
      }
      return false;
  }

Hope this helps,

Michael
________________________________________________________________
On Tue, 18 Dec 2007 13:43:57 +0100 Ludwig Berland
<[hidden email]> wrote:

>Hello every one
>
>I am sorry if my questions seems to be a bit silly, but I was not able to find
>any good solution for my problem.
>
>What I whant to do is a simple image operation on several files with different
>names in different subfolders.
>I am able to do it automatically but on only one folder, and I have to rename
>each file to fit the name in my macro which is not convenient at all.
>
>This is the code I am using now :
>
>dir1= getDirectory("Choose Destination Directory");
>open(dir1+"rouge.tif");
>open(dir1+"bleu.tif");
>
>selectWindow("rouge.tif");
>run("Multiply...")
>saveAs("Tiff", dir1+"GFrouge.tif");
>
>imageCalculator("Add create 32-bit", "bleu.tif","GFrouge.tif");
>//run("Image Calculator...", "image1=bleu.tif operation=Add image2=rouge.tif
>create 32-bit");
>saveAs("Tiff",dir1+"add.tif");
>
>The structure of my directory and file is the following:
>Folder A containing subfolders B,C,D,… Each sub folder contain only two images
>named XXX-NDD2.tif and XXX-NDD3.tif ( where XXX is different from one
>subfolder to the other)
>
>What I want to do is the following : Select only the Folder A and allow imageJ
>going first to subdirectory B, read the name of the files and make the
>operation multiply XXX-NDD3.tif, save as GFXXX.tif in subfolder B then image
>calculator of adding GFXXX and  XXX-NDD2.tif  and save as : add-XXXX.tif still
>in subfolder B. Then do the same thing recursively for the other subdirectory.
>
>Thank you for your help.
>
>Ludwig
>
Reply | Threaded
Open this post in threaded view
|

Re: Recusive operation on subfolders

Ludwig_Curie

Thank you Michael for your help, and sorry for the late answering,
I tried several things based on what you told me. But my problem is not completely solved.
I used this code :

{dir = getDirectory("Choose a Directory ");

find(dir);

  function find(dir) {
      list = getFileList(dir);
      for (i=0; i<list.length; i++) {
 showProgress(i, list.length);          
 if (endsWith(list[i], "/"))
              find(""+dir+list[i]);
          else if (endsWith(list[i], ".tif"))
{
for (i=0; i<list.length; i++) {

     open(""+dir+list[i]);
}
        }
// I simplified your code because imagej gave me the error of an undefined variable in the function valid(name) and it could work without.


My concern is that now I am able to open all the file of sub directory, but I don't know how to make image J understood that he as to make operation on two image that are in the same subfolder. And save the result in that subfolder
Basically it has to add the image with i=0 to the image i=1 and save the new to the folder where those image are. And then do the same for image 2 and 3, 4+5 etc to the end of the list.
I tried without success different things. Do you have any idea ?

Thank you again for your help..

Ludwig



Ludwig Berland
2007-12-20



En réponse au message
de : Michael Schmid
du : 2007-12-18 21:00:56
À : [hidden email]
CC :
Sujet : Re: Recusive operation on subfolders

Hi Ludwig,

you can take Search.txt in the ImageJ sources macros as an example
of how to address a directory recursively:

  extensions = newArray(".tif", ".tiff");  //or what extensions you
want to address

  function find(dir) {
      list = getFileList(dir);
      for (i=0; i <list.length; i++) {
          showProgress(i, list.length);
          if (endsWith(list[i], "/"))
              find(""+dir+list[i]);
          else if (contents && valid(list[i])) {
              //whatever you want to do with the file list[i]
          }
      }
  }

  function valid(name) {  //check extension
      for (i=0; i <extensions.length; i++) {
         if (endsWith(name, extensions[i]))
             return true;
      }
      return false;
  }

Hope this helps,

Michael
________________________________________________________________
On Tue, 18 Dec 2007 13:43:57 +0100 Ludwig Berland
<[hidden email] > wrote:

>Hello every one
>
>I am sorry if my questions seems to be a bit silly, but I was not able to find
>any good solution for my problem.
>
>What I whant to do is a simple image operation on several files with different
>names in different subfolders.
>I am able to do it automatically but on only one folder, and I have to rename
>each file to fit the name in my macro which is not convenient at all.
>
>This is the code I am using now :
>
>dir1= getDirectory("Choose Destination Directory");
>open(dir1+"rouge.tif");
>open(dir1+"bleu.tif");
>
>selectWindow("rouge.tif");
>run("Multiply...")
>saveAs("Tiff", dir1+"GFrouge.tif");
>
>imageCalculator("Add create 32-bit", "bleu.tif","GFrouge.tif");
>//run("Image Calculator...", "image1=bleu.tif operation=Add image2=rouge.tif
>create 32-bit");
>saveAs("Tiff",dir1+"add.tif");
>
>The structure of my directory and file is the following:
>Folder A containing subfolders B,C,D,… Each sub folder contain only two images
>named XXX-NDD2.tif and XXX-NDD3.tif ( where XXX is different from one
>subfolder to the other)
>
>What I want to do is the following : Select only the Folder A and allow imageJ
>going first to subdirectory B, read the name of the files and make the
>operation multiply XXX-NDD3.tif, save as GFXXX.tif in subfolder B then image
>calculator of adding GFXXX and  XXX-NDD2.tif  and save as : add-XXXX.tif still
>in subfolder B. Then do the same thing recursively for the other subdirectory.
>
>Thank you for your help.
>
>Ludwig
>



--
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.503 / Virus Database: 269.17.4/1188 - Release Date: 17/12/2007 14:13
Reply | Threaded
Open this post in threaded view
|

Re: Recusive operation on subfolders

Michael Schmid
Hi Ludwig,

you could try something like the following:

//how even and odd files end:
var even = newArray("0.tif", "2.tif", "4.tif", "6.tif", "8.tif" );
var odd = newArray("1.tif", "3.tif", "5.tif", "7.tif", "9.tif" );

//body of the macro
   {
     dir = getDirectory("Choose a Directory ");
     find(dir);
   }

//here the work is done
   function find(dir) {
     list = getFileList(dir);
     for (i=0; i<list.length; i++) {
       showProgress(i, list.length);
       if (endsWith(list[i], "/"))
               find(""+dir+list[i]);
       else {
         whichEven = -1;
         for (j=0; j<lengthOf(even); j++)
           if (endsWith(list[i], even[j])) whichEven = j;
         if (whichEven >=0) {
           file0 = dir+list[i];
           temp = substring(list[i], 0, lengthOf(list[i])-lengthOf
(even[whichEven]));
           file1 = dir + temp + odd[whichEven];
           print("File0: "+file0);  //the even file
           print("File1: "+file1);  //the odd file
         }

       }
    }


Michael
________________________________________________________________

On 20 Dec 2007, at 18:18, Ludwig Berland wrote:

>
> Thank you Michael for your help, and sorry for the late answering,
> I tried several things based on what you told me. But my problem is  
> not completely solved.
> I used this code :
>
> {dir = getDirectory("Choose a Directory ");
>
> find(dir);
>
>   function find(dir) {
>       list = getFileList(dir);
>       for (i=0; i<list.length; i++) {
>  showProgress(i, list.length);
>  if (endsWith(list[i], "/"))
>               find(""+dir+list[i]);
>           else if (endsWith(list[i], ".tif"))
> {
> for (i=0; i<list.length; i++) {
>
>      open(""+dir+list[i]);
> }
>         }
> // I simplified your code because imagej gave me the error of an  
> undefined variable in the function valid(name) and it could work  
> without.
>
>
> My concern is that now I am able to open all the file of sub  
> directory, but I don't know how to make image J understood that he  
> as to make operation on two image that are in the same subfolder.  
> And save the result in that subfolder
> Basically it has to add the image with i=0 to the image i=1 and  
> save the new to the folder where those image are. And then do the  
> same for image 2 and 3, 4+5 etc to the end of the list.
> I tried without success different things. Do you have any idea ?
>
> Thank you again for your help..
>
> Ludwig
>
>
>
> Ludwig Berland
> 2007-12-20
>
>
>
> En réponse au message
> de : Michael Schmid
> du : 2007-12-18 21:00:56
> À : [hidden email]
> CC :
> Sujet : Re: Recusive operation on subfolders
>
> Hi Ludwig,
>
> you can take Search.txt in the ImageJ sources macros as an example
> of how to address a directory recursively:
>
>   extensions = newArray(".tif", ".tiff");  //or what extensions you
> want to address
>
>   function find(dir) {
>       list = getFileList(dir);
>       for (i=0; i <list.length; i++) {
>           showProgress(i, list.length);
>           if (endsWith(list[i], "/"))
>               find(""+dir+list[i]);
>           else if (contents && valid(list[i])) {
>               //whatever you want to do with the file list[i]
>           }
>       }
>   }
>
>   function valid(name) {  //check extension
>       for (i=0; i <extensions.length; i++) {
>          if (endsWith(name, extensions[i]))
>              return true;
>       }
>       return false;
>   }
>
> Hope this helps,
>
> Michael
> ________________________________________________________________
> On Tue, 18 Dec 2007 13:43:57 +0100 Ludwig Berland
> <[hidden email] > wrote:
>> Hello every one
>>
>> I am sorry if my questions seems to be a bit silly, but I was not  
>> able to find
>> any good solution for my problem.
>>
>> What I whant to do is a simple image operation on several files  
>> with different
>> names in different subfolders.
>> I am able to do it automatically but on only one folder, and I  
>> have to rename
>> each file to fit the name in my macro which is not convenient at all.
>>
>> This is the code I am using now :
>>
>> dir1= getDirectory("Choose Destination Directory");
>> open(dir1+"rouge.tif");
>> open(dir1+"bleu.tif");
>>
>> selectWindow("rouge.tif");
>> run("Multiply...")
>> saveAs("Tiff", dir1+"GFrouge.tif");
>>
>> imageCalculator("Add create 32-bit", "bleu.tif","GFrouge.tif");
>> //run("Image Calculator...", "image1=bleu.tif operation=Add  
>> image2=rouge.tif
>> create 32-bit");
>> saveAs("Tiff",dir1+"add.tif");
>>
>> The structure of my directory and file is the following:
>> Folder A containing subfolders B,C,D,… Each sub folder contain  
>> only two images
>> named XXX-NDD2.tif and XXX-NDD3.tif ( where XXX is different from one
>> subfolder to the other)
>>
>> What I want to do is the following : Select only the Folder A and  
>> allow imageJ
>> going first to subdirectory B, read the name of the files and make  
>> the
>> operation multiply XXX-NDD3.tif, save as GFXXX.tif in subfolder B  
>> then image
>> calculator of adding GFXXX and  XXX-NDD2.tif  and save as : add-
>> XXXX.tif still
>> in subfolder B. Then do the same thing recursively for the other  
>> subdirectory.
>>
>> Thank you for your help.
>>
>> Ludwig
>>
>
>
>
> --
> No virus found in this incoming message.
> Checked by AVG Free Edition.
> Version: 7.5.503 / Virus Database: 269.17.4/1188 - Release Date:  
> 17/12/2007 14:13