Scripting basics

Revision as of 15:00, 23 January 2017 by Etarena (talk | contribs) (Add collapse logic for 'scripting' in menu)
Getting Started
User Guides
Tips and Tricks
All Techniques
User input
Basics of script writing
How to use the Javadoc
Batch processing
Script Editor
Auto Imports
Running headlessly
Multithreading in Clojure
Multithreading in JavaScript
Chess in Jython
ImageJ Macro
Lisp (Clojure)
Python (Jython)
R (Renjin)
Ruby (JRuby)


ImageJ and Fiji are able to run scripts written in different languages. Besides all the differences the approach on how to use the API of ImageJ is similar for all of them. This article will introduce the basic concepts and is valid for all scripting languages.

Get an image and perform an action

First we want to learn different ways to select an image and perform an action on it. In ImageJ1 the image is represented by an ImagePlus object. The recommended way to select an ImagePlus opbject is to use Script Parameters:

 * @ImagePlus imp
 * @Integer(label='Filter radius',description='The sigma of the gaussian filter.',value=2) sig

import ij.IJ, "Gaussian Blur...", "sigma=" + sig)

Script Parameters are placed at the beginning of the script file. If only one @ImagePlus is used, the front most image is selected. A second Script Parameter is used to get the radius of the gaussion filter. By using print(imp) we verify, that an ImagePlus object is assigned to the variable.

To perform an operation on the selected image, we use Therefore we have to import the class IJ. There are three different versions of the run() method of these we need the one with three parameters. The first parameter is the image to perform the action on, the second parameters defines the action (called command) and the last parameter is used to configure the action (here we set the filter radius). The easiest way to find a command is to use the Recorder.

The second approach is similar to how to perform this operation using the macro language:

import ij.IJ

imp = IJ.getImage()
sig = IJ.getNumber('Filter radius:', 2), "Gaussian Blur...", "sigma=" + sig)

The first step is to select the front most image by using IJ's method getImage(). The second step is to use the method getNumber() to show up a dialog to enter the filter radius. Running the filter is the same as in the previous example.

Finally we want to use the WindowManager to select the front most image:

import ij.IJ
import ij.WindowManager

imp = WindowManager.getCurrentImage()
sig = IJ.getNumber('Filter radius:', 2), "Gaussian Blur...", "sigma=" + sig)

This is nearly identical to the use of IJ.getImage() and therefore not recommended. The WindowManager class contains some useful methods that can be used to select more than one image (e.g. getImageTitles() and getIDList().

Opening images

In ImageJ there are many different ways to open images (or more general datasets). We want to introduce some of them.

The first example uses the DatasetIOService. It is part of SCIFIO, a flexible framework for SCientific Image Format Input and Output. Two types of image files are opened. The first one is an example image, downloaded from the Internet. The second image can be chosen by the user. Both datasets are displayed using the UIService that is part of the SciJava project.

 * @DatasetIOService ds
 * @UIService ui
 * @String(label='Image URL', value='') fileUrl
 * @File(label='local image') file

// Load a sample file from the internet and a local file of your choice.
dataset1 =
dataset2 =
// Display the datasets.

If a script only depends on ImageJ1 functionality, one can use the function IJ.openImage(). It will return an ImagePlus object.

 * @String(label='Image URL', value='') fileUrl
 * @File(label='local image') file

import ij.IJ

// Load a sample file from the internet and a local file of your choice.
imagePlus1 = IJ.openImage(fileUrl)
imagePlus2 = IJ.openImage(file.getAbsolutePath())
// Display the datasets.

IJ.openImage() is based on the class You can use it directly to open images and other files (e.g. text files). The example uses the class to select a file. This is an alternative to the usage of the Scripting Parameter @File.


// Use the OpenDialog to select a file.
filePath = new OpenDialog('Select an image file').getPath()
// Open the selected file.
imagePlus = new Opener().openImage(filePath)
// Display the ImagePlus.