Scripting Headless

Revision as of 08:28, 29 February 2016 by Hinerm (talk | contribs) (Create headless scripting guide)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Template:Scripting

ImageJ2 scripts are designed to operate independently of User Interface - including "headlessly", 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 ["script parameters"]

Basic run

Let's say we 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 --run hello.py "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, in order of declaration, by using a comma-separated list of parameters e.g., :

./ImageJ-linux64 --ij2 --headless --run hello.py "Mr,Mrs Kraken"

Mapping parameters

Because parameters are filled in order of declaration, all the "user" parameters should be declared first (that is, the actual inputs you want to change from run to run). Sometimes you may need to run a script where the "user" parameters are declared after "system" parameters:

# @DatasetIOService data
# @String imagePath

img = data.open(imagePath)
print(img);

In this case, we have to assign values to each parameter with a "name=value" pair notation:

./ImageJ-linux64 --ij2 --headless --run test.py "imagePath=/home/hinerm/Fiji.app/samples/blobs.gif"

Multiple such parameters can still be combined when separated with commas.