Using a macro to process images and save them with their original name

Hello! We use a macro to process images for colocalization. We then wanted to just batch process an entire folder but we could not figure out how to have imagej grab the names of each individual picture and save the processed images with their corresponding names. Can anyone please help us? Thank you!

Hi @j0v4n4

Welcome to the Forum!

So… that is very doable… You can take a look at the following code:

/*
* Macro to process multiple images (.tif) in a folder/subfolders.
*/

#@File(label = "Input directory", style = "directory") input
#@File(label = "Output directory", style = "directory") output
#@String(label = "File suffix", value = ".tif") suffix
	
processFolder(input);
	
// function to scan folders/subfolders/files to find files with correct suffix
function processFolder(input) {
	list = getFileList(input);
	for (i = 0; i < list.length; i++) {
		if(File.isDirectory(input + File.separator + list[i]))
			processFolder("" + input + File.separator + list[i]);
		if(endsWith(list[i], suffix))
			processFile(input, output, list[i]);
	}
}

function processFile(input, output, file) {
	// INSERT YOUR OWN CODE IN HERE - WILL PROCESS EACH IMAGE INDIVIDUALLY
        // To save your images by name - the name of the image is saved as the variable 'file'...
}

This code is modified from this slide from the Scripting with Fiji workshop. You might find that helpful to go through… you can use the sample data we provide and that original code as a test to see how it works saving with the same file name, etc.

These are some other helpful links for Scripting in ImageJ/Fiji:

Hope this helps a bit!

eta :slight_smile:

1 Like

Wow! Thanks a ton eta! I will get to it :smiley:

1 Like

So, I keep getting an error when running our script. I am not sure where it is finding an error, but I think the program is having trouble with labels? Sorry to keep bugging, can you please help?

I can also attach the macro we are trying to use. Works so well with a single tiff. file :frowning:

Here is what we have:

run("Split Channels");
close();

run("Enhance Local Contrast (CLAHE)", "blocksize=50 histogram=256 maximum=3 mask=*None* fast_(less_accurate)");
run("Bandpass Filter...", "filter_large=60 filter_small=6 suppress=None tolerance=5 autoscale saturate");
run("Threshold...");
setThreshold(190, 255);
run("Convert to Mask");
run("Close");
run("Fill Holes");
run("Erode");
run("Erode");
run("Erode");
run("Erode");
run("Dilate");
run("Dilate");
run("Dilate");
run("Dilate");
saveAs("Tiff", "C:\\Users\\navarretejs\\Documents\\Green.tif");
run("Analyze Particles...", "size=300-1000 show=Outlines display summarize");
close();
close();
run("Enhance Local Contrast (CLAHE)", "blocksize=50 histogram=256 maximum=3 mask=*None* fast_(less_accurate)");
run("Bandpass Filter...", "filter_large=60 filter_small=6 suppress=None tolerance=5 autoscale saturate");
run("Threshold...");
setThreshold(190, 255);
run("Convert to Mask");
run("Close");

run("Fill Holes");
run("Erode");
run("Erode");
run("Erode");
run("Erode");
run("Dilate");
run("Dilate");
run("Dilate");
run("Dilate");
saveAs("Tiff", "C:\\Users\\navarretejs\\Documents\\Red.tif");
run("Analyze Particles...", "size=300-1000 show=Outlines display summarize");
close();
close();

//Reopen Red and Green images and create a combined image of the overlap
open("C:\\Users\\navarretejs\\Documents\\Green.tif");
open("C:\\Users\\navarretejs\\Documents\\Red.tif");
imageCalculator("AND create", "Green.tif","Red.tif");
selectWindow("Result of Green.tif");
saveAs("Tiff", "C:\\Users\\navarretejs\\Documents\\Overlap.tif");
close();
close();
close();
open("C:\\Users\\navarretejs\\Documents\\Overlap.tif");
run("Analyze Particles...", "size=300-1000 show=Outlines display summarize");
close();
close();
selectWindow("Summary");

Thank you

@j0v4n4

What do you mean by this? Sorry… too - what exactly are you trying to do with your macro? (I’m still waking up … coffee hasn’t quite kicked in yet!)

This is a separate issue you are having to your original post?

It would be great if you could attach some images for us to run using your code - would be more helpful to de-bug.

eta

Oh yes sorry, so with our macro we were trying to process a tiff file to get a cell count for 2 different channels (red and green) as well as a count for the cells that overlap. So we have our image and split the channels (red and green) and have the macro do a cell count for us.

Initially we had an issue with batch processing b/c we didn’t know how to tell imagej to grab the names of each individual picture and save the processed images with their corresponding names.

I tried to use your modified code to help us with this issue but I kept getting errors :frowning:

I have attached a couple of images. That would be great if you could use these and run the code and help us de-bug :smiley:

Thank you so much!

@j0v4n4

So… I am unable to run your macro code on any of these images… they are all RGB - so I am not getting multiple channels here.

But what I can tell you regarding debugging your code…

  1. You should really take some time to look at the links I posted above - go through the scripting workshop - it will help.
  2. The example code I posted there also has some solutions regarding the use of Script Parameters and variables to make your code more general (as opposed to hardcoding directories, etc.)… try to incorporate some of those elements into your code.
  3. You could actually make some macro functions for your processing of the green and then red channels - a function for each. That will help to organize your code a bit, etc.

If you are still having troubles - post again - but try a bit more on your own first. You are already halfway there!

eta

2 Likes

Thanks eta! Will do :smiley: I’ll let you know how things go!

1 Like