I'm writing an ImageJ macro to iterate through a folder of .lsm confocal microscope images, make a Z Project from each, combine the two channels into red and green, and save as an RGB image. The code works fine for one directory, but now it's throwing an error that has something to do with the filename from getFileList
.
Here's the relevant part of the code:
dir1 = getDirectory("Choose Source Directory ");
format = getFormat();
dir2 = getDirectory("Choose Destination Directory ");
list = getFileList(dir1);
setBatchMode(true);
for (i = 0; i < list.length; i++) {
showProgress(i+1, list.length);
open(dir1+list[i]);
run("Z Project...", "projection=[Max Intensity]");
run("Split Channels");
run("Merge Channels...", "c1=C1-MAX_" + list[i] + " c2=C2-MAX_" + list[i]);
saveAs(format, dir2 + list[i]);
close();
}
It's necessary that "C1-MAX_"
is appended to the input string because those prefixes are added to the image name during the calls to Z Project and Split Channels.
For reference, the name of the files look like Negative 1 5x.lsm
, Negative 2 5x.lsm
, Positive 1 5x.lsm
, etc. Based on these filenames, I expect ImageJ to call the Merge Channels command with the strings "C1-MAX_Negative 1 5x.lsm" and "C2-MAX_Negative 1 5x.lsm".
Instead, I get the error message "C1-MAX_Negative" is not a valid choice for "C1 (red):"
I don't understand why ImageJ is trying to call Merge Channels with the string "C1-MAX_Negative" and not "C1-MAX_Negative 1 5x.lsm". Why isn't list[i]
returning what I think it should?
It is probably because of the space in the filename. Try putting the name in square brackets like that:
run("Merge Channels...", "c1=[C1-MAX_" + list[i] + "] c2=[C2-MAX_" + list[i] + "]");