Skip to content

Run an ImageJ batch processing macro in “headless” mode

G23rd October 2018

CNo Comments

TImageJ, PowerShell

Instructions for running an ImageJ macro script in “headless” mode via the command prompt in Windows.

With the release of ImageJ version 2 (ImageJ2) comes the ability to run scripts in “headless” mode, which means that ImageJ can be launched without a GUI from the command prompt and variables parsed to the script. This offers numerous benefits, especially where large volumes of images need to be analysed, but there may also be limitations to function where a required plugin is bound to a GUI.

I have modified a batch processing macro so that it runs in --headless mode as this may be of considerable use to researchers.

Revised batch processing macro (that works!)

The following code is based on a previous script, with certain modifications:

  • GUI behaviours have been removed (such as the prompt to select a directory).
  • Input variables are flagged at the start of the script (ie #@String).
  • The timestamp component has been improved.
  • The input and output variables are assumed to be the same (but this can be changed with additional declarations).
// Blank ImageJ Macro Script that loops through files in a directory
// Written by Adam Dimech
// https://code.adonline.id.au/imagej-batch-process-headless/

// Specify global variables

#@String input
#@String suffix

// Add trailing slashes
input=input+"\\";
output=input; // This can be changed to a separate variable if need-be

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

Months = 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 day names
getDateAndTime(year, month, dayOfWeek, dayOfMonth, hour, minute, second, msec);
timestamp ="";
if (dayOfMonth<10) {timestamp = timestamp+"0";}
timestamp = ""+DayNames[dayOfWeek]+" "+Months[month]+" "+dayOfMonth+" "+year+", "+timestamp;
if (hour<10) {timestamp = timestamp+"0";}
timestamp = timestamp+""+hour;
if (minute<10) {timestamp = timestamp+"0";}
timestamp = timestamp+":"+minute+"";
if (second<10) {timestamp = timestamp+"";}
timestamp = timestamp+":"+msec;

// Do something to each file

open(input+file);

  // Insert additional code here

close(file);

// Print log of activities for reference...

print (timestamp + ": Processing " + input + file); 
}

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

print("Task complete.");

Save this code into a file with an .ijm extension.

Launching the script from the Command Prompt

In this example, we will ask our batch script (called “example.ijm”) to open PNG files in the folder at G:\Path\To\Image\Files (note that there is no trailing slash).

To launch ImageJ (Fiji) in “headless” mode and execute the batch script, use the following syntax in the Command Prompt to navigate to the folder containing ImageJ:

c:
cd "C:\Path\To\ImageJ\fiji-win64\Fiji.app"
ImageJ-win64 --ij2 --headless --console --run E:\Path\To\example.ijm "input='G:\Path\To\Image\Files', suffix='.png'"

Note how the input variables (ie input and suffix) are linked as a string in double quotaton marks, but the variables themselves are in single quotation marks (this is a Windows-specific requirement). The --console flag tells ImageJ to print the output of print() functions to the Command Prompt (so you can read what is happening).

Screen capture of the command prompt showing code.

Running an ImageJ batch processing script in “headless” mode in the Windows Command Prompt.

Launching the script from PowerShell

It is also possible to launch ImageJ in “headless” mode via PowerShell. The syntax is almost the same:

cd "C:\Path\To\ImageJ\fiji-win64\Fiji.app"
./ImageJ-win64 --ij2 --headless --console --run E:\Path\To\example.ijm "input='G:\Path\To\Image\Files', suffix='.png'"
Screen capture of Windows Power Shell showing lines of code.

Running an ImageJ batch processing script in “headless” mode in Windows PowerShell.

Code on GitHub

The example script has been added to GitHub Gist.

   

Comments:

No comments have yet been submitted. Be the first!

Have your say:

The following HTML is permitted:
<a href="" title=""> <b> <blockquote cite=""> <code> <em> <i> <q cite=""> <strike> <strong>

Comments will be published subject to the Editorial Policy.