This is an archive of the old MediaWiki-based ImageJ wiki. The current website can be found at imagej.net.

Scripting Headless

Learn
Topics
Introduction
Getting Started
User Guides
Tutorials
Tips and Tricks
Presentations
Plugins
Techniques
All Techniques
Colocalization
Deconvolution
Registration
Segmentation
Stitching
Tracking
Visualization
Scripting
Overview
User input
Basics of script writing
How to use the Javadoc
Batch processing
Script Editor
Auto Imports
Templates
Running headlessly
Comparisons
Toolbox
Multithreading in Clojure
Multithreading in JavaScript
Chess in Jython
Languages
BeanShell
Groovy
ImageJ Macro
JavaScript
Lisp (Clojure)
MATLAB
Python (Jython)
R (Renjin)
Ruby (JRuby)
Scala

ImageJ scripts are designed to operate independently of user interface—including headless, with no user interface. This is made easy with the use of script parameters.

To start ImageJ in the ImageJ2 headless mode, run (with the launcher appropriate for your system substituted):

./ImageJ-linux64 --ij2 --headless

By default, when ImageJ runs headlessly it acts like a one-off program: it will only perform the requested operations, then quit. To run a script headlessly, use:

./ImageJ-linux64 --ij2 --headless --run path/to/script [key1=value1,key2=value2,...]



Basic run

Let's say we have the following Python script saved in a file, hello.py:

#@String name

print('Hello ' + name)

we could run this script with the command:

./ImageJ-linux64 --ij2 --headless --console --run hello.py 'name="Mr Kraken"'

Note that the name parameter must be enclosed in double quotes, since it is a string literal.

The optional --console argument allows to have print, IJ.log and error statements returned to the console window.


On Windows systems, single/double quotes might be inverted though, such that strings are enclosed in single quotes while the list of argument as well as the path to the py script are in double quotes.

ImageJ-win64.exe --ij2 --headless --console --run "PathTo/hello.py" "name='Mr Kraken'"

Multiple parameters

If your script has more than one parameter:

#@String name1
#@String name2

print('Hello ' + name1 + " and " + name2)

then these are filled by using a comma-separated list of parameter pairs—e.g.:

./ImageJ-linux64 --ij2 --headless --console --run hello.py 'name1="Mr",name2="Mrs Kraken"'

Similarly for Windows (again respect single/double quotes),

ImageJ-win64.exe --ij2 --headless --console --run "PathTo/hello.py" "name1='Mr', name2='Kraken'"