batch process macro

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

batch process macro

Knecht, David
I am trying to modify the batch process macro for my needs but I want it to saves the files it processes with their original names but in a different folder that I specify (or simply a new folder in the same folder if that is easier) rather than overwriting the files.  Can someone help me add that function to this macro?  Thanks, Dave

// "BatchProcessFolders"
//
// This macro batch processes all the files in a folder and any
// subfolders in that folder. In this example, it runs the Subtract
// Background command of TIFF files. For other kinds of processing,
// edit the processFile() function at the end of this macro.

macro "batch process folder scale and make movie"{
 requires("1.33s");
  dir = getDirectory("Choose a Directory ");
  setBatchMode(true);
  count = 0;
  countFiles(dir);
  n = 0;
  processFiles(dir);
  //print(count+" files processed");

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

  function processFiles(dir) {
     list = getFileList(dir);
     for (i=0; i<list.length; i++) {
         if (endsWith(list[i], "/"))
             processFiles(""+dir+list[i]);
         else {
            showProgress(n++, count);
            path = dir+list[i];
            processFile(path);
         }
     }
 }

 function processFile(path) {
      if (endsWith(path, ".tif")) {
          open(path);

run("8-bit");
run("Properties...", " unit=pixel pixel_width=.64 pixel_height=.64 voxel_depth=1.0000 frame=[5 min] origin=0,0");

run("Scale...", "x=.7 y=.7 z=.7 width=974 height=728  interpolation=Bilinear average process create");
run("Label...", "format=00:00 starting=0 interval=4 x=850 y=20 font=18 text=hr:min ");

}
          save(path);
          close();
     }
 }}

Visiting Professor David Knecht
Beatson Institute for Cancer Research
University of Glasgow
Switchback Road, Bearsden
Glasgow Scotland G61 1BD
UK





Visiting Professor David Knecht
Beatson Institute for Cancer Research
University of Glasgow
Switchback Road, Bearsden
Glasgow Scotland G61 1BD
UK
Reply | Threaded
Open this post in threaded view
|

Re: batch process macro

Rob van 't Hof
Hi,
I use something along these lines:

dir=getDirectory("Choose a Directory");
processedDir=dir + "\Processed\\";
File.makeDirectory(processedDir);

//loop through your file list and after you open each file
imgName=getTitle();

//do your processing
//then save in new directory
saveAs("Tiff", processedDir+ imgName);

Does that help?
bye,
Rob






On 14/02/2012 16:21, David Knecht wrote:

> I am trying to modify the batch process macro for my needs but I want it to saves the files it processes with their original names but in a different folder that I specify (or simply a new folder in the same folder if that is easier) rather than overwriting the files.  Can someone help me add that function to this macro?  Thanks, Dave
>
> // "BatchProcessFolders"
> //
> // This macro batch processes all the files in a folder and any
> // subfolders in that folder. In this example, it runs the Subtract
> // Background command of TIFF files. For other kinds of processing,
> // edit the processFile() function at the end of this macro.
>
> macro "batch process folder scale and make movie"{
>   requires("1.33s");
>    dir = getDirectory("Choose a Directory ");
>    setBatchMode(true);
>    count = 0;
>    countFiles(dir);
>    n = 0;
>    processFiles(dir);
>    //print(count+" files processed");
>
>    function countFiles(dir) {
>       list = getFileList(dir);
>       for (i=0; i<list.length; i++) {
>           if (endsWith(list[i], "/"))
>               countFiles(""+dir+list[i]);
>           else
>               count++;
>       }
>   }
>
>    function processFiles(dir) {
>       list = getFileList(dir);
>       for (i=0; i<list.length; i++) {
>           if (endsWith(list[i], "/"))
>               processFiles(""+dir+list[i]);
>           else {
>              showProgress(n++, count);
>              path = dir+list[i];
>              processFile(path);
>           }
>       }
>   }
>
>   function processFile(path) {
>        if (endsWith(path, ".tif")) {
>            open(path);
>
> run("8-bit");
> run("Properties...", " unit=pixel pixel_width=.64 pixel_height=.64 voxel_depth=1.0000 frame=[5 min] origin=0,0");
>
> run("Scale...", "x=.7 y=.7 z=.7 width=974 height=728  interpolation=Bilinear average process create");
> run("Label...", "format=00:00 starting=0 interval=4 x=850 y=20 font=18 text=hr:min ");
>
> }
>            save(path);
>            close();
>       }
>   }}
>
> Visiting Professor David Knecht
> Beatson Institute for Cancer Research
> University of Glasgow
> Switchback Road, Bearsden
> Glasgow Scotland G61 1BD
> UK
>
>
>
>
>
> Visiting Professor David Knecht
> Beatson Institute for Cancer Research
> University of Glasgow
> Switchback Road, Bearsden
> Glasgow Scotland G61 1BD
> UK
>

--
_____________________________
Dr. Rob van 't Hof
Senior Lecturer

Centre for Molecular Medicine
MRC IGMM
University of Edinburgh
Western General Hospital
Crewe Road, Edinburgh EH4 2XU
United Kingdom


Phone: (+44)-131-6511031
email: [hidden email]
_____________________________



The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.
Reply | Threaded
Open this post in threaded view
|

Re: batch process macro

Krs5
In reply to this post by Knecht, David
Hi David,



One option is to let the user choose a directory. This directory should already exist.



dir2 = getDirectory("Choose Destination Directory ");



// Another option to save the data in a subdirectoy (in example called: analysed) of the original directory:

// dir2 = dir1+"analysed"+File.separator;

// File.makeDirectory(dir2);





Your code would change to (not tested!):



macro "batch process folder scale and make movie"{

 requires("1.33s");

  dir = getDirectory("Choose a Directory ");

  dir2 = getDirectory("Choose Destination Directory ");

  setBatchMode(true);

  count = 0;

  countFiles(dir);

  n = 0;

  processFiles(dir, dir2);

  //print(count+" files processed");



  function countFiles(dir) {

     list = getFileList(dir);

     for (i=0; i<list.length; i++) {

         if (endsWith(list[i], "/"))

             countFiles(""+dir+list[i]);

         else

             count++;

     }

 }



  function processFiles(dir, dir2) {

     list = getFileList(dir);

     for (i=0; i<list.length; i++) {

         if (endsWith(list[i], "/"))

             processFiles(""+dir+list[i], dir2);

         else {

            showProgress(n++, count);

            path = dir+list[i];

            processFile(path, dir2);

         }

     }

 }



 function processFile(path,dir2) {

      if (endsWith(path, ".tif")) {

          open(path);

          title1=getTitle();



run("8-bit");

run("Properties...", " unit=pixel pixel_width=.64 pixel_height=.64 voxel_depth=1.0000 frame=[5 min] origin=0,0");



run("Scale...", "x=.7 y=.7 z=.7 width=974 height=728  interpolation=Bilinear average process create");

run("Label...", "format=00:00 starting=0 interval=4 x=850 y=20 font=18 text=hr:min ");



}

          save(dir2+title1);

          close();

     }

 }}





Hope it works



Kees



-----Original Message-----
From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of David Knecht
Sent: 14 February 2012 16:22
To: [hidden email]
Subject: batch process macro



I am trying to modify the batch process macro for my needs but I want it to saves the files it processes with their original names but in a different folder that I specify (or simply a new folder in the same folder if that is easier) rather than overwriting the files.  Can someone help me add that function to this macro?  Thanks, Dave



// "BatchProcessFolders"

//

// This macro batch processes all the files in a folder and any

// subfolders in that folder. In this example, it runs the Subtract

// Background command of TIFF files. For other kinds of processing,

// edit the processFile() function at the end of this macro.



macro "batch process folder scale and make movie"{

 requires("1.33s");

  dir = getDirectory("Choose a Directory ");

  setBatchMode(true);

  count = 0;

  countFiles(dir);

  n = 0;

  processFiles(dir);

  //print(count+" files processed");



  function countFiles(dir) {

     list = getFileList(dir);

     for (i=0; i<list.length; i++) {

         if (endsWith(list[i], "/"))

             countFiles(""+dir+list[i]);

         else

             count++;

     }

 }



  function processFiles(dir) {

     list = getFileList(dir);

     for (i=0; i<list.length; i++) {

         if (endsWith(list[i], "/"))

             processFiles(""+dir+list[i]);

         else {

            showProgress(n++, count);

            path = dir+list[i];

            processFile(path);

         }

     }

 }



 function processFile(path) {

      if (endsWith(path, ".tif")) {

          open(path);



run("8-bit");

run("Properties...", " unit=pixel pixel_width=.64 pixel_height=.64 voxel_depth=1.0000 frame=[5 min] origin=0,0");



run("Scale...", "x=.7 y=.7 z=.7 width=974 height=728  interpolation=Bilinear average process create");

run("Label...", "format=00:00 starting=0 interval=4 x=850 y=20 font=18 text=hr:min ");



}

          save(path);

          close();

     }

 }}



Visiting Professor David Knecht

Beatson Institute for Cancer Research

University of Glasgow

Switchback Road, Bearsden

Glasgow Scotland G61 1BD

UK











Visiting Professor David Knecht

Beatson Institute for Cancer Research

University of Glasgow

Switchback Road, Bearsden

Glasgow Scotland G61 1BD

UK