Recursive adding of a composite slice to a stack and saving as montage

Hi all,

I’m new here and I kindly ask for your help with a macro.

I have a folder with 200+ images. These are all 3 images stack. I need to add a fourth slice to each stack with the composite of these 3 channels, make a montage and then save it to a folder.

I managed to get the script to work but when i wrap it in the code to process several images at the same time it crashes. Any idea what I’m doing wrong? Thank you!

function action(input, output, filename) { //commands to be run all wrapped into the action function
    open(input + filename);
    run("Make Composite");
    run("RGB Color");
    selectImage(1);
    Stack.setDisplayMode("color");
    run("RGB Color");
    selectImage(1);
    close();
    run("Concatenate...", "all_open title=[Concatenated Stacks]");
    run("Make Montage...", "columns=4 rows=1 scale=0.5 first=1 last=4 increment=1 border=1 font=12");
    close();  
}

input = getDirectory("Input directory"); //input folder, make sure there are no subdirectories
output = getDirectory("Output directory"); //output folder

setBatchMode(true); //function to read all files in directory
list = getFileList(input);
for (i = 0; i < list.length; i++)
        action(input, output, list[i]); //calls out function action wrapped above
setBatchMode(false);
close();

I fixed it. Needed a command to close all the open windows. Here it goes the corrected one in case it is of any help for anyone:

function action(input, output, filename) { //commands to be run all wrapped into the action function
    open(input + filename);
    run("Make Composite");
    run("RGB Color");
    selectImage(1);
    Stack.setDisplayMode("color");
    run("RGB Color");
    selectImage(1);
    close();
    run("Concatenate...", "all_open title=[Concatenated Stacks]");
    run("Make Montage...", "columns=4 rows=1 scale=0.5 first=1 last=4 increment=1 border=1 font=12");
    saveAs("Tiff", output + filename);
    while (nImages>0) { 
        selectImage(nImages); 
        close(); 
    } 
}

input = getDirectory("Input directory"); //input folder, make sure there are no subdirectories
output = getDirectory("Output directory"); //output folder

setBatchMode(true); //function to read all files in directory
list = getFileList(input);
for (i = 0; i < list.length; i++)
        action(input, output, list[i]); //calls out function action wrapped above
setBatchMode(false);`
2 Likes

You can also use:

run("Close All");
1 Like

Ah thats neat! Thank you imagejan