Re: Output_Files not Saved in Subfolder when the Subfolder contains Super-subfolders

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

Re: Output_Files not Saved in Subfolder when the Subfolder contains Super-subfolders

GP
Hie!

I was not able to find the solution to the problem I encountered probably it's some minor mistake:
The following steps have to be performed in the script:  

1. Go through the main Directory
2. Find all the Folders present (Ex: 'test1', 'test2', 'test3'.. etc)
3. In each Folder create a Subfolder (let's call it 'Recrop')
4. Get list of images in each Folder
5. Perform image functions on them and Save output images in respective 'Recrop' subfolders.
6. Go to all 'Recrop' subfolders and create two Super-subfolders 'SuperSub1' and 'SuperSub2'
7. Get list of images in 'Recrop'
8. Perform two types of image functions and save them respectively in 'SuperSub1' and 'SuperSub2'

The script works fine when executed till step 5
On executing step 6 'SuperSub1' and 'SuperSub2' are created inside 'Recrop' but the output images from step 5 are not saved in 'Recrop' anymore.

The error might be in the way I am calling the function but I can't figure out how to fix it.

I have attached my macro here:

function processDir(dir) {
listdir = getFileList(dir);
for (j = 0; j < listdir.length; j++) {
outDir = dir+listdir[j]+"Recrop";
File.makeDirectory(outDir);
outpath = outDir+"/";
inDir = dir+listdir[j];
processFolder(inDir); } }

function processFolder(inDir) {
list = getFileList(inDir);
for (i = 0; i < list.length; i++) {
        path = inDir+list[i]; //print (path);
       processFile(path, outpath); } }

setBatchMode(true);
function processFile(path, outpath){
          if (endsWith(path, ".tif")|| endsWith(path, ".TIF")) {
                open(path);
               
                Process here...

                Title = File.nameWithoutExtension();
                saveAs("TIFF", outpath+Title);            //doesnt save images to 'outpath'  
                close(); } }                                           // on executing code after this block


function processFolder(recrop){
        listrecrop = getFileList(recrop);
        for(k=0; k<listrecrop.length; k++) {
                path_re = recrop+listrecrop[k];

                if (endsWith(path_re, "/")) {
                   list_recrop = getFileList(recrop);
                      for(l=0; l<list_recrop.length; l++) {
                        inpath_re = path_re+list_recrop[l];
                       
                        outDir_sub1 = path_re+"/"+"SuperSub1";
                        File.makeDirectory(outDir_sub1);
                        outpath_sub1= outDir_sub1+"/";
                         
                        outDir_sub2 = path_re+"/"+"SuperSub2";
                        File.makeDirectory(outDir_sub2);
                        outpath_sub2 = outDir_sub2+"/";

                processFile(inpath_re, outpath_sub1, outpath_sub2); } } } }

setBatchMode(true);
function processFile(inpath_re, outpath_sub1, outpath_sub2) {
                if (endsWith(inpath_re, ".tif")|| endsWith(inpath_re, ".TIF")) {
                open(inpath_re);
                 
                Process here..
                saveAs("TIFF", outpath_sub1);
               
close(); }
               if (endsWith(inpath_re, ".tif")|| endsWith(inpath_re, ".TIF")) {
                open(inpath_re);
                 
                Process here..
                saveAs("TIFF", outpath_sub2);
               
close(); } }

Thank you so much,
Best Regards,
Reply | Threaded
Open this post in threaded view
|

Re: Output_Files not Saved in Subfolder when the Subfolder contains Super-subfolders

Krs5
Dear GP,

Your macro has no main body from which you call the functions. See for example https://imagej.nih.gov/ij/macros/Arrays.txt. The first 8 lines of code are the main body of the macro followed by some functions. These functions (reverseArray  and printArray) are called from within the main body. You have just a collection of functions.

Without any testing it might be possible to just change the first function to main body by changing "function processDir(dir)" to "macro process_Dir" and add "setBatchMode(true);" to this part of the code. But I did not test your code.

 Best wishes

Kees

Dr Ir K.R. Straatman
Senior Experimental Officer
Advanced Imaging Facility
Centre for Core Biotechnology Services
University of Leicester
http://www2.le.ac.uk/colleges/medbiopsych/facilities-and-services/cbs/lite/aif 

ImageJ workshops 1 and 2 June 2017: http://www2.le.ac.uk/colleges/medbiopsych/facilities-and-services/cbs/AIF/workshops/imagej-workshops-June-2017 


-----Original Message-----
From: GP [mailto:[hidden email]]
Sent: 30 March 2017 15:53
To: [hidden email]
Subject: Re: Output_Files not Saved in Subfolder when the Subfolder contains Super-subfolders

Hie!

I was not able to find the solution to the problem I encountered probably it's some minor mistake:
The following steps have to be performed in the script:  

1. Go through the main Directory
2. Find all the Folders present (Ex: 'test1', 'test2', 'test3'.. etc) 3. In each Folder create a Subfolder (let's call it 'Recrop') 4. Get list of images in each Folder 5. Perform image functions on them and Save output images in respective 'Recrop' subfolders.
6. Go to all 'Recrop' subfolders and create two Super-subfolders 'SuperSub1'
and 'SuperSub2'
7. Get list of images in 'Recrop'
8. Perform two types of image functions and save them respectively in 'SuperSub1' and 'SuperSub2'

The script works fine when executed till step 5 On executing step 6 'SuperSub1' and 'SuperSub2' are created inside 'Recrop'
but the output images from step 5 are not saved in 'Recrop' anymore.

The error might be in the way I am calling the function but I can't figure out how to fix it.

I have attached my macro here:

function processDir(dir) {
listdir = getFileList(dir);
for (j = 0; j < listdir.length; j++) {
outDir = dir+listdir[j]+"Recrop";
File.makeDirectory(outDir);
outpath = outDir+"/";
inDir = dir+listdir[j];
processFolder(inDir); } }

function processFolder(inDir) {
list = getFileList(inDir);
for (i = 0; i < list.length; i++) {
        path = inDir+list[i]; //print (path);
       processFile(path, outpath); } }

setBatchMode(true);
function processFile(path, outpath){
          if (endsWith(path, ".tif")|| endsWith(path, ".TIF")) {
                open(path);
               
                Process here...

                Title = File.nameWithoutExtension();
                saveAs("TIFF", outpath+Title);            //doesnt save
images to 'outpath'  
                close(); } }                                           // on
executing code after this block


function processFolder(recrop){
        listrecrop = getFileList(recrop);
        for(k=0; k<listrecrop.length; k++) {
                path_re = recrop+listrecrop[k];

                if (endsWith(path_re, "/")) {
                   list_recrop = getFileList(recrop);
                      for(l=0; l<list_recrop.length; l++) {
                        inpath_re = path_re+list_recrop[l];
                       
                        outDir_sub1 = path_re+"/"+"SuperSub1";
                        File.makeDirectory(outDir_sub1);
                        outpath_sub1= outDir_sub1+"/";
                         
                        outDir_sub2 = path_re+"/"+"SuperSub2";
                        File.makeDirectory(outDir_sub2);
                        outpath_sub2 = outDir_sub2+"/";

                processFile(inpath_re, outpath_sub1, outpath_sub2); } } } }

setBatchMode(true);
function processFile(inpath_re, outpath_sub1, outpath_sub2) {
                if (endsWith(inpath_re, ".tif")|| endsWith(inpath_re,
".TIF")) {
                open(inpath_re);
                 
                Process here..
                saveAs("TIFF", outpath_sub1);
               
close(); }
               if (endsWith(inpath_re, ".tif")|| endsWith(inpath_re,
".TIF")) {
                open(inpath_re);
                 
                Process here..
                saveAs("TIFF", outpath_sub2);
               
close(); } }

Thank you so much,
Best Regards,



--
View this message in context: http://imagej.1557.x6.nabble.com/Re-Output-Files-not-Saved-in-Subfolder-when-the-Subfolder-contains-Super-subfolders-tp5018423.html
Sent from the ImageJ mailing list archive at Nabble.com.

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

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
GP
Reply | Threaded
Open this post in threaded view
|

Re: Output_Files not Saved in Subfolder when the Subfolder contains Super-subfolders

GP
Hie!

Thank you so much I will try this way as well but I fixed it last night and the following code works now:

dir = getDirectory("Choose a Directory");

processDir(dir);
function processDir(dir) {
listdir = getFileList(dir);
for (j = 0; j < listdir.length; j++) { 
outDir = dir+listdir[j]+"Recrop";
File.makeDirectory(outDir);
outpath = outDir+"/";
inDir = dir+listdir[j]; 
processFolder(inDir); } }

function processFolder(inDir) {
list = getFileList(inDir);
for (i = 0; i < list.length; i++) {
    path = inDir+list[i]; 
processFile(path, outpath); } }

setBatchMode(true); 
function processFile(path, outpath){ 
    if (endsWith(path, ".tif")|| endsWith(path, ".TIF")) { 
    open(path); 
                
    Process here... 

    Title = File.nameWithoutExtension(); 
    saveAs("TIFF", outpath+'/'+Title);
close();
recrop = outpath+'/';
processRecrop(recrop); } } 

function processRecrop(recrop) {
   outDir_sub1 = recrop+"/"+"SuperSub1"; 
   File.makeDirectory(outDir_sub1);
   outpath_sub1 = outDir_sub1+"/"; 
   outDir_sub2 = recrop+"/"+"SuperSub2";
   File.makeDirectory(outDir_sub2);
   outpath_sub2 = outDir_sub2+"/";
   
   path_sub = recrop+'/';
   list_recrop = getFileList(recrop); {
   for(k=0; k<list_recrop.length; k++) {
       path_sub = recrop+list_recrop[k];  } }
   processFile_recrop(path_sub, outpath_sub1, outpath_sub2); }

setBatchMode(true);
function processFile_recrop(path_sub, outpath_sub1, outpath_sub2) {
    if (endsWith(path_sub, ".tif")|| endsWith(path_sub, ".TIF")) {
        open(path_sub);

        Process here..

        Title_sub1= File.nameWithoutExtension();
        saveAs("TIFF", outpath_sub1+Title_sub1);
close(); }
    if (endsWith(path_sub, ".tif")|| endsWith(path_sub, ".TIF")) {
        open(path_sub);
        
        Process here..

        Title_sub2= File.nameWithoutExtension();
        saveAs("TIFF", outpath_sub2+Title_sub2);        
close(); } }      

I wanted to ask if there might be a way to tell the pathname for the initial directory for headless execution from both Windows and Mac Command line. I already tried to write the pathname instead of "dir = getDirectory()" but it didn't work.

Thanks again,

Best Regards, 

On Fri, Mar 31, 2017 at 10:32 AM, Straatman, Kees (Dr.) [via ImageJ] <[hidden email]> wrote:
Dear GP,

Your macro has no main body from which you call the functions. See for example https://imagej.nih.gov/ij/macros/Arrays.txt. The first 8 lines of code are the main body of the macro followed by some functions. These functions (reverseArray  and printArray) are called from within the main body. You have just a collection of functions.

Without any testing it might be possible to just change the first function to main body by changing "function processDir(dir)" to "macro process_Dir" and add "setBatchMode(true);" to this part of the code. But I did not test your code.

 Best wishes

Kees

Dr Ir K.R. Straatman
Senior Experimental Officer
Advanced Imaging Facility
Centre for Core Biotechnology Services
University of Leicester
http://www2.le.ac.uk/colleges/medbiopsych/facilities-and-services/cbs/lite/aif 

ImageJ workshops 1 and 2 June 2017: http://www2.le.ac.uk/colleges/medbiopsych/facilities-and-services/cbs/AIF/workshops/imagej-workshops-June-2017 


-----Original Message-----
From: GP [mailto:[hidden email]]
Sent: 30 March 2017 15:53
To: [hidden email]
Subject: Re: Output_Files not Saved in Subfolder when the Subfolder contains Super-subfolders

Hie!

I was not able to find the solution to the problem I encountered probably it's some minor mistake:
The following steps have to be performed in the script:  

1. Go through the main Directory
2. Find all the Folders present (Ex: 'test1', 'test2', 'test3'.. etc) 3. In each Folder create a Subfolder (let's call it 'Recrop') 4. Get list of images in each Folder 5. Perform image functions on them and Save output images in respective 'Recrop' subfolders.
6. Go to all 'Recrop' subfolders and create two Super-subfolders 'SuperSub1'
and 'SuperSub2'
7. Get list of images in 'Recrop'
8. Perform two types of image functions and save them respectively in 'SuperSub1' and 'SuperSub2'

The script works fine when executed till step 5 On executing step 6 'SuperSub1' and 'SuperSub2' are created inside 'Recrop'
but the output images from step 5 are not saved in 'Recrop' anymore.

The error might be in the way I am calling the function but I can't figure out how to fix it.

I have attached my macro here:

function processDir(dir) {
listdir = getFileList(dir);
for (j = 0; j < listdir.length; j++) {
outDir = dir+listdir[j]+"Recrop";
File.makeDirectory(outDir);
outpath = outDir+"/";
inDir = dir+listdir[j];
processFolder(inDir); } }

function processFolder(inDir) {
list = getFileList(inDir);
for (i = 0; i < list.length; i++) {
        path = inDir+list[i]; //print (path);
       processFile(path, outpath); } }

setBatchMode(true);
function processFile(path, outpath){
          if (endsWith(path, ".tif")|| endsWith(path, ".TIF")) {
                open(path);
               
                Process here...

                Title = File.nameWithoutExtension();
                saveAs("TIFF", outpath+Title);            //doesnt save
images to 'outpath'  
                close(); } }                                           // on
executing code after this block


function processFolder(recrop){
        listrecrop = getFileList(recrop);
        for(k=0; k<listrecrop.length; k++) {
                path_re = recrop+listrecrop[k];

                if (endsWith(path_re, "/")) {
                   list_recrop = getFileList(recrop);
                      for(l=0; l<list_recrop.length; l++) {
                        inpath_re = path_re+list_recrop[l];
                       
                        outDir_sub1 = path_re+"/"+"SuperSub1";
                        File.makeDirectory(outDir_sub1);
                        outpath_sub1= outDir_sub1+"/";
                         
                        outDir_sub2 = path_re+"/"+"SuperSub2";
                        File.makeDirectory(outDir_sub2);
                        outpath_sub2 = outDir_sub2+"/";

                processFile(inpath_re, outpath_sub1, outpath_sub2); } } } }

setBatchMode(true);
function processFile(inpath_re, outpath_sub1, outpath_sub2) {
                if (endsWith(inpath_re, ".tif")|| endsWith(inpath_re,
".TIF")) {
                open(inpath_re);
                 
                Process here..
                saveAs("TIFF", outpath_sub1);
               
close(); }
               if (endsWith(inpath_re, ".tif")|| endsWith(inpath_re,
".TIF")) {
                open(inpath_re);
                 
                Process here..
                saveAs("TIFF", outpath_sub2);
               
close(); } }

Thank you so much,
Best Regards,



--
View this message in context: http://imagej.1557.x6.nabble.com/Re-Output-Files-not-Saved-in-Subfolder-when-the-Subfolder-contains-Super-subfolders-tp5018423.html
Sent from the ImageJ mailing list archive at Nabble.com.

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

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



To unsubscribe from Re: Output_Files not Saved in Subfolder when the Subfolder contains Super-subfolders, click here.
NAML



--
Gunjan Pandey

GP
Reply | Threaded
Open this post in threaded view
|

Re: Output_Files not Saved in Subfolder when the Subfolder contains Super-subfolders

GP
In reply to this post by GP
Hie!

From Windows command line only ''\\" in the pathname works.
Ex: dir = "D:\\path\\to\\folder\\for\\saving\\the\\world\\"

Best Regards,
Reply | Threaded
Open this post in threaded view
|

Re: Output_Files not Saved in Subfolder when the Subfolder contains Super-subfolders

Glen MacDonald-2
GP,
Avoid the need to track the OS specific file separator character  and the need escape it by adding this variable:
sep=File.separator;
https://imagej.nih.gov/ij/developer/macro/functions.html#S

Glen MacDonald
Digital Microscopy Center
Box 357923
University of Washington
Seattle, WA 98195-7923  USA
(206) 616-4156
[hidden email]








> On Mar 31, 2017, at 4:31 AM, GP <[hidden email]> wrote:
>
> Hie!
>
> From Windows command line only ''\\" in the pathname works.
> Ex: dir = "D:\\path\\to\\folder\\for\\saving\\the\\world\\"
>
> Best Regards,
>
>
>
> --
> View this message in context: http://imagej.1557.x6.nabble.com/Re-Output-Files-not-Saved-in-Subfolder-when-the-Subfolder-contains-Super-subfolders-tp5018423p5018426.html
> Sent from the ImageJ mailing list archive at Nabble.com.
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

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