Scripting toolbox
short URLFrom Fiji
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.
Note: To copy the snippets, just double-click somewhere into the code. If Javascript is enabled, this will automatically select the complete snippet.
Contents |
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
include_class 'ij.IJ' path = "/path/to/file" imp = ij.IJ.openImage(path) imp.show
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); imps[0].show();
or, with more options:
importClass(Packages.loci.plugins.BF); importClass(Packages['loci.plugins.in.ImporterOptions']); // 'in' is a reserved word, hence the different syntax 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); imps[0].show();
- 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(dir1);
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
Wait a given amount of time, or until user presses OK
Macro
wait(100);
or
waitForUser();
Javascript
Thread.sleep(100);
or
- Python
- Ruby
- Clojure
- Beanshell
Select multiple ROIs from ROI manager and combine them
Macro
roiManager("select", newArray(0,2,4));
roiManager("AND");
Javascript
importClass(Packages.ij.plugin.frame.RoiManager);
rm = RoiManager.getInstance();
rm.setSelectedIndexes([0,2,4]);
rm.runCommand("AND");
Python
from ij.plugin.frame import RoiManager
rm = RoiManager.getInstance()
rm.setSelectedIndexes([0, 2, 4])
rm.runCommand("AND")
Beanshell
import ij.plugin.frame.RoiManager;
rm = RoiManager.getInstance();
rm.setSelectedIndexes(new int[] { 0, 2, 4});
rm.runCommand("AND");
- Ruby
- Clojure
Unlocking an image
Sometimes things go wrong and all you see is "blabla.jpg is locked" when you try to process the image in some way. Then all you can do is to force-unlock the image, like so:
Beanshell
IJ.getImage().unlock();