Macro in batch processing to split channel and save single channel image

Hi friends,

I’m trying to split my three-channel raw images and save single channel image for each raw image I have. I have many of those raw images so I want to do it in batch. I’m very new to script and macros so I tried to write the two command lines I want the software to do for me, but it only saves the first split channel and it ask me to confirm saving for each image. Can someone offer me some suggestions how I should revise my code? I greatly appreciate any input.

Hi @454Casull,

The Batch Process plugin take care of the saving in the output folder
BUT
it can save only 1 output image, with the name of the input image. (so you should make sure to select 2 different folders otherwise it will overright your images)

You can find on this page (and below) a small script that will do the job.

// ask user to select a folder
dir = getDirectory("Select A folder");
// get the list of files (& folders) in it
fileList = getFileList(dir);
// prepare a folder to output the images
output_dir = dir + File.separator + " output" + File.separator ;
File.makeDirectory(output_dir);


//activate batch mode
setBatchMode(true);

// LOOP to process the list of files
for (i = 0; i < lengthOf(fileList); i++) {
	// define the "path" 
	// by concatenation of dir and the i element of the array fileList
	current_imagePath = dir+fileList[i];
	// check that the currentFile is not a directory
	if (!File.isDirectory(current_imagePath)){
		// open the image and split
		open(current_imagePath);
		// get some info about the image
		getDimensions(width, height, channels, slices, frames);
		// if it's a multi channel image
		if (channels > 1) run("Split Channels");

		// now we save all the generated images as tif in the output_dir
		ch_nbr = nImages ; 
		for ( c = 1 ; c <= ch_nbr ; c++){
			selectImage(c);
			currentImage_name = getTitle();
			saveAs("tiff", output_dir+currentImage_name);
		}
		// make sure to close every images befores opening the next one
		run("Close All");
	}
}
setBatchMode(false);

Cheers,

Romain

@romainGuiet

Wow that code does the magic work! The explanation is clear too! Thanks so much for sharing it :smiley:!