ImageJ Merge macro

Hello there.

I m trying to use a macro for merging 2 images. They have names like 156 F Ctr A1c1.jpg (and c2.jpg for chan2)
How can i tell ImageJ to automatically choose the image containing c1 in the name for channel 1 and c2 for channel 2?
run(“Merge Channels…”, “c1=??? c3=??? create”);

Here I suppose my 2 images are already opened in ImageJ.

Next step, it would be great if I could add functions to automatically open images in my folder 2 by 2 and then follow with the merge but It’s maybe too much to ask…

Thank you for your help!

Hi @Steevo,

if you want to open images from a folder and merge them, I’d recommend programming a for-loop which opens the images, keeps the names of the images in a variable and then merges them.
Something like:

filelist = getFileList(directory) 
for (i = 0; i < lengthOf(filelist); i += 2) {
    open(directory + File.separator + filelist[i]);
    image1 = getTitle();
    open(directory + File.separator + filelist[i + 1]);
    image2 = getTitle();

    run("Merge Channels...", "c1=" + image1 + " c2=" + image2 + " create keep");
    saveAs("tif", filelist[i] + "_merged.tif");

    run("Close All");
}

Furthermore, you may want to go through this tutorial (starting at slide 29) to learn how to process images in a folder a bit more in detail:

Let us know if you have any further question!

Cheers,
Robert

Thank you very much @haesleinhuepf for your very fast reply. I have already add to your code a directory variable which is missing I guess. My error now seems to be due to the backslash just front of my image name when trying to merge. it says “\936F” is not a valid choice for “c1”
When I copied the path of my directory, I did change all the " \ " with " / "
Do you have an easy way to fix this?

Im still reading your tutorial to find out a way to
Thank you

1 Like

Could you please share the code which is throwing the error message? :upside_down_face:

ofc. Here it is:

directory="C:/Users/SteevO-LENOVO/Desktop/test macro/Single/"
filelist = getFileList(directory) 
for (i = 0; i < lengthOf(filelist); i += 2) {
    open(directory + File.separator + filelist[i]);
    image1 = getTitle();
    open(directory + File.separator + filelist[i + 1]);
    image2 = getTitle();

    run("Merge Channels...", "c1=" + image1 + " c2=" + image2 + " create keep");
    saveAs("jpg", filelist[i] + "_merged.jpg");

    
}
DEBUG:
Memory	*	53MB of 12155MB (<1%)
nImages()	*	2
getTitle()	*	"\936F RCIU 4x A2c2.jpg"
directory	*	"C:/Users/SteevO-LENOVO/Desktop/test macro/Single/"
filelist	*	array[4]
i	*	0
image1	*	"\936F RCIU 4x A2c1.jpg"
image2	*	"\936F RCIU 4x A2c2.jpg"

---		---
Error:		"\936F" is not a valid choice for "c1" in line 9:
		run ( "Merge Channels..." , "c1=" + image1 + " c2=" + image2 + " create keep" <)> ;
1 Like

Thanks for also sharing the debug! This line is suspicious:

getTitle()	*	"\936F RCIU 4x A2c2.jpg"

Is it possible that your files contain a \ at the beginning of the name? Or a special character?

Ah wait. ImageJ macro doesn’t like spaces in parameters and file names.

Please encapsulate the image name in rectangular brackets:

run("Merge Channels...", "c1=[" + image1 + "] c2=[" + image2 + "] create keep");

Ok This works! I check that and come back in a few

1 Like

Ok so now, my only problem seems to be a saving one. I dont see my merged files and I have no code bug

@haesleinhuepf
Maybe a clue. When i dont run the close function of the script, I saw 2 things.

  1. It creates 2 merged files for each images couple. 2 times each
  2. The title of my image in ImageJ became \336 F RCIU… instead of just 336 F RCIU (w/o backslash)

Hm, this backslash in the filename (not in the path, in the filename) is confusing. Can you make a little screenshot of that folder, so that I can see the filenames?

You need to get rid of it. You could for example rename the images after opening:

open(...);
rename("first_image");
image1=getTitle(); // image1 will be "first_image" then...

Capture2

By the way, I solved the doubling issue. It was my fault sorry…

I need a screenshot of the FILEname, not the path… :wink:

Oh sorry is you wanted the filename and not the pathway… But the filenames are really regular starting with the number

1 Like

I expect something like this:
image

https://www.ic.unicamp.br/~stolfi/EXPORT/www/ISO-8859-1-Encoding.html

Good Idea. Maybe but in fact I gave the number 336 randomly in my last post^^. In my situation now it is 936 and 995. But this will be a problem I need to solve anyway because the filenames can start with any number with my different analysis

I think I found it! Please remove the File.separator, because it’s the \ in Windows.

Ok now the name is correct but still no merged file saved in my folder :confused:

I got it!
saveAs(“jpg”, directory+ filelist[i] + “_merged.jpg”);

One more thing, now my merged name is MY_IMAGE_NAMEc1.jpg_merged
Is there a way to delete c1.jpg before adding the suffixe?

Thank you so much for your time and your help…

Glad that it works now!

Yes, using the replace() command. You can explore what it can do with the documentation in the auto-complete: