Save Results Table in Macro for Batch Processing Images

Hi there,

I am new to this forum and I was wondering if someone could help me with my macro. I have several images I want to batch process and save the results table as a .xls or .csv file for each image. The problem I have with my macro is that when it goes to process each image, a window pops up. I think this because of the getDirectory command - this is where I save the spreadsheet, currently on my desktop. However, I keep having to hit select for each image. As I have several images, I want to avoid this and just run the macro without further input. I would appreciate any help on this if possible.

dir = getDirectory ("");
name = getTitle; 
dotIndex = indexOf(name, "."); 
name = substring(name, 0, dotIndex); 
run("Split Channels");
run("Enhance Contrast", "saturated=0.35");
run("Gaussian Blur...", "sigma=10");
run("Subtract Background...", "rolling=50");
setAutoThreshold("Moments dark");
setAutoThreshold("Moments dark");
setThreshold(10, 255);
//setThreshold(10, 255);
run("Convert to Mask");
run("Subtract Background...", "rolling=16");
run("Mean", "block_radius_x=2 block_radius_y=2");
setAutoThreshold("Moments dark");
setAutoThreshold("Moments dark");
run("Convert to Mask");
run("Set Measurements...", "area mean standard min integrated redirect=[Blue] decimal=3");
run("Analyze Particles...", "size=6-150 pixel show=[Overlay Masks] display exclude summarize");
saveAs("Measurements", dir + name +"Results.csv");

Good day,

this is a cross-post from the ImageJ-list:

You’ve received two suggestions on the list of how to solve the issue.
If your problem still exists, then please supply more information.



Hi djw23,

A plugin I wrote might help you:

I know imageJ already has the native capability to save the results table as a .csv but my plugin can create a new or append an existing .xlsx excel file on the user’s desktop and works very well with batch processing. Install it as you would for any plugin available from the update site manager in imageJ.

To implement the plugin just call it from the macro:
run(“Read and Write Excel”);


1 Like

Hi Antinos,
Your “Read and Write Excel” is fantastic. I measured several sample. All results went to a sheet in excel. It seems that several tables or groups in a sheet. Could you give a little more information or trick how to proceed these data, such make table and summarize it.

Thank you


Hey Antinos,
so I wanted to use your plugins for exporting the Results of Cell Count Analysis. (I mostly need the Marker Positions) But after using the Plugin it only prints the file name and into the Excel sheet.

Do you have any Idea were the problem might be?


Hi Elaias,

I think I just replied to you on youtube. I believe the issue here is related to the way the ‘Cell Counter’ plugin (Plugins>Analyze>Cell Counter) is handling the creation and population of a ‘results’ table. I couldn’t easily find the source code for the Cell_Counter plugin on github so I decompiled the .class file to have a peak at what might be going on. From my lay-person’s perspective, the cell counter plugin does not seem to be populating a normal ImageJ results table. I think it is calling the deprecated ‘IJ.write’ command instead of ‘ResultsTable.setValue’ or equivalent, which might be part of the problem. Regardless, it is clear that the results table generated by the Cell Counter plugin is not behaving normally. Even ImageJ macro commands and GUI functions cannot retrieve from or manipulate data in the table. Also, the table cannot be renamed without the loss of its contents. For these reasons, I cannot see an obvious work-around for this problem. In the past, I am pretty sure that the contents of the Cell Counter results table could be saved with the normal ImageJ ‘saveas’ function, but even that doesn’t seem to work currently.

Kind regards.

Hello Antony,

Yes I also asked this on Youtube, just read your answer there. Thank you so much with helping me figure out the problem. I will surely try to contact the people from Cell Counter and confront them with this issue. Hopefully there is a way to figure this out.
Thanks for the fast replies!
Kind regards,


Sorry I couldn’t be more help.

In case you don’t have any luck contacting the plugin author/maintainer, I also found this thread, where the use of an alternative ImageJ tool (MTB Cell Counter) was recommended:

Also, here is the MTB Cell Counter Wiki:

I haven’t used it myself, so cannot vouch for its stability or utility, although it does look appropriate and well maintained.

Best wishes.


Need help in saving a .csv/xlsx/xls file.

My script worked once, and then I got an error message w/o touching the code.
@haesleinhuepf @aklemm I assume that you know how to solve it.

//1. Open a floating 32-bit .tiff file from ilastik before running this script:

//2. Seletcting a file from the source folder of the ***RED*** channel
//and getting image name
orgName =getTitle();

//3. Selecting the output folder to save ROIs later
output_path = getDirectory("Choose output folder"); 
fileList = getFileList(output_path); 

//Made for segmentation images made by ilastik
//4. Selecting the image window and start processing segmentation images made by ilastik
run("Duplicate...", "title=" + orgName + "copy");

//5. Making nuclei value = 1, Background = 0

//7. Making an 8-bit from 32-bit (made by ilastik)

//8. Making whole nuclei
run("Fill Holes");

//9. Counting nuclei
run("Set Measurements...", "area perimeter display redirect=None decimal=3");
run("Analyze Particles...", "size=200-Infinity display exclude clear include summarize add in_situ");

//10. Saving ROIs to a .zip file
roiManager("save", output_path + orgName + ".zip")

//11. Show process complete
showMessage("ROI Extraction", orgName + " ROIs .zip file saved!");

//12. Saving Results to a .csv
saveAs("Nuclei_count",  output_path + orgName + ".csv");

Everything else works fine.


the first string in the saveAs command has to be the format, in your case “results”.

saveAs("results", output_path + orgName + ".csv");


Ok, so maybe I did change the code. Oops.

Thanks @aklemm!