Scripting toolbox

Revision as of 07:45, 24 May 2011 by Eglinger (talk | contribs)

This page is meant to provide small code snippets as a starting point for writing scripts. See also the Scripting comparisons and the dedicated pages for the ImageJ Macro language, Javascript, Jython, JRuby, Clojure, and Beanshell.

For scripting python/jython, see also Albert Cardona's comprehensive Fiji tutorial.

Opening an image using ImageJ

Macro

path = "/path/to/file";
open(path);

Javascript

importClass(Packages.ij.IJ);

var path = "/path/to/file";
var imp = IJ.openImage(path);
imp.show();

Python

from ij import IJ

path = "/path/to/file"
imp = IJ.openImage(path)
imp.show()

Ruby

Clojure

(import 'ij.IJ)

(let [path "/path/to/file"
      imp (IJ/openImage path)]
  (.show imp))

Beanshell

import ij.IJ;

path = "/path/to/file";
imp = IJ.openImage(path);
imp.show();

Opening an image using Bio-Formats

Macro

path = "/path/to/file";
run("Bio-Formats Importer", "open=" + path + "autoscale color_mode=Default view=Hyperstack stack_order=XYCZT");

Javascript

importClass(Packages.loci.plugins.BF);

var path = "/path/to/file";
var imps = BF.openImagePlus(path);

or, with more options:

importClass(Packages.loci.plugins.BF);
importClass(Packages.loci.plugins.in.ImporterOptions);
importClass(Packages.loci.common.Region);


var path = "/path/to/file";
var options = new ImporterOptions();
options.setId(path);
options.setAutoscale(true);
options.setCrop(true);
options.setCropRegion(0, new Region(x, y, w. h));
options.setColorMode(ImporterOptions.COLOR_MODE_COMPOSITE);
var imps = BF.openImagePlus(options);
  • Python
  • Ruby
  • Clojure
  • Beanshell


Opening, processing, and saving a sequence of files in a folder

Macro

dir1 = getDirectory("Choose Source Directory ");
dir2 = getDirectory("Choose Destination Directory ");
list = getFileList(dir);

for (i=0; i<list.length; i++) {
  if (endsWith(list[i], ".tif")) {
    open(dir1 + list[i]);
    // process the image
    saveAs("TIFF", dir2+list[i]);
    close();
  }
}

See also the tutorial How to apply a common operation to a complete directory.

  • Javascript
  • Python
  • Ruby
  • Clojure
  • Beanshell