# An ImageJ batch processing macro that works

G15th March 2017

A helpful generic ImageJ macro script that can be used to loop through files of type in a selected directory and then do something to them.

ImageJ is a public domain, Java-based image processing program developed at the National Institutes of Health in the USA. The programme is often used in scientific research where its macros can be deployed to edit and analyse technical images (such as photos taken under a microscope).

ImageJ has a macro language (called Image J Macro Language or IJM) which can be used to automate most commands. Unfortunately ImageJ is not particularly intuitive and many users suffer difficulties when attempting to use ImageJ for the first time.

The following code is for a macro which will prompt a user in ImageJ to select a folder containing PNG images. It will then loop through each PNG file in the directory and perform a function.

// Blank ImageJ Macro Script that loops through files in a directory

// Specify global variables

input = getDirectory("Input Directory");
output = input; // Output images to the same directory as input (prevents second dialogue box, otherwise getDirectory("Output Directory"))
Dialog.create("File Type");
Dialog.addString("File Suffix: ", ".png", 5); // Select another file format if desired
suffix = Dialog.getString();

processFolder(input);

// Scan folders/subfolders/files to locate files with the correct suffix

function processFolder(input) {
list = getFileList(input);
for (i = 0; i < list.length; i++) {
if(File.isDirectory(input + list[i]))
processFolder("" + input + list[i]);
if(endsWith(list[i], suffix))
processFile(input, output, list[i]);
}
}

// Loop through each file

function processFile(input, output, file) {

// Define all variables

MonthNames = newArray("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"); // Generate month names
DayNames = newArray("Sun", "Mon","Tue","Wed","Thu","Fri","Sat"); // Generate date names
getDateAndTime(year, month, dayOfWeek, dayOfMonth, hour, minute, second, msec); // Get date and time information

// Do something to each file

open(file);
close(file);

// Print log of activities for reference...

print (DayNames[dayOfWeek], dayOfMonth, MonthNames[month], year + "," + hour + ":" + minute + ":" + second + ": Processing " + input + file);
}

// A final statement to confirm the task is complete...

print("Task complete.");

In this case, a file in the directory is opened and then closed again, but of course this function would be supplimented with other actions in between. A log of activities is produced to show that things are working, but this option can be deleted from the script if not required.

Other scripts and methodologies that purport to perform this function exist, but I have had trouble getting them to work. Likewise, a previous version of the above code with @Parameters refused to run outside the Script Editor in ImageJ 1.51j.

This code should be saved to a text file using a .ijm extension and executed in ImageJ via Plugins > Macros > Run. A copy is also available on Github Gist.