1
votes

everyone,

I have a folder with multiple sub-folders, and each of which contains 18 number of tif(.tif) files. I'd like to open every sub-folders files as a stack in imageJ.

With some online macro documentation, I can open files in each sub-folders, and do somethings like change 16-bit files into 8-bit files.

But as my analysis require, I need open each sub-floder as a stacks, I have some problem to get stack with macro,

Here is the code that I have currently,

inputDir = getDirectory("choose the input directory"); 
outputDir = getDirectory("choose the output directory"); 

processDir(inputDir, outputDir); 

function processDir(inputDir, outputDir) { 
    listdir = getFileList(inputDir); 
    for (j = 0; j < listdir.length; j++) { 
        print("Processing: " + listdir[j]); 
        File.makeDirectory(outputDir + listdir[j]); 
        outputFolder = outputDir + listdir[j]; 
        inputFolder = inputDir + listdir[j]; 
        setBatchMode(true); 
        processFolder(inputFolder); 
        setBatchMode(false); 
    } 
} 

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

function processFile(inputFolder, outputFolder, file) { 
    print("Processing: " + inputFolder + file); 
    open(inputFolder + file); 
    run("Image Sequence...", "open=[inputFolder + file] number=18 starting=1 increment=1 scale=100 file=[] or=[] sort");
    run("Enhance Contrast", "saturated=0.35"); 
    run("Apply LUT", "stack"); 
    setAutoThreshold("Otsu dark"); 
    setThreshold(60, 255); 
    run("Convert to Mask", " "); 

    print("Saving to: " + outputFolder); 

    saveAs("tiff", outputFolder+file); 
    close(); 
}

If you could tell me how to open sub-folder as a stack.

Thank you in advance.

3
Hi. I wondered if you have come up with a solution to this problem because I have the same ...? Thanks! - tjebo
Hi, I changed to: Image J --- drag to open a folder contain many sub-folders ( you will have many stacks open after this step) --- open macro, add the code without "inputDir",but other code you want to deal with you stack, don't forget add "close()" in the edn of macro --- then click one stack, click "run" --- other stacks as the same, click the stack click run. I use this method finished my work, a little time-couse, hope could help you. - Duerna
thanks. Have found a way that worked for my requirements. Will put it as an answer, although I am not sure if it really helps for your task. - tjebo

3 Answers

0
votes

I made a macros here that iterates over folders and imports image sequences and saves as image sequences. You can detail what you want it to do in the for loop in between the open and save commands. Hopefully this helps!

inputDir = getDirectory('Choose Input Directory'); 
outputDir = getDirectory('Choose Output Directory'); 

listdir = getFileList(inputDir);

for(i=0; i< listdir.length; i++) {
    print("Processing: " + inputDir + listdir[i]);
    File.makeDirectory(outputDir +listdir[i]);
    inputFolder = inputDir + listdir[i];
    outputFolder = outputDir + listdir[i];
    inputFiles = getFileList(inputFolder);
    inputFile = inputFolder + inputFiles[0];
    run("Image Sequence...", "open=[inputFile] convert_to_rgb sort");
    run("Image Sequence... ", "format=TIFF save=[outputFolder]");
    close();
}
0
votes

I am not exactly proficient in other languages than R, so this answer contains a lot of "snippetting" from sites. It has been a few days since then and I haven't copied the sources. Many apologies.

The following macro loops through each subfolder of a directory, creates a stack and then does "something" with the stack, in my case make a gif out of it and save this under the name of the subfolder.

inputDir = getDirectory('Choose Input Directory'); 
outputDir = getDirectory('Choose Output Directory'); 

listFiles(inputDir); 

list = getFileList(inputDir);

function listFiles(dir) { 
        list = getFileList(dir); 
        for (i=0; i<list.length; i++) { 

                setBatchMode(true);
for (i=0; i<list.length; i++) {
    file = dir + list[i];
    open(file);
    title = getTitle();
saveAs("Gif", dir+title);
close();
    }
setBatchMode(false); 

        } 
} 

I think the main difference to @Duerna 's code is the nested for-loop. Hope this helps.

edit So, after like two years (!) I had to use this again and I was a bit dumbfounded by my own answer. Here some more help, especially to my future self:

Add the macro via Plugins -> New -> Macro

It is important that your source directory only folders that contains images! The gifs are saved in the input directory, I guess one could safely remove the output directory line of code, or change the function variable for saving.

0
votes
inputDir1 = getDirectory("Choose the folder "); 
inputDir2 = getDirectory("Choose where to save the image Stack ");
run("Image Sequence...", "open="+inputDir1+" sort");
filename= getTitle();
saveAs("Tiff", inputDir2+filename+".tif");
close()