Skip to content

Instantly share code, notes, and snippets.

@romainGuiet
Last active February 6, 2024 16:08
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);
@Kenny19920122
Copy link

Thank you soooooo much!!!! You saved me.
One small thing though, I couldn't do the total automatic batch for the images (some errors occur i guess), so I can only press run and have to click 'OK' for each file I opened to split the channels. But it still saved a lot of my time anyway.
Super thankful!

@romainGuiet
Copy link
Author

Hi @Kenny19920122,

Thank you soooooo much!!!! You saved me.

I'm happy to read that !

can only press run and have to click 'OK' for each file I opened to split the channels

This is strange... what kind of file format are your using? tif ?
Maybe you can open a topic on https://forum.image.sc with screenshot of errors or of the log .

Cheers,

@mtcm2021
Copy link

Hi, I tried using this to separate a batch of RGB tifs and the output folder just had the original RGB tifs. I'm not sure what I did wrong. Any suggestions?

@romainGuiet
Copy link
Author

romainGuiet commented Nov 30, 2021

Hi @mtcm2021 ,
In line 25 we were checking if the image has more than 1 channel

if ((channels > 1)

BUT RGB images are considered a 1 channel image with the function "getDimensions()"

You can test with the code below:

run("Clown");
getDimensions(width, height, channels, slices, frames);
print(channels);

So I modify the script to add a RGB condition line 25 is now :

if ((channels > 1)||( bitDepth() == 24 )) run("Split Channels");

Please let me know how it works !
Cheers
r

@mayishazn
Copy link

Thanks so much! This is great! And also a good introduction to macros on imagej

@Garcia-Hernandez-R
Copy link

Hi @romainGuiet

Thanks a lot for the macro! I wanted to ask you about the extension of the file once it is saved; when I do the split channels manually I receive the images in .tif but when I use the macro I receive them in .tiff. I have tried to change this in the macro but it keeps returning .tiff. Do you know how I could change this? for further image processing I need it to be in .tif format. Thank you very much in advance!

@romainGuiet
Copy link
Author

Hi @Garcia-Hernandez-R ,
I guess in line 32 saveAs("tiff", output_dir+currentImage_name); you can modify to saveAs("tif", output_dir+currentImage_name);

@Garcia-Hernandez-R
Copy link

Hi @Garcia-Hernandez-R , I guess in line 32 saveAs("tiff", output_dir+currentImage_name); you can modify to saveAs("tif", output_dir+currentImage_name);

Hi, thanks a lot for answering. I have tried, but it is still returning as .tiff. I will try again just know and let you know. Thanks again

@Garcia-Hernandez-R
Copy link

@romainGuiet

Hi @Garcia-Hernandez-R , I guess in line 32 saveAs("tiff", output_dir+currentImage_name); you can modify to saveAs("tif", output_dir+currentImage_name);

Hi, thanks a lot for answering. I have tried, but it is still returning as .tiff. I will try again just know and let you know. Thanks again

Hi, not working, and I don't really understand why. Here a screenshot https://drive.google.com/file/d/1nzMGXDY75KA9tAvdR0VhZTdr_3pQAkpP/view?usp=sharing

@PatrickJS44
Copy link

Your macro works like a dream! I've been trying various scripts and tools for batch spliting image channels, thank you so much!
Is it possible to modify the "save as" phase so that I can customize the renaming rule for each channel? Like adding prefix or suffix for splitted Blue channel output image.
You can leave it if such function could cause complex extra works. I got a batch renaming tool for this after all. Thanks again for your helpful scripts!

@romainGuiet
Copy link
Author

Hi @Garcia-Hernandez-R ,

I'm sorry but I didn't get the latest notification and thought it was solved... what is the file format of the input image ? Tiff ?

@romainGuiet

Hi @Garcia-Hernandez-R , I guess in line 32 saveAs("tiff", output_dir+currentImage_name); you can modify to saveAs("tif", output_dir+currentImage_name);

Hi, thanks a lot for answering. I have tried, but it is still returning as .tiff. I will try again just know and let you know. Thanks again

Hi, not working, and I don't really understand why. Here a screenshot https://drive.google.com/file/d/1nzMGXDY75KA9tAvdR0VhZTdr_3pQAkpP/view?usp=sharing

@romainGuiet
Copy link
Author

Hi @PatrickJS44 ,

Is it possible to modify the "save as" phase so that I can customize the renaming rule for each channel? Like adding prefix or suffix for splitted Blue channel output image.

you could modify the loop like this :

// let's assume you have 3 channels
prefix_array = { "ch1_prefix" ,  "ch2_prefix" ,  "ch3_prefix" }
for ( c = 1 ; c <= ch_nbr ; c++){
	selectImage(c);
	currentImage_name = getTitle();
	saveAs("tiff", output_dir+prefix_array [c-1]+currentImage_name); // -1 because array start at 0
}

Cheers,

R

@castrojenna
Copy link

Hi all, I am fairly new to imageJ and writing code in general, and I used this macro to split my channels. however, it converted all of my RGB images to 8-bit grayscale. I'd like the images to remain in color, is there an addition to this macro that will do this? Thanks.

@romainGuiet
Copy link
Author

Hi @castrojenna ,

it converted all of my RGB images to 8-bit grayscale
Great that the expected results 😄 !

If you want to keep the Red, Green, Blue Lookup Tables , which make grey images look red, green or blue you could use the lines below

// let's assume the images are RGB, we define an arrray with the colors
colors = newArray("Red","Green","Blue");
for ( c = 1 ; c <= ch_nbr ; c++){
	selectImage(c);
	currentImage_name = getTitle();
	run(colors[c-1]);
	//run("RGB Color"); // uncomment to convert to RGB so the images has this color when opened in another software (Photoshop...)
	saveAs("tiff", output_dir+currentImage_name);
}

@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?

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