Scripting basics

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
Parameters
Basics of script writing
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


Introduction

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
 */
print(imp)

import ij.IJ

IJ.run(imp, "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 IJ.run(). 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)
IJ.run(imp, "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)
IJ.run(imp, "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='http://wsr.imagej.net/images/clown.jpg') fileUrl
 * @File(label='local image') file
 */

// Load a sample file from the internet and a local file of your choice.
dataset1 = ds.open(fileUrl)
dataset2 = ds.open(file.getAbsolutePath())
// Display the datasets.
ui.show(dataset1)
ui.show(dataset2)

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='http://wsr.imagej.net/images/clown.jpg') 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.
imagePlus1.show()
imagePlus2.show()

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

import ij.io.Opener
import ij.io.OpenDialog

// 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.
imagePlus.show()