Help with using the "process folder" template to do a batch split channel

Trying to familiarize myself with the macros and use the macro template to process multiple images (“Process folder”) to run “split channels” on a folder of multiple files.

After running the “split channels” I want to select and close the red and blue channels and keep the green channel open, but I am not sure how to specify this (see in lines 30 and 32 below)

/*
 * 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.
	open(input + File.separator + file);
	run("Split Channels");
	selectWindow(input + File.separator + file + (blue)); //I am not sure how to specify the blue channel
	close();	
	selectWindow(input + File.separator + file + (red)); //I am not sure how to specify the red channel
	close();
	
	// Leave the print statements until things work, then remove them.
	print("Processing: " + input + File.separator + file);
	print("Saving to: " + output);
}
1 Like

Hey Joushua @jsamsoondar ,

Just try ... + " (blue)" as you are adding a string to those variables.

If you’re new to ImageJ macro, variables, strings and stuff, you may find my lecture series helpful:

Best,
Robert

Thanks for the reply, and I have seen many of your lecture videos already, they are very well done. That Episode 03b has been particularly helpful!

Sorry no luck with that, still getting the message “No window with the title “C:\Users…tif” found.”

Is this what you had in mind (see below)?
I also tried putting the “+ (blue)” inside the brackets but that didn’t work either

/*
 * 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.
	open(input + File.separator + file);
	run("Split Channels");
	selectWindow(input + File.separator + file) + " (blue)"; //I am not sure how to specify the blue channel
	close();	
	selectWindow(input + File.separator + file) + " (red)"; //I am not sure how to specify the red channel
	close();
	
	// Leave the print statements until things work, then remove them.
	print("Processing: " + input + File.separator + file);
	print("Saving to: " + output);
}

Alright, instead of select_window(... use print(.... This will print out the name of the window ImageJ is trying to select. Compare it to the available windows and try to understand what’s missing and what’s too much in your code :wink:

Hint: also run print(input);

Wow that is hugely helpful, so I basically was able to troubleshoot by running print(input) found that the correct way to write it is:

	selectWindow(file + " (blue)")

the “File.separator + file” bits needed to be left out

so the working product is as follows:

`/*
 * 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.
	open(input + File.separator + file);

	
	
	run("Split Channels");
	
	selectWindow(file + " (blue)"); //I am not sure how to specify the blue channel
	close();	
	selectWindow(file + " (red)"); //I am not sure how to specify the red channel
	close();
	
	// Leave the print statements until things work, then remove them.
	print("Processing: " + input + File.separator + file);
	print("Saving to: " + output);
}
`

so in summary, (if anyone wants to use this or has a similar question) running this code will take all the images within a file of your choosing and split the color channels, close the red and blue ones, and leave the green one open (which can be edited to change which colors you close or leave open).

Thanks for your help! I certainly could not have figured that out on my own.

1 Like