Difference between revisions of "Scripting toolbox"

(Javascript)
(Javascript)
Line 21: Line 21:
  
  
* Python
+
==== Python ====
  
* Ruby
+
<source lang="python">
 +
from ij import IJ
  
* Clojure
+
path = "/path/to/file"
 +
imp = IJ.openImage(path)
 +
imp.show()
 +
</source>
  
* Beanshell
+
==== Ruby ====
 +
 
 +
==== Clojure ====
 +
 
 +
<source lang="lisp">
 +
(import 'ij.IJ)
 +
 
 +
(let [path "/path/to/file"
 +
      imp (IJ/openImage path)]
 +
  (.show imp))
 +
</source>
 +
 
 +
==== Beanshell ====
 +
 
 +
<source lang="java">
 +
import ij.IJ;
 +
 
 +
path = "/path/to/file";
 +
imp = IJ.openImage(path);
 +
imp.show();
 +
</source>
  
 
=== Opening an image using Bio-Formats ===
 
=== Opening an image using Bio-Formats ===

Revision as of 08:54, 19 May 2011

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.

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();
  }
}
  • Javascript
  • Python
  • Ruby
  • Clojure
  • Beanshell