SaveAs command with ROI-s on selected image

Tags: #<Tag:0x00007fd54029d458>


Hello everybody!
I am trying to write macro, but I am a total beginner.
I need open folder with many images (possible to open them only by Bio-formats).
Then process some analysis on image.
Save results to Excel table.
Save image - 3 with selected ROI-s (which is the results of - stack to images, the one was processed) in different folder.
Then Close all the windows and
Finally Open next image.

I am stack with line - SaveAs image 3
Here is my macro I already did :slight_smile:

path = getDirectory("D:\\POKUSY\\PEL-koppert\\POKUS\\KLICENIE-pel\\TEST_10 perc\\");
filelist = getFileList(path); //load array of all files inside input directory
 for (i=0; i< filelist.length; i++) {
            run("Bio-Formats Importer", "open= + path + filelist[i] autoscale color_mode=Custom rois_import=[ROI manager] view=Hyperstack stack_order=XYCZT series_0_channel_0_red=0 series_0_channel_0_green=255 series_0_channel_0_blue=0 series_0_channel_1_red=255 series_0_channel_1_green=0 series_0_channel_1_blue=0 series_0_channel_2_red=0 series_0_channel_2_green=0 series_0_channel_2_blue=255");
 //insert commands to edit image here
run("Stack to Images");
setOption("BlackBackground", true);
run("Make Binary");
run("Fill Holes");
run("Set Measurements...", "area standard centroid perimeter bounding fit shape integrated median skewness kurtosis area_fraction redirect=None decimal=3");
run("Analyze Particles...", "size=2000-15000 display exclude summarize add");
run("Read and Write Excel");
saveAs("Tiff", "D:\\POKUSY\\PEL-koppert\\POKUS\\KLICENIE-pel\\vysledky\\" + "3_01.tiff");
list = getFileList(input);
for (i = 0; i < list.length; i++)
action(input, output, list[i]);

Thank you very much for your help. janka


Hello Janka,

Some things:

  1. Currently your images are all saved to the same place and with the same name.

    • Assign another directory (e.g. path2) that will be your output directory: path2 = getDirectory("Choose output directory");

    • Get the name of the most recently opened file (your stack) and then add on โ€œ_3โ€ to the end: filename = File.nameWithoutExtension(); and then save using saveAs("Tiff", path2 + filename + "_03.tiff");

  2. Closing windows

    • You can close all the image windows using run("Close All"); or close specific windows using close(string); where string is any part of the image name e.g. close(".tiff"); to close all images open with โ€œ.tiffโ€ in the title.

Also, does the Read and Write Excel plugin open up a window? If you want to run the macro without user input, you could save the results using saveAs("Results", path2 + filename + "_3.csv"); - this file can be opened using Excel, but it wonโ€™t allow you to create multiple worksheets for Excel.

Hope this helps,



Hello Rob, thank you very much for your fast respond. I have tried your suggestions, but my macro has still some struggling parts. I will try to solve them by myself. But thank you one more time for your time and help, janka.


@nozka, donโ€™t be afraid to post your problems here, a lot of people can help. If you search the forums usually someone has had a similar problem also.

Go to this post in a different topic to find great resources from the ImageJ community that will help with macro scripting.