Skip to content

Instantly share code, notes, and snippets.

@romainGuiet
Last active April 26, 2024 00:18
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save romainGuiet/cf42f3b1d31222a76d602dfe2f028894 to your computer and use it in GitHub Desktop.
Save romainGuiet/cf42f3b1d31222a76d602dfe2f028894 to your computer and use it in GitHub Desktop.
An imagej macro example to process a folder of images, split the channels and save them in an output directory
// 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 , or a RGB
if ((channels > 1)||( bitDepth() == 24 )) 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);
@Huerhenjiujie
Copy link

Hi, thank you so much your macro, it is really helpful to.
I have tried to run it on the server with headless mode and it gave error below:
java.lang.VerifyError: Bad type on operand stack

I googled it, it is about the "this" statement missing, and I am really confuse about it.
Could you help me with that?

@RBosire1949
Copy link

Thank you for the code!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment