Image batch processing

I have the following problem I hope you can help. I am using Fiji imageJ for image processing and I don’t have much experience with Java, and this might be a too-tall order, but here goes. I have a directory path starting at = “/username/desktop/rawdata/6_month/image_type_n” within this directory there are n number of directories called animal_no_n, that again contain n number of directories named brainsection_n which finally contain a list of tif. files needing a certain type of image processing, feature extraction and saving those as csv files sorted by each brain section under each mouse no. An example for the final path to the files would be = “/username/desktop/rawdata/6_month/Image_type_1/animal_no_1/brainsection_1/”. Is it possible to run four for loops within each other? so that a for loop starts from the directory “image_type_1” where I ask it to open the directory animal_no_1 to n and starting at animal_no_1 then run a for loop opening all brain sections directories starting at the first brain section with a for loop finally running all the image processing on the tif files.

Hope my problem was clear enough (Basically four for loops within each other). Thank you in advance for any feedback.

Dear @SabaM ,

that’s a great question and good news: You don’t need to learn Java for that. In Fijis script editor, you find a template macro, that goes through folders and sub folders to process all images it finds:


In it, there is a function that processes a given file in one of the folders. For developing it, I would recommend replacing lines 5-7 with something like the following so that you don’t need to enter stuff in a dialog over and over:

input = "/username/desktop/rawdata/";
output = "/username/desktop/analysis_results/";
suffix = "tif"; 

If you want to learn more about functions (like processFile in the example) and user interfaces (the stuff that start with #@, check out these two mini lectures:

Let us know how it goes and if you need any furhther support or explanation :slight_smile:


1 Like

Hi Robert,

Thank you for the very helpfully reply. Your youtube channel is great, but I am having trouble with saving them correctly.

I have the following script:
// Macro template to process multiple images in a folder
input = “/Users/sabamolhemi/Desktop/rawdata/6_month/Tg/dapi”;
output = “/Users/sabamolhemi/Desktop/Results/6_month/Tg/dapi”;
suffix = “tif”;


// function to scan folders/subfolders/files to find files with correct suffix
function processFolder(input) {
list = getFileList(input);
list = Array.sort(list);
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) {
setAutoThreshold(“Default dark”);
//setThreshold(76, 255);
setOption(“BlackBackground”, true);
run(“Convert to Mask”);
run(“Analyze Particles…”, “size=120-Infinity pixel display exclude clear include summarize add in_situ”);
saveAs(“Results”,output + File.separator + file);

// Do the processing here by adding your own code.
// Leave the print statements until things work, then remove them.
print("Processing: " + input + File.separator + file);
print("Saving to: " + output);

It seems that it runs through all my folders, subfolders and files, but I am having trouble saving them according to mouse number and under each mouse number saving the cvs files clustered together in each brain section directory.
I was imagining something in the line, that once opening the first animal directory, to ask the program to create a new directory at the output folder (here: “/Users/sabamolhemi/Desktop/Results/6_month/Tg/dapi”) and automatically naming the new directory after the current animal_no_n directory, and once opening the first brain section folder (BS_n) to create a subfolder also named automatically after the current BS_n folder in the newly made animal no directory. Thereafter saving all tif files as cvs files under that newly made output directory: “/Users/sabamolhemi/Desktop/Results/6_month/Tg/dapi/current animal_no_n/current BS_n”). There might be a better way of saving them, but just as the files in the input path are sorted, the automated process of saving needs to be saved in the same sorted manner automatically.

Is this possible, or is there another way of getting this done?

Just a side question; would it be possible to code all of this on matlab and then compile it to work on imageJ?

Thanks you again for the help, appreciate it!
Saba :slight_smile: