Run(Merge Channels) for batch processing of images

I have three different folders with each contains same number of images. I have already sorted those images to make sure them in order. Then I try to batch merge images from three folders, e.g. the 1st images from each folder will merge.

I have not loop this macro yet because I don’t understand why the merge function doesn’t work.

macro "merge channels"{
    file1= getDirectory("Choose a Directory");
    list1= getFileList(file1); 
    file2= getDirectory("Choose a Directory");
    list2= getFileList(file2); 
    file3= getDirectory("Choose a Directory");
    list3= getFileList(file3); 

    run("Merge Channels...", "c1=list3[1] c2=list2[1] c4=list1[1] keep");



This macro can open three files as I want.
The file1,file2,file3 are the paths, the list1,list2,list3 are the filenames. I printed them which shows clearly.
So the only problem is the :
run("Merge Channels...", "c1=list3[1] c2=list2[1] c4=list1[1] keep");

“list[3]” is not a valid choice for “C1(red):”

You’re handing over a literal “list3[1]” as an argument, instead of the content of list3[1]. Use string concatenation to build your arguments string:

run("Merge Channels...", "c1=" + list3[1] + " c2=" + list2[1] + " c4=" + list1[1] + " keep");

Please also see the documentation on how to use the run() macro command.

macro "batch_merge_channels"{
    count = 1;
    file1= getDirectory("Choose a Directory");
    list1= getFileList(file1); 
    file2= getDirectory("Choose a Directory");
    list2= getFileList(file2); 
    file3= getDirectory("Choose a Directory");
    small = n1;
    small = n2;

      name = "RGB 00"+count;
      run("Merge Channels...", "c2="+list2[1]+ " c4="+list1[1]+ " keep");
      saveAs("tiff", file3+name);
   // setBatchMode(true);

Thanks for your help, I write this code and it works.

1 Like


I’m a complete novice with writing macros/coding but I’m trying to learn.
I’m confused here as when I use this code and select my folders it says “10x” (part of my file name) is not a valid choice for C2 Green.

My Files names are all the same format “10x 1-1-1 _DAPI” (or _GFP or _TxRed) the 1-1-1 is the unique identifier of the sample. This code should not be dependent on any of that I thought. I told the program to use IJ1 macro. And I have the three seperate folders (in my case labeled DAPI, TxRed, and GFP).

I did not change any of the code.

Can anyone help?
Other code would work to. I’m just trying to merge 100 images (300 single channel images).

The macro option string is built of key=value pairs separated by spaces (see the documentation). If your file names (i.e. the ‘values’ of the parameters c1, c2, etc.) contain spaces, you have to enclose them in square brackets [] like this:

run("Merge Channels...", "c2=["+list2[1]+ "] c4=["+list1[1]+ "] keep");

so at runtime, after resolving the list variables, this will give something like:

run("Merge Channels...", "c2=[10x 1-1-1 _DAPI.tif] c4=[10x 1-1-1 _GFP.tif] keep");

Does that make sense?


Hello, Jimmy! (and anybody who wants to merge 2 or more channels)

I never used Java (I believe it’s java code) but the logics is quite similar to python, so I changed your code a little:

macro "batch_merge_channels"{
    file1 = getDirectory("DAPI");
    //you can change the name of the inquiry to whatever wavelength you need, e.g. CY5
    list1 = getFileList(file1);
    //gets the list of files in the folder DAPI
    n1 = lengthOf(list1);
    //gets the number of files in folder DAPI, it should be the same as the number 
    //of files in folder GFP
    file2 = getDirectory("GFP");
    list2 = getFileList(file2); 
    file3 = getDirectory("Merge");
    //the output folder. When started first the number of files is 0
    list3 = getFileList(file3);
    n2 = lengthOf(list3);
    small = n1;
    //condition for for-loop

    for(i = n2 + 1; i < small; i++) {
      //i will always follow the aborted number of merges, you might not have the problem, 
      //but with small memory and a huge set of images it is useful
      name = list2[i];
      //not to lose your track, though you can change it to anything else
      open(file1 + list1[i]);
      open(file2 + list2[i]);
      run("Merge Channels...", "c2=[" + list2[i] + "] c3=[" + list1[i] + "] keep");
      saveAs("tiff", file3 + name);

My laptop isn’t the strongest one, + I was processing huge sets of images, so would run out of memory every 300 images. To keep the process running after abort, n2 is used to increment the ‘i’.

Hope, it will be useful for people.

P.S. If you need 3 wavelengths or more just add

fileN = getDirectory("WL");
listN = getFileList(fileN); 

to the code.

To avoid running out of memory, it suffices to close all images after having saved them in the for loop :wink:

1 Like

That’s right :slight_smile: I just got the idea how I ran out of memory, cheers!
Should I change it like this?

saveAs("tiff", file3 + name);

Thanks for sharing…

Thanks, very helpful!!

I am using the above code to merge images in two different folders with the same amount of files; however, I don’t seem to get any output from this script. Using the macro recorder, I am getting run(“Merge Channels…”, “c2=[” + list2[i] + “] c3=[” + list1[i] + “] create”); instead of run(“Merge Channels…”, “c2=[” + list2[i] + “] c3=[” + list1[i] + “] keep”); I am trying to debug the code by checking my variables; I tried removing the semicolon to see what the variable value is for the directory and file names but I am not able to see the result of that. Does anyone have any insight on this? I simply just want to merge two channels into one merged file.


A post was split to a new topic: Need help debugging a channel-merging macro