Difference between revisions of "Jython Scripting"

(Added category Scripting)
Line 8: Line 8:
 
<p>Functions are objects, and thus storable in variables.</p>
 
<p>Functions are objects, and thus storable in variables.</p>
 
<p>Jython (and python in general) accepts a mixture of procedural and object-oriented code.</p>
 
<p>Jython (and python in general) accepts a mixture of procedural and object-oriented code.</p>
 +
<p>Jython currently implements the Python language at its 2.5 version. All [http://www.python.org/doc/2.5.2/ documentation for python 2.5] applies to Jython bundled with Fiji.</p>
  
 
<h3>Defining variables: obtaining the current image</h3>
 
<h3>Defining variables: obtaining the current image</h3>

Revision as of 10:34, 16 October 2008

Jython tutorial for ImageJ

Language basics

Any text after a # is commented out.

There are no line terminators (such as ';' in other languages), neither curly braces to define code blocks.

Indentation defines code blocks.

Functions are defined with def, and classes with class.

Functions are objects, and thus storable in variables.

Jython (and python in general) accepts a mixture of procedural and object-oriented code.

Jython currently implements the Python language at its 2.5 version. All documentation for python 2.5 applies to Jython bundled with Fiji.

Defining variables: obtaining the current image

imp = IJ.getImage()

Which is the same as:

imp = WindowManager.getCurrentImage()

Since calling the above is long and tedious, one can declare a variable that points to the above static methods:

c = WindowManager.getCurrentImage

Above note the lack of parentheses.

To execute the function, just use parentheses on it:

imp = c()

The above gets the value of c, which is the method named getCurrentImage in class WindowManager, and executes it, storing its returned object in imp.

Manipulating pixels

Creating a grayscale ramp image

First create an image and obtain its pixels:

imp = ImagePlus("my new image", FloatProcessor(512, 512))
pix = imp.getProcessor().getPixels()

The length of an array:

n_pixels = len(pix)

Then loop to modify them:

# catch width
w = imp.getWidth()

# create a ramp gradient from left to right
for i in range(len(pix)):
  pix[i] = i % w

# adjust min and max, since we know them
imp.getProcessor().setMinAndMax(0, w-1)

... and show the new image:

imp.show()


Creating a random 8-bit image

First import necessary packages: Random, from standard java util library, and jarray, the Jython module for native java arrays:

from java.awt import Random
from jarray import zeros

Then create the array and fill it with random bytes:

width = 512
height = 512

pix = zeros(width * height, 'b')
Random().nextBytes(pix)

Now make a new IndexColorModel (that's what ImageJ's ij.process.LUT class is) for 8-bit images:

channel = zeros(256, 'b')
for i in range(256):
    channel[i] = (i -128) 
cm = LUT(channel, channel, channel)

... and compose a ByteProcessor from the pixels, and assign it to an ImagePlus:

imp = ImagePlus("Random", ByteProcessor(width, height, pix, cm)
imp.show()

Creating a random image, the easy way

All the above can be summarized like the following:

 from java.util import Random
 imp = IJ.createImage("A Random Image", "8-bit", 512, 512, 1)
 Random().nextBytes(imp.getProcessor().getPixels())
 imp.show()

Example: running a watershed plugin on an image

 # Obtain an image
 imp = IJ.openImage("http://rsb.info.nih.gov/ij/images/bridge.tif")
 ip = imp.getProcessor()
 
 # apply a threshold: only zeros and ones
 ip.setThreshold(0, 74)
 Thresholder().run("mask")
 ip.invert()
 
 # apply watershed
 edm = EDM()
 edm.setup("watershed", imp)
 edm.run(ip)
 
 imp.show()

The EDM plugin that contains the watershed could have been indirectly applied to the currently active image, which is not recommended:

 IJ.run("Watershed")

If you had called show() on the image at any early stage, just update the screen with:

 imp.updateAndDraw()

The Jython interpreter plugin

The interpreter provides a screen and a prompt. Type any jython code on the prompt to interact with ImageJ.

Launch it from plugins - Scripting - Jython Interpreter. See Scripting Help for all keybindings, and also Scripting comparisons.

Within the interpreter, all ImageJ, java.lang.* and TrakEM2 classes are automatically imported. So creating new images and manipulating them is very straighforward.

Creating Jython scripts: the workflow

The recommended setup is the following:

  • Edit a file in your favorite text editor, and save it with a an underscore in the name and a .py extension anywhere under ImageJ plugins folder.
  • Run Plugins - Scripting - Refresh Jython scripts only the very first time after newly creating the file under any folder or subfolder of ImageJ's plugins folder. A menu item will appear with its name, from which it can be run.
  • Keep editing (and saving) the file from your editor. Just select the menu item to execute it over and over. Or use the "Find..." command window to launch it easily (keybinding 'l').

The next time Fiji is run, automatic commands in macros/StartupMacros.txt will setup all your scripts in the Plugins menu.