Batch Run (make Composite) of immages

Hello,
I acquired images of the different samples in 3 different channels and i would like to make a composite foe each single sample.
I am still new to scripting, but i still managed to work my way until this point. To be honest, i took the easy path and put each channel in a different folder. In this way i have 3 folders (1 for each channel) with the same number of files and each file is the same sample.
Example: run Composite of file1 in folder1 + file1 in folder2 + file3 in folder3

The problem is that i have an error on line 26 and i think i overlook something simple yet invisible for a newbie like me.

macro "batch_merge_channels"{
    count = 1;
    setBatchMode(true);
    file1= getDirectory("Choose a Directory");
    list1= getFileList(file1); 
    n1=lengthOf(list1);
    file2= getDirectory("Choose a Directory");
    list2= getFileList(file2); 
    n2=lengthOf(list2);
    file3= getDirectory("Choose a Directory");
    list3= getFileList(file3); 
    n3=lengthOf(list3);
    file4= getDirectory("Choose a Directory");
    list4= getFileList(file4); 
    n4=lengthOf(list4);
    open(file1+list1[1]);
    open(file2+list2[1]);
    open(file3+list3[1]);
    small = n1;
    if(small<n2)
    small = n2;

    for(i=0;i<small;i++)
    {
      name = "zCompo 00"+count;
      run("Images to Stack");
      run("Make Composite");
      run("Channels Tool...");
      Stack.setPosition(1,1,1);
      run("Grays");
      Stack.setPosition(3,1,1);
      run("Red");
      saveAs("tiff", file4+name);
      count++;
    }
   // setBatchMode(true);
}

Hi @Valerio_Laghi!

Congrats for making your way into scripting! It takes some time, but the effort is totally worthwhile. Be patient, it’ll pay off.

Let’s take a look at your code:
I assume that the problematic line is

If that’s the case, I have a hunch for the cause of the error: you’re just opening the first image of each folder (you left the open(...) statements outside the loop, also they have a fixed index) AND you’re not closing the composites you create (after saveAs(...) there should be a close(); statement).
That way, in the second round through the loop, you’ll have a just a composite image, and you’ll face an error like this: imagen

Anyways, it would be useful if you could also post the error message that you get, or a screenshot, to be sure.

There’s also a couple of loose ends, but nothing too serious:

  1. These are unused:
  1. It looks like you’re trying to get the count of the folder with the smallest number of files (in case it’s uneven), but you’re actually keeping the largest number. Also, you’re leavin out the third folder.

The way to go here should be:

small = minOf(n1, minOf(n2, n3));
  1. There’s a better way for this:

3a) to get a padded number (like 0034), use:

      name = "zCompo " + IJ.pad(count, 4);

3b) there’s no need for the count variable, it is just i + 1
so:

     name = "zCompo " + IJ.pad(i + 1, 4);

and you can get rid of:

Finally, you’re missing setting the LUT for channel 2.
See if you can implement these changes, and let me know how it went.

Cheers,
Nico

1 Like

@NicoDF Thank you a lot for your help!

This is the final script and now work well.

macro "batch_merge_channels"{
    setBatchMode(true);
    file1= getDirectory("Choose a Directory FOR Transmitted Light");
    list1= getFileList(file1); 
    n1=lengthOf(list1);
    file2= getDirectory("Choose a Directory FOR GFP");
    list2= getFileList(file2); 
    n2=lengthOf(list2);
    file3= getDirectory("Choose a Directory FOR mCherry");
    list3= getFileList(file3); 
    n3=lengthOf(list3);
    file4= getDirectory("Choose a Directory AS Output");
    list4= getFileList(file4); 
    n4=lengthOf(list4);
    small = minOf(n1, minOf(n2, n3));
    if(small<n2)
    small = n2;

    for(i=0;i<small;i++)
    {
      name = "zCompo " + IJ.pad(i + 1, 4);
      open(file1+list1[i]);
      open(file2+list2[i]);
      run("Despeckle");
      open(file3+list3[i]);
      run("Despeckle");
      run("Images to Stack");
      run("Make Composite", "display=Composite");
      run("Channels Tool...");
      Stack.setPosition(1,1,1);
      run("Grays");
      Stack.setPosition(2,1,1);
      run("Green");
      Stack.setPosition(3,1,1);
      run("Red");
      saveAs("tiff", file4+name);
      close();
    }
   // setBatchMode(true);
}
1 Like

Great! I see that you also customized the dialog titles, and added a little pre-processing. :+1:

Note that you should still remove these lines: