Macro to split Channels

I’m trying to create a BatchProcessFolder macro to open a two channel stacked image and then split the channels and save it as an individual stack (C1, C2, etc) in the output stack.

Here is what I have so far. The macro appears to run (no errors) but the output folder does not have any images saved there. Any help would be much appreciated!

/*
 * Macro template to process multiple images in a folder
 */

#@ File (label = "Input directory", style = "directory") input
#@ File (label = "Output directory", style = "directory") output
#@ String (label = "File suffix", value = ".tif") suffix

// See also Process_Folder.py for a version of this code
 // in the Python scripting language.

processFolder(input);

// function to scan folders/subfolders/files to find files with correct suffix
 function processFolder(input) {
list = getFileList(input);
list = Array.sort(list);
for (i = 0; i < list.length; i++) {
	if(File.isDirectory(input + File.separator + list[i]))
		processFolder(input + File.separator + list[i]);
	if(endsWith(list[i], suffix))
		processFile(input, output, list[i]);
 }
}

function processFile(input, output, file) {
// Do the processing here by adding your own code.
// Leave the print statements until things work, then remove them.

open(file);
run("Split Channels");
title = getTitle();
saveAs(output+file); 
close();
title = getTitle();
saveAs(output+file); 
close();
print(title);
print("Processing: " + input + File.separator + file);
print("Saving to: " + output);

}

Hi Chris,

you can try to add a File.separator when saving the output : saveAs(output+File.separator+ outputFileName);
[ as you have in the line if(File.isDirectory(input + File.separator + list[i]))

[[ in the code below I simply add a File.separator to the variables “input” and “output” at the begining ]]

When you open you need to specify input + file .
Finally when you save , both outputs were named using “file”, so the second would overwrite the first one.

The code below works for me :smiley:

//@ File (label = "Input directory", style = "directory") input
//@ File (label = "Output directory", style = "directory") output
//@ String (label = "File suffix", value = ".tif") suffix

// See also Process_Folder.py for a version of this code
// in the Python scripting language.

print (input);
print (output) ;

input += File.separator;
output += File.separator;

print (input);
print (output) ;


processFolder(input);

// function to scan folders/subfolders/files to find files with correct suffix
function processFolder(input) {
list = getFileList(input);
list = Array.sort(list);
Array.print(list);
//waitForUser("");

for (i = 0; i < list.length; i++) {
	if(File.isDirectory(input + File.separator + list[i]))
		processFolder(input + File.separator + list[i]);
	if(endsWith(list[i], suffix))
		processFile(input, output, list[i]);
 }
}

function processFile(input, output, file) {
// Do the processing here by adding your own code.
// Leave the print statements until things work, then remove them.

open(input + file);
run("Split Channels");
title = getTitle();
saveAs("Tiff", output + title); 
close();
title = getTitle();
saveAs("Tiff", output + title); 
close();
print(title);
print("Processing: " + input + File.separator + file);
print("Saving to: " + output);
}


Cheers,

Romain

2 Likes

The answer of @romainGuiet is good. I would just add that you can address the images that appear as as result of Split Channels as they always have a predictable name with respect to the original. The only thing you need is a base name of the original file, then add to this, " (red)", " (green)" and " (blue)"

For example, a base name of “My Image.tif” will always split channels into “My Image.tif (red)”, “My Image.tif (green)” and “My Image.tif (blue)”

2 Likes

indeed you will get (red) (green) and (blue) with RGB image or C1-… , C2-… and Ci with multi channels composite images :yum:

R

3 Likes

Quite right! Good catch :smiley: Been working too much with RGB recently!

1 Like

Thanks all! I’m away from my image workstation but I’ll try this as soon as I get back. [Edit: works perfectly!]

I was previously doing this with a Batch->marco but having to hardcode the directories so this will be so much better!

And yes dnmason, one of the reasons that I use the getTitle() is that it predictable saves it as C1, C2, etc. and I want to save it as such.
Is there a more elegant way or doing this, i.e. I didn’t know how to “call” the windows and save it with a predictable filename?

Hi,

try
saveAs(output+ File.separator+ file);

Nico