Difference between revisions of "Jython Scripting"

(Visualize any number of TIFF stacks in a single composite multi-color image stack)
m (- With IJ.log(): Correct code formatting)
 
(126 intermediate revisions by 16 users not shown)
Line 1: Line 1:
== The Jython interpreter plugin ==
+
{{Learn | languages}}
  
The interpreter provides a screen and a prompt. Type any jython code on the prompt to interact with ImageJ.
+
== Introduction ==
  
Launch it from plugins - Scripting - Jython Interpreter. See [[Scripting Help]] for all keybindings, and also [[Scripting comparisons]].
+
Jython is an implementation of the Python programming language designed to run on the Java platform. <ref>[http://rsb.info.nih.gov/ij/plugins/index.html Wikipedia entry on Jython]. Accessed: 2016-08-30</ref> In ImageJ Jython is one of several [[Scripting#Supported_languages|supported languages]].
  
Within the interpreter, all ImageJ, java.lang.* and TrakEM2 classes are automatically imported. So creating new images and manipulating them is very straighforward.
+
== When to use Jython ==
  
 +
All scripting language supported by ImageJ can be used to access the [http://javadoc.imagej.net/ ImageJ API]. There are only differences in how the imports are handled and in the syntax of the selected language. Jython has a syntax that differs from most other language as indentations instead of brackets are used to group code blocks.
  
=== Language basics ===
+
The following list will help you to decide if Jython is the right choice to create scripts for ImageJ:
  
* Any text after a # is commented out.
+
* If you have experience with Python you can easily use Jython for ImageJ scripting. But you have to keep in mind that tools commonly used in many Python projects (e.g. Numpy) are not available in Jython. By building your [[Jython_Scripting#Self_written_Jython_modules_for_ImageJ|own modules]] you can create complex scripts that otherwise are only possible by writing ImageJ plugins in Java.
* There are no line terminators (such as ';' in other languages), neither curly braces to define code blocks.
+
* If don't have any experience in programming, the Python language is a good choice to start with. If your only aim is to write scripts for ImageJ, there are other languages you should try first (e.g. [[Groovy_Scripting|Groovy]]).
* Indentation defines code blocks.
+
* In Python many problems can be solved with less code than in other languages. Still the code is easy to read. Have a look at the examples on this page and decide if you want to start using Python for ImageJ scripting.
* Functions are defined with <i>def</i>, and classes with <i>class</i>.
 
* 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 [http://www.python.org/doc/2.5.2/ documentation for python 2.5] applies to Jython bundled with Fiji (with the remarks listed later).
 
  
 +
=== Explanation ===
  
=== Workflow for creating Jython scripts ===
+
The Java implementation of Python is limited to the [https://docs.python.org/2/library/index.html standard library] of Python 2.<br>
 +
It is not possible to use external python modules (like Numpy...) however any Java class residing in the Fiji installation can be used.<br>
  
The recommended setup is the following:
+
Even with the given limitations, Jython is a powerful language for ImageJ scripting. Hopefully the examples on this page can convince you of that.
  
* Edit a file in your favorite text editor, and save it with an underscore in the name and a .py extension anywhere under ImageJ plugins folder.
+
== Jython basics for ImageJ ==
* Run ''Plugins>Scripting>Refresh Jython scripts'' <b>only</b> 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 ''Plugins>Utilities>Find Commands...'' window to launch it easily (keybinding 'l').
 
  
The next time Fiji is run, it will setup all your scripts in the Plugins menu.
+
{{Notice|For an introduction in ImageJ scripting visit the page [[Scripting_basics|Scripting basics]].}}
  
=== Some limitations of jython ===  
+
=== Introduction ===
  
Though jython tries to be as close as possible as python, there are some differences you may experience during scripting.
+
The aim of this page is not to teach how to program in Python. This purpose is much better fulfilled by the [https://docs.python.org/2/library/index.html documentation of Python2]. The focus of this page is to show how features of the Python language can be useful for ImageJ scripting.
  
* <u>Float "special numbers" such as ''NaN'' and ''Inf'' are not handled.</u>
+
That is why more complex examples are used that are fully functional. Just copy the code to the [[Using_the_Script_Editor|Script Editor]] and try them by yourself. Extensive in-line documentation is used to explain the implementation.
For instance,
 
<source lang="python"> a = float('nan') </source>
 
will create the correct float number in python, but will throw an exception in jython.
 
  
Instead, to create a NaN in jython, use:
+
=== Hello World ===
<source lang="python">
+
==== - With print ====
>>> a = Double.NaN
+
There are 2 ways to print some information back to the user.<br>
>>> print a
+
The first one is a classical python print statement, that will print some information to the console. <br>
NaN
+
<code>print "Hello world"</code><br>
</source>
+
You can print any kind of variable and objects.<br>
To test if a number is NaN:
+
<code>print "This is a string followed by an int", 10</code>
<source lang="python">
 
>>> if Double.isNaN(a):
 
        print "a is NaN!"
 
a is NaN!
 
</source>
 
  
* <u>Some existing python modules can't be imported in jython.</u>
+
NB1 : If used in a plugin, and no console window is open then the printed information will not be visible to the user (contrary to the <code>log</code> function below)
This is for instance the case of the module ''numpy'', which would have been really convenient for analysing data and results.
 
  
But see these java numerical libraries: http://math.nist.gov/javanumerics/#libraries , of which:
+
NB2 : Using numerous print statements might slow down the execution time when used in a plugin (not observed when executing from the script interpreter).
  
*JaMa (Java Matrix Package)
 
*Java3D (particularly its [http://java.sun.com/products/java-media/3D/forDevelopers/j3dapi/javax/vecmath/package-summary.html vecmath] package provides general matrix and vector classes ([http://java.sun.com/products/java-media/3D/forDevelopers/j3dapi/javax/vecmath/GMatrix.html GMatrix], [http://java.sun.com/products/java-media/3D/forDevelopers/j3dapi/javax/vecmath/GVector.html GVector]).
 
  
... are already included in Fiji.
+
==== - With IJ.log() ====
 
+
<source lang=python>
== Jython tutorials for ImageJ ==
+
from ij import IJ
 
+
IJ.log("Hello world")
=== Defining variables: obtaining the current image ===
+
IJ.log("This is a string followed by an int " + str(10))
 
 
<source lang="python">
 
imp = IJ.getImage()
 
</source>
 
 
 
Which is the same as:
 
<source lang="python">
 
imp = WindowManager.getCurrentImage()
 
</source>
 
 
 
Since calling the above is long and tedious, one can declare a variable that points to the above static methods:
 
 
 
<source lang="python">
 
c = WindowManager.getCurrentImage
 
 
</source>
 
</source>
 +
Contrary to the print statement the log function display some output into a log window (newly open if not already open), and accept only a string as argument.
  
Above note the lack of parentheses.
+
=== Image selection using the GenericDialog class ===
  
To execute the function, just use parentheses on it:
+
This example script will create up to 10 new images and create a GenericDialog to select 3 of them. Finally the names of the selected images are printed to the Log window. It is recommended to copy the code to the [[Using_the_Script_Editor|Script Editor]] and run it by yourself.
  
<source lang="python">
+
The following list links to documentation of the used Python features:
imp = c()
+
* [https://docs.python.org/2/library/__future__.html Future statement definitions]
</source>
+
* [https://docs.python.org/2/library/functions.html Built-in Functions]
 +
* [https://docs.python.org/2/library/stdtypes.html#str.join str.join()-method]
 +
* [https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions List Comprehensions]
 +
* [https://www.python.org/dev/peps/pep-0289/ Generator Expressions]
 +
* [http://stackoverflow.com/questions/36901/what-does-double-star-and-star-do-for-python-parameters ** (double star) and * (star) parameters]
 +
* [https://docs.python.org/2/library/%5F%5Fmain%5F%5F.html Top-level script environment (__main__)]
 +
* [http://stackoverflow.com/questions/5893163/what-is-the-purpose-of-the-single-underscore-variable-in-python Purpose of the single underscore “_” variable]
  
The above gets the value of <i>c</i>, which is the method named getCurrentImage in class WindowManager, and executes it, storing its returned object in <i>imp</i>.
 
  
 +
{{GitHubEmbed|org=imagej|repo=imagej-scripting|path=src/main/resources/script_templates/Tutorials/Wiki_Jython_Tutorial_1.py}}
  
=== Manipulating pixels ===
+
=== Using Scripting Parameters ===
  
==== Creating a grayscale ramp image ====
+
The second example is inspired by atomic resolution images recorded with an Transmission Electron Microscope (TEM). Such images show a regular structure (a crystal), but the images are noisy because of the low signal. By using a Fourier filter the contrast can be enhanced.
First create an image and obtain its pixels:
 
  
<source lang="python">
+
The script will create a periodic structure and add some random noise. The user can control the parameters of the created image. This is realized using [[Script_parameters|Script parameters]]. The Fourier filtering has been created by using the [[Introduction_into_Macro_Programming#The_recorder|Recorder]]. Finally a simple image calculator is used to show that functions can be passed as parameters.
imp = ImagePlus("my new image", FloatProcessor(512, 512))
 
pix = imp.getProcessor().getPixels()
 
</source>
 
 
 
The length of an array:
 
 
 
<source lang="python">
 
n_pixels = len(pix)
 
</source>
 
 
 
Then loop to modify them:
 
 
<source lang="python">
 
# 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)
 
</source>
 
  
... and show the new image:
+
This list links to the documentation of Python features that are introduced with this example:
  
<source lang="python">
+
* [https://docs.python.org/2/library/functions.html#zip The zip() function]
imp.show()
+
* [http://stackoverflow.com/questions/8421337/rotating-a-two-dimensional-array-in-python Rotating a two-dimensional array]
</source>
+
* [https://docs.python.org/2/reference/expressions.html#lambda Lambda expressions]
  
  
==== Creating a random 8-bit image ====
+
{{GitHubEmbed|org=imagej|repo=imagej-scripting|path=src/main/resources/script_templates/Tutorials/Wiki_Jython_Tutorial_2.py}}
  
First import necessary packages: Random, from standard java util library, and [http://www.jython.org/archive/21/docs/jarray.html jarray], the Jython module for native java arrays:
+
=== A batch opener using <code>os.walk()</code> ===
  
<source lang="python">
+
We have yet introduced some powerful functions build into Python. Another one is <code>walk()</code> from the <code>os</code> module. It can be used to go through a directory structure and process the contained files. In this example <code>walk()</code> is used to batch open images with ImageJ's function <code>openImage()</code>.
from java.awt import Random
 
from jarray import zeros
 
</source>
 
  
Then create the array and fill it with random bytes:
+
To read more about the used features, the following list provides links to additional information:
  
<source lang="python">
+
* [https://docs.python.org/2/library/os.html#os.walk The walk() function]
width = 512
+
* [https://docs.python.org/2/library/os.path.html The documentation of os.path]
height = 512
+
* [https://docs.python.org/2/library/os.html#os.listdir The listdir() function]
+
* [http://javadoc.imagej.net/ImageJ1/ Javadoc on IJ.openImage()]
pix = zeros(width * height, 'b')
+
* [https://docs.python.org/2/library/functions.html#isinstance Testing the type of an object using isinstance()]
Random().nextBytes(pix)
+
* [https://docs.python.org/2/library/functions.html#type Identifying the type of an object using type()]
</source>
+
* [https://docs.python.org/2/reference/simple_stmts.html#continue Using continue to control a loop]
 +
* [https://docs.python.org/2/library/stdtypes.html#truth-value-testing Truth Value Testing]
  
(See the [http://www.jython.org/archive/21/docs/jarray.html jarray documentation] where the 'b'-byte, 'd'-double, etc. are explained.)
 
  
Now make a new IndexColorModel (that's what ImageJ's ij.process.LUT class is) for 8-bit images:
+
{{GitHubEmbed|org=imagej|repo=imagej-scripting|path=src/main/resources/script_templates/Tutorials/Wiki_Jython_Tutorial_3.py}}
  
<source lang="python">
+
== Importing Java module and classes ==
channel = zeros(256, 'b')
+
Another great feature of Jython is the possibility to use functions from Java jar package that resides in the jar folder of imageJ.
for i in range(256):
 
    channel[i] = (i -128)
 
cm = LUT(channel, channel, channel)
 
</source>
 
  
... and compose a ByteProcessor from the pixels, and assign it to an ImagePlus:
+
=== ImageJ and Fiji API ===
  
<source lang="python">
+
The following API documentation lists all available module and functions :
imp = ImagePlus("Random", ByteProcessor(width, height, pix, cm)
+
* [http://javadoc.scijava.org/ImageJ1/ ImageJ]
imp.show()
+
* [http://javadoc.scijava.org/Fiji/ Fiji]
</source>
 
  
==== Creating a random image, the easy way ====
+
Those package are built-in with Fiji, but any package that resides in the jars folder can be imported provided you know the path to the class.
  
All the above can be summarized like the following:
+
Let's show one example with the ImageJ package and the class [http://javadoc.scijava.org/ImageJ1/ij/plugin/frame/RoiManager.html RoiManager]. According to the javadoc the RoiManager class resides in <code>ij.plugin.frame</code>. Therefore the code will look like :
  
 
<source lang="python">
 
<source lang="python">
from java.util import Random
+
from ij.plugin.frame import RoiManager
imp = IJ.createImage("A Random Image", "8-bit", 512, 512, 1)
+
RM = RoiManager()       # we create an instance of the RoiManager class
Random().nextBytes(imp.getProcessor().getPixels())
+
rm = RM.getRoiManager() # "activate" the RoiManager otherwise it can behave strangely
imp.show()
 
 
</source>
 
</source>
  
=== Running a watershed plugin on an image ===
+
=== Using openCV in Jython ===
 
+
It is even possible to use most of opencv functionalities within Jython/Fiji. There are several options (see the [https://imagej.net/OpenCV wiki page about opencv]), yet the most straight forward is probably IJ-OpenCV which is available via the update sites. It will automatically download the necessary packages and dependencies in your Fiji installation.  
<source lang="python">
 
# 1 - Obtain an image
 
blobs = IJ.openImage("http://rsb.info.nih.gov/ij/images/blobs.gif")
 
# Make a copy with the same properties as blobs image:
 
imp = blobs.createImagePlus()
 
ip = blobs.getProcessor().duplicate()
 
imp.setProcessor("blobs copy", ip)
 
  
# 2 - Apply a threshold: only zeros and ones
+
A manual installation is also possible by putting the jar packages in the jar folder of imageJ. They are avalaible on the [https://github.com/joheras/IJ-OpenCV IJopenCV github], which even provides a maven option.
# Set the desired threshold range: keep from 0 to 74
 
ip.setThreshold(147, 147, ImageProcessor.NO_LUT_UPDATE)
 
# Call the Thresholder to convert the image to a mask
 
IJ.run(imp, "Convert to Mask", "")
 
  
# 3 - Apply watershed
+
==== Matrices ====
# Create and run new EDM object, which is an Euclidean Distance Map (EDM)
+
The first thing to know about OpenCV is that most functions work with an OpenCV matrix object. Fortunately, the IJ-OpenCV project provides some converters :
# and run the watershed on the ImageProcessor:
 
EDM().toWatershed(ip)
 
 
 
# 4 - Show the watersheded image:
 
imp.show()
 
</source>
 
 
 
The EDM plugin that contains the watershed could have been indirectly applied to the currently active image, which is <i>not</i> recommended:
 
  
 
<source lang="python">
 
<source lang="python">
imp = IJ.getImage()  # the current image
+
#@ ImagePlus ImP
imp.getProcessor().setThreshold(174, 174, ImageProcessor.NO_LUT_UPDATE)
+
from ijopencv.ij      import ImagePlusMatConverter
IJ.run(imp, "Convert to Mask", "")
+
from ijopencv.opencv  import MatImagePlusConverter
IJ.run(imp, "Watershed", "")
+
from ij              import ImagePlus
</source>
 
  
If you had called <i>show()</i> on the image at any early stage, just update the screen with:
+
# Convert ImagePlus (actually the contained ImageProcessor) to Matrix object
 +
imp2mat = ImagePlusMatConverter()
 +
ImMat = imp2mat.toMat(imp.getProcessor())
 +
print ImMat
  
<source lang="python">
+
# Convert Matrix object to ImageProcessor
imp.updateAndDraw()
+
mat2ip = MatImagePlusConverter()
 +
NewIP  = mat2ip.toImageProcessor(ImMat)
 +
NewImp = ImagePlus("Matrix converted back to ImagePlus", NewIP)
 +
print NewImP
 
</source>
 
</source>
  
 +
Such kind of converter is also available for PointRoi to opencv keypoints...
  
==== ... and counting particles, and measuring their areas ====
+
Now to use opencv function, we use the [http://bytedeco.org/javacpp-presets/opencv/apidocs/ JavaCPP API] that contains almost all functions of opencv.
 
+
Continuing from the <i>imp</i> above, that contains the now watersheded "blobs" sample image:
 
 
 
 
<source lang="python">
 
<source lang="python">
# Create a table to store the results
+
from org.bytedeco.javacpp.opencv_core import Mat, CvMat, vconcat
table = ResultsTable()
 
# Create a hidden ROI manager, to store a ROI for each blob or cell
 
roim = RoiManager(True)
 
# Create a ParticleAnalyzer, with arguments:
 
# 1. options (could be SHOW_ROI_MASKS, SHOW_OUTLINES, SHOW_MASKS, SHOW_NONE, ADD_TO_MANAGER, and others; combined with bitwise-or)
 
# 2. measurement options (see [http://rsb.info.nih.gov/ij/developer/api/ij/measure/Measurements.html Measurements])
 
# 3. a ResultsTable to store the measurements
 
# 4. The minimum size of a particle to consider for measurement
 
# 5. The maximum size (idem)
 
# 6. The minimum circularity of a particle
 
# 7. The maximum circularity
 
pa = ParticleAnalyzer(ParticleAnalyzer.ADD_TO_MANAGER, Measurements.AREA, table, 0, Double.POSITIVE_INFINITY, 0.0, 1.0)
 
pa.setHideOutputImage(True)
 
 
 
if pa.analyze(imp):
 
  print "All ok"
 
else:
 
  print "There was a problem in analyzing", blobs
 
 
 
# The measured areas are listed in the first column of the results table, as a float array:
 
areas = table.getColumn(0)
 
  
</source>
+
## Typical matrices ##
  
To print out the area measurement of each:
+
# Identity Matrix of size (3x3) and type 8-bit
 +
Id = Mat().eye(3,3,0).asMat()
 +
print Id
 +
print CvMat(Id) # handy to visualise the matrix
  
>>> for area in areas: print area
+
# Matrix of ones (3x3)
76.0
+
One = Mat().ones(3,3,0).asMat()
185.0
 
658.0
 
434.0
 
...
 
  
 +
# Matrix of zeros (3x3)
 +
Zero = Mat().zeros(3,3,0).asMat()
  
Now, we want to measure the intensity of each particle. To do so, we'll retrieve the ROI from the ROIManager, set them one at a time on the original (non-watershed, non-thresholded) image stored in the variable <i>blobs</i>, and measure:
+
# Custom Matrices
 +
# 1D-Matrix can be initialize from a list
 +
# For 2D (or more) we have to concatenate 1D-Matrices
  
<source lang="python">
+
Row1 = Mat([1,2,3,4,5]) # 1D matrix
# Create a new list to store the mean intensity values of each blob:
+
Row2 = Mat([6,7,8,9,10])
means = []
 
  
for roi in RoiManager.getInstance().getRoisAsArray():
+
TwoColumn = Mat()             # initialise output
  blobs.setRoi(roi)
+
vconcat(Col1, Col2, TwoColumn) # output stored in TwoColumn
  stats = blobs.getStatistics(Measurements.MEAN)
+
print CvMat(TwoColumn)
  means.append(stats.mean)
 
 
</source>
 
</source>
  
Finally read out the measured mean intensity value of each blob, along with its area:
+
{{Warning | The <code>org.bytedeco.javacpp.opencv_core.Mat</code> object is different than the <code>org.opencv.core.Mat</code> !! They don't have exactly the same attributes and functions. In Fiji you should always use the objects from <code>org.bytedeco.javacpp</code>.}}
 
 
<source lang="python">
 
for area, mean in zip(areas, means):
 
  print area, mean
 
</source>
 
 
 
6.0 191.47368421052633
 
185.0 179.2864864864865
 
658.0 205.61702127659575
 
434.0 217.32718894009216
 
477.0 212.1425576519916
 
...
 
 
 
=== Creating an image from a text file ===
 
 
 
A data file containing rows with 4 columns:
 
 
 
...
 
399 23 30 10.12
 
400 23 30 12.34
 
...
 
 
 
... where the columns are X, Y, Z and value, for every pixel in the image.
 
We assume we know the width and height of the image.
 
From this sort of data, we create an image, read out all lines and parse the numbers:
 
 
 
<source lang="python">
 
width = 512
 
height = 512
 
stack = ImageStack(width, height)
 
  
file = open("/home/albert/Desktop/data.txt", "r")
 
  
try:
+
Similarly there is some apparent redudancy for the function in the javacpp API.
  fp = FloatProcessor(width, height)
 
  pix = fp.getPixels()
 
  cz = 0
 
  # Add as the first slice:
 
  stack.addSlice(str(cz), fp)
 
  # Iterate over all lines in the text file:
 
  for line in file.readlines():
 
    x, y, z, value = line.split(" ")
 
    x = int(x)
 
    y = int(y)
 
    z = int(z)
 
    value = float(value)
 
    # Advance one slice if the Z changed:
 
    if z != cz:
 
      # Next slice
 
      fp = FloatProcessor(width, height)
 
      pix = fp.getPixels()
 
      stack.addSlice(str(cz), fp)
 
      cz += 1
 
    # Assign the value:
 
    pix[y * width + x] = value
 
  # Prepare and show a new image: 
 
  imp = ImagePlus("parsed", stack)
 
  imp.show()
 
# Ensure closing the file handle even if an error is thrown:
 
finally:
 
  file.close()
 
</source>
 
  
 +
ex : Transform exists in 3 different places :
 +
* <code>org.opencv.core.Core.transform</code>
 +
This one takes <code>org.opencv.core.Mat</code> as input. It is currently challenging to have such object in Fiji.
  
=== Obtain/View histogram and measurements from an image ===
+
* <code>org.bytedeco.javacpp.opencv_core.cvTransform</code>
 +
using <code>CvArr</code> as input, but even if you manage to convert your input as a <code>CvArr</code> it crashes Fiji. Apparently it is a deprecated version.
  
The easiest way is to grab an image and call an ImageJ command to show its histogram:
+
* <code>org.bytedeco.javacpp.opencv_core.transform</code>
 +
That's the one to use ! It takes only <code>org.bytedeco.javacpp.opencv_core.Mat</code> as input, which is the most approriate in Fiji/Jython
  
<source lang="python">
 
imp = IJ.openImage("http://rsb.info.nih.gov/ij/images/blobs.gif")
 
IJ.run(imp, "Histogram", "")
 
</source>
 
  
How ImageJ does it, internally, has to do with the [http://rsb.info.nih.gov/ij/developer/api/ij/process/ImageStatistics.html ImageStatisics] class:
+
==== Scalar ====
 +
In addition to Matrices, opencv allows to use Scalar objects
 +
A scalar is a 4 item element (v0, v1, v2, v3).
 +
If v1=v2=v3=0 then the Scalar is real.
  
 
<source lang="python">
 
<source lang="python">
stats = imp.getStatistics()
+
from org.bytedeco.javacpp.opencv_core import Scalar
print stats.histogram
 
</source>
 
  
array('i',[0, 0, 0, 0, 0, 0, 0, 0, 53, 0, 0, 0, 0, 0, 0, 0, 304,
+
# Real scalar can be initiated with a float parameters
            0, 0, 0, 0, 0, 0, 0, 1209, 0, 0, 0, 0, 0, 0, 0, 3511, 0,
+
Number = Scalar(5.0)
            0, 0, 0, 0, 0, 0, 7731, 0, 0, 0, 0, 0, 0, 0, 10396, 0, 0,
+
Number = Scalar(float(5))
            0, 0, 0, 0, 0, 7456, 0, 0, 0, 0, 0, 0, 0, 3829, 0, 0, 0,
+
print Number
            0, 0, 0, 0, 1992, 0, 0, 0, 0, 0, 0, 0, 1394, 0, 0, 0, 0,
 
            0, 0, 0, 1158, 0, 0, 0, 0, 0, 0, 0, 1022, 0, 0, 0, 0, 0,
 
            0, 0, 984, 0, 0, 0, 0, 0, 0, 0, 902, 0, 0, 0, 0, 0, 0,
 
            0, 840, 0, 0, 0, 0, 0, 0, 0, 830, 0, 0, 0, 0, 0, 0, 0,
 
            926, 0, 0, 0, 0, 0, 0, 0, 835, 0, 0, 0, 0, 0, 0, 0, 901,
 
            0, 0, 0, 0, 0, 0, 0, 1025, 0, 0, 0, 0, 0, 0, 0, 1180, 0,
 
            0, 0, 0, 0, 0, 0, 1209, 0, 0, 0, 0, 0, 0, 0, 1614, 0, 0,
 
            0, 0, 0, 0, 0, 1609, 0, 0, 0, 0, 0, 0, 0, 2220, 0, 0, 0,
 
            0, 0, 0, 0, 2037, 0, 0, 0, 0, 0, 0, 0, 2373, 0, 0, 0, 0,
 
            0, 0, 0, 1568, 0, 0, 0, 0, 0, 0, 0, 1778, 0, 0, 0, 0, 0,
 
            0, 0, 774, 0, 0, 0, 0, 0, 0, 0, 1364, 0, 0, 0, 0, 0, 0, 0])
 
  
 +
# Using an integer as parameter has a different meaning
 +
Empty = Scalar(5) # This initiate an empty Scalar object of size 5
 +
print Empty
  
The histogram, area and mean are computed by default. Other values like the median need to be specified.
+
# Alternatively one can set the other values of the Scalar
 
+
Complex = Scalar(1,2,3,4)
To calculate other parameters, specify them by bitwise-or composition (see flags in [http://rsb.info.nih.gov/ij/developer/api/ij/measure/Measurements.html Measurements]):
+
print Complex
 
 
<source lang="python">
 
stats = imp.getStatistics(Measurements.MEAN | Measurements.MEDIAN | Measurements.AREA)
 
print "mean:", stats.mean, "median:", stats.median, "area:", stats.area
 
 
</source>
 
</source>
  
mean: 103.26857775590551 median: 64.0 area: 65024.
 
  
 +
==== Operations ====
 +
It is possible to perform some operations between matrices, or between Scalar and matrices.
  
If we set a ROI to the image, then we are measuring only for the inside of the ROI. Here we set an oval ROI of radius 25 pixels, centered:
 
 
<source lang="python">
 
<source lang="python">
radius = 25
+
from org.bytedeco.javacpp.opencv_core import Scalar, Mat, subtract
roi = OvalRoi(imp.width/2 - radius, imp.height/2 -radius, radius*2, radius*2)
 
imp.setRoi(roi)
 
stats = imp.getStatistics(Measurements.MEAN | Measurements.MEDIAN | Measurements.AREA)
 
print "mean:", stats.mean, "median:", stats.median, "area:", stats.area
 
</source>
 
  
mean: 104.96356275303644 median: 64.0 area: 1976.0
+
A = Mat([1,2,3,4,5])
 +
B = Mat([1,2,-3,-4,0])
  
 +
Number = Scalar(10.0)
  
 +
## Number - B ( B-Number is also possible)
 +
Expr = subtract(Number,B)
 +
print CvMat(Expr.asMat())
  
To display the histogram window ourselves, we may use the [http://rsb.info.nih.gov/ij/developer/api/ij/gui/HistogramWindow.html HistogramWindow] class:
+
## A - B
 
+
Out = Mat()
<source lang="python">
+
subtract(A,B,Out)
hwin = HistogramWindow(imp)
+
print CvMat(Out)
 
</source>
 
</source>
  
... of which we may grab the image (the plot itself) and save it:
+
== Self written Jython modules for ImageJ ==
  
<source lang="python">
+
In Jython you can write all commands line by line in a single file and execute it. To create a neat program, [https://docs.python.org/2/tutorial/controlflow.html#defining-functions functions] and [https://docs.python.org/2/tutorial/classes.html classes] can be used to structure code. To prevent using copy&past for regularly used functions and classes, [https://docs.python.org/2/tutorial/modules.html modules] are the way to choose. Modules are files that contain functions and classes to import into other files.
plotimage = hwin.getImagePlus()
 
IJ.save(plotimage, "/path/to/our/folder/plot.tif")
 
</source>
 
  
=== Removing bleeding from one channel to another ===
+
To load modules, one has to save them to a directory where Jython will find them. Two lines of code will reveal these directories to you:
  
The technique to use is to divide one channel by the other: the channel to denoise divided by the channel that bled through.
+
<source lang='python'>
 
+
from sys import path
The relatively high-level way to do it is to split the channels and call the ImageCalculator with a "Divide" argument:
+
print(path)
 
 
<source lang="python">
 
# 1 - Obtain an RGB image stack
 
imp = WindowManager.getCurrentImage()
 
if imp.getType() != ImagePlus.COLOR_RGB:
 
  IJ.showMessage("The active image is not RGB!")
 
  raise RuntimeException("The active image is not RGB!")
 
 
 
if 1 == imp.getNSlices():
 
  IJ.showMessage("Not a stack!")
 
  raise RuntimeException("Not a stack!")
 
 
 
# 2 - Prepare stacks to split slices
 
stack = imp.getStack()
 
red_stack = ImageStack(imp.width, imp.height)
 
green_stack = ImageStack(imp.width, imp.height)
 
 
 
# 3 - Iterate all slices -- notice slices are 1<=i<=size
 
for i in range(1, imp.getNSlices()+1):
 
  slice = stack.getProcessor(i)
 
  red_stack.addSlice(str(i), slice.toFloat(0, None))
 
  green_stack.addSlice(str(i), slice.toFloat(1, None))
 
 
 
# 4 - Apply "divide" via ImageCalculator to the red_stack, which is a new 32-bit stack
 
# Don't use the parameters "create" or "float" or "32" in the parameters string
 
# of the calc.calculate call--then the result of the operation would be
 
# in a new stack that opens beyond our control. Without them, results are
 
# applied to the red_stack
 
calc = ImageCalculator()
 
calc.calculate("Divide stack", ImagePlus("red", red_stack), ImagePlus("green", green_stack))
 
 
 
# 5 - Compose a new color stack
 
new_stack = ImageStack(imp.width, imp.height)
 
for i in range(1, imp.getNSlices()+1):
 
  cp = stack.getProcessor(i).duplicate()
 
  cp.setPixels(0, red_stack.getProcessor(i))
 
  new_stack.addSlice(stack.getSliceLabel(i), cp)
 
 
 
# 6 - Show the new image
 
ImagePlus("Normalized " + imp.title, new_stack).show()
 
 
</source>
 
</source>
  
 +
When running this code the result is an output like
  
Alternatively and as an example of direct pixel manipulation, we'll iterate all slices of the image stack, divide the red channel by the green channel, and compose a new stack:
+
<source lang='python'>
 
+
['/home/michael/Software/ImageJ.app/jars/Lib', '/home/michael/Software/ImageJ.app/jars/jython-shaded-2.7.0.jar/Lib', '__classpath__', '__pyclasspath__/']
<source lang="python">
 
# 1 - Obtain an RGB image stack
 
imp = WindowManager.getCurrentImage()
 
if imp.getType() != ImagePlus.COLOR_RGB:
 
  IJ.showMessage("The active image is not RGB!")
 
  raise RuntimeException("The active image is not RGB!")
 
 
 
if 1 == imp.getNSlices():
 
  IJ.showMessage("Not a stack!")
 
  raise RuntimeException("Not a stack!")
 
 
 
stack = imp.getStack()
 
 
 
# 2 - Create a new stack to store the result
 
new_stack = ImageStack(imp.width, imp.height)
 
 
 
# 3 - Iterate all slices -- notice slices are 1<=i<=size
 
for i in range(1, imp.getNSlices()+1):
 
  # Get the slice i
 
  slice = stack.getProcessor(i)
 
  # Get two new FloatProcessor with the green and red channel data in them
 
  red = slice.toFloat(0, None)
 
  green = slice.toFloat(1, None)
 
  pix_red = red.getPixels()
 
  pix_green = green.getPixels()
 
  # Create a new FloatProcessor for the normalized result
 
  new_red = FloatProcessor(imp.width, imp.height)
 
  pix_new_red = new_red.getPixels()
 
  # Iterate and set all normalized pixels
 
  for k in range(len(pix_red)):
 
    if 0 != pix_green[k]:
 
      pix_new_red[k] = pix_red[k] / pix_green[k]
 
  # Create a ColorProcessor that has the normalized red and the same green and blue channels
 
  cp = slice.duplicate()
 
  cp.setPixels(0, new_red)  # at channel 0, the red
 
  # Store the normalized slice in the new stack, copying the same slice label
 
  new_stack.addSlice(stack.getSliceLabel(i), cp)
 
 
 
# 4 - Show the normalized stack
 
new_imp = ImagePlus("Normalized " + imp.title, new_stack)
 
new_imp.show()
 
 
</source>
 
</source>
  
Notice that this second approach is much slower: accessing every pixel from jython has a high cost. If you would like to do very fast pixel-level manipulations, use java or [[Clojure Scripting|Clojure]].
+
This tells us that the folder <code>jars/Lib/</code> inside our ImageJ/Fiji directory is the right place to save modules. As <code>Lib/</code> does not exist by default, we have to create it.
  
=== Subtract the minimal value to an image ===
+
When a module is imported for the first time, Jython will compile it to Java code. If there is a module named <code>myModule.py</code>, Jython will create a file called <code>myModule$py.class</code>. The next time the module is imported, the jython interpreter uses the <code>.class</code> file instead of the <code>.py</code> file, even if this <code>.py</code> file was modified.
  
Which is to say, translate the histogram so that the lowest value is at zero.
+
To force the interpreter to use the last version of the py script there are 2 possibilities :
 +
* Close Fiji, delete the <code>myModule$py.class</code> file and restart Fiji
 +
* Use the following lines of code (found at [http://stackoverflow.com/questions/10531920/jython-import-or-reload-dynamically stackoverflow]) that will force Jython to recompile all modules
  
<source lang="python">
+
<source lang='python'>
# Obtain current image and its pixels
+
# Use this to recompile Jython modules to class files.
imp = IJ.getImage()
+
from sys import modules
pix = imp.getProcessor().convertToFloat().getPixels()
+
modules.clear()
 
+
# Imports of Jython modules are placed below:
# find out the minimal pixel value
+
import myModule
min = reduce(Math.min, pix)
 
 
 
# create a new pixel array with the minimal value subtracted
 
pix2 = map(lambda x: x - min, pix)
 
 
 
ImagePlus("min subtracted", FloatProcessor(imp.width, imp.height, pix2, None)).show()
 
 
</source>
 
</source>
  
Notice we used:
+
=== Adding a custom directory ===
* The <i>reduce</i> function to obtain a single value from a list of values (the pixel array) by applying a function to every pair of consecutive values (in this case, the Math.min).
 
* <i>lambda</i>, which is used to declare an anonymous function that takes one argument.
 
* The <i>map</i> function, which runs a function given as argument to every element of a list (here, every pixel) and returns a new list with all the results.
 
  
=== Extract a specific color channel for a given time frame of a composite image ===
+
If you don't want to use <code>jars/Lib/</code> to save your modules, you have to extend the array <code>sys.path</code>:
  
Suppose you have a 4D multicolor image, and want to obtain a stack of slices corresponding to a specific color channel and time frame.
+
<source lang='python'>
 
 
The [http://pacific.mpi-cbg.de/javadoc/ij/CompositeImage.html CompositeImage] is a stack whose slices are interpreted as belonging to specific color channels, Z slices and time frames. To find out which slice corresponds to what, use the <i>getStackIndex</i> method of the [http://pacific.mpi-cbg.de/javadoc/ij/ImagePlus.html ImagePlus], which translates between color channels, z slices and time frames to the slice index in the underlying [http://pacific.mpi-cbg.de/javadoc/ij/ImageStack.html ImageStack].
 
 
 
<source lang="python">
 
from ij import IJ, ImagePlus, ImageStack
 
 
 
def extractChannel(imp, nChannel, nFrame):
 
""" Extract a stack for a specific color channel and time frame """
 
stack = imp.getImageStack()
 
ch = ImageStack(imp.width, imp.height)
 
for i in range(1, imp.getNSlices() + 1):
 
  index = imp.getStackIndex(nChannel, i, nFrame)
 
  ch.addSlice(str(i), stack.getProcessor(index))
 
return ImagePlus("Channel " + str(nChannel), ch)
 
 
 
imp = IJ.getImage()
 
extractChannel(imp, 1, 1).show()
 
</source>
 
 
 
Notice that color channels, stack slices and time frames are all 1-based. For example, if you have 3 color channels, then these have indices 1, 2, and 3 (not 0, 1 and 2).
 
 
 
 
 
=== Visualize any number of TIFF stacks in a single composite multi-color image stack ===
 
 
 
Suppose you have 100 stacks of <i>Drosophila</i> fly brains, each with different neurons labeled in green. Suppose that you have registered all these confocal stacks. Were you to overlay them, you would see whether the labeled neurons overlap in 3D space or not.
 
 
 
Here is a script to do that. First, it asks for a directory containing any number of TIF image stacks. It assumes all stacks have the same dimensions, and that they are all single channel (i.e. just red, or just green, etc.). Then, it displays a small window with 5 colors: red, green, blue, orange, and gray. Any of the hundreds of stacks in the directory can be assigned to each color channel. So you will see up to 5 stacks at the same time, their colors overlaid.
 
 
 
The stacks are accessed in a virtual way, so even 1000 (one thousand) stacks will be managed just fine in small laptop.
 
 
 
One could easily add more color channels.
 
 
 
The script uses [[Imglib]] scripting to normalize images and generate the color composite. See this [http://www.ini.uzh.ch/~acardona/fiji-tutorial/#s10 imglib scripting tutorial] for in-depth explanations.
 
 
 
<source lang="python">
 
# 2010-12-03 Albert Cardona and Arnim Jenett
 
# At HHMI Janelia Farm, Fiji tutorials class
 
#
 
# Select a directory with multiple image stacks
 
# all of the same dimensions, and show a channel
 
# chooser window to visualize up to 5 of them
 
# in red, green, blue, orange, and gray.
 
#
 
# The stacks are all virtual, opened via LOCI
 
# with BFVirtualStack. The composition of the
 
# RGB ColorProcessor is done with the
 
# script.imglib library.
 
#
 
# Each color channel is shown normalized.
 
# Currently works only with TIF stacks,
 
# and it will interpret them as single-channel.
 
 
 
 
 
from loci.plugins.util import BFVirtualStack
 
from loci.formats import ChannelSeparator
 
from ij.io import DirectoryChooser
 
import os
 
from javax.swing import JScrollPane, JPanel, JComboBox, JLabel, BoxLayout, JFrame
 
from java.awt import Color
 
from java.awt.event import ActionListener
 
from script.imglib.math import Compute, Or, Multiply
 
from script.imglib.algorithm import Normalize
 
from script.imglib.color import Red, Green, Blue, RGBA
 
from mpicbg.imglib.image.display.imagej import ImageJFunctions as IJF
 
 
 
 
 
# Choose a directory with lots of tif stacks
 
dc = DirectoryChooser("Choose directory with stacks")
 
srcDir = dc.getDirectory()
 
 
 
# Open each tif stack as a virtual BFVirtualStack
 
bfvs = []
 
names = []
 
for filename in os.listdir(srcDir):
 
  if filename.endswith(".tif"):
 
    cs = ChannelSeparator()
 
    names.append(srcDir + filename)
 
    cs.setId(srcDir + filename)
 
    bfvs.append( BFVirtualStack(srcDir + filename, cs, False, False, False) )
 
 
 
names.sort()
 
names = ["None"] + names
 
 
 
# Predefine a set of desired color channels
 
colors = ["Red", "Green", "Blue", "Orange", "Gray"]
 
 
 
table = {colors[0] : 1, colors[1] : 0, colors[2] : 0, colors[3] : 0, colors[4] : 0}
 
# 0 is None, 1 is the first
 
 
 
def asImg(color, section):
 
  global bfvs, table
 
  index = table[color]
 
  if 0 == index:
 
    return 0  # is "None" color
 
  return IJF.wrap(ImagePlus("", bfvs[index-1].getProcessor(section)))
 
 
 
def maybeNormalize(fn):
 
  """ Do not normalize if no images are present. """
 
  cursors = []
 
  fn.findCursors(cursors)
 
  if len(cursors) > 0:
 
    return Multiply(Normalize(fn), 255)
 
  return fn
 
 
 
class VS(VirtualStack):
 
  def __init__(self):
 
    self.last = None
 
  def getProcessor(self, i):
 
    """ Channel color composition into a single RGB image, as ColorProcessor """
 
    global table, imp, asImg
 
    red =  asImg(colors[0], i)
 
    green = asImg(colors[1], i)
 
    blue = asImg(colors[2], i)
 
    # Add the orange
 
    orange = asImg(colors[3], i)
 
    red = Or(red, orange) # 'or' the orange
 
    green = Or(green, Multiply(orange, 0.5))  # 'or' half the orange
 
    # Add the gray
 
    gray = asImg(colors[4], i)
 
    red = Or(red, gray)
 
    green =  Or(green, gray)
 
    blue = Or(blue, gray)
 
    # Transform to RGB by normalizing and scaling to 255
 
    red = maybeNormalize(red)
 
    green = maybeNormalize(green)
 
    blue = maybeNormalize(blue)
 
    # Compose
 
    rgb = Compute.inRGBA(RGBA(red, green, blue))
 
    self.last = IJF.displayAsVirtualStack(rgb).getProcessor()
 
    return self.last
 
  def getSize(self):
 
    return bfvs[0].getSize()
 
  def getSliceLabel(self, i):
 
    return str(i)
 
  def getWidth(self):
 
    return self.last.getWidth()
 
  def getHeight(self):
 
    return self.last.getHeight()
 
  def getPixels(self, i):
 
    return self.getProcessor(i).getPixels()
 
  def setPixels(self, pix, i):
 
    pass
 
 
 
 
 
# Create a new image stack
 
from java.io import File
 
ourImp = ImagePlus(File(srcDir).getName(), VS())
 
ourImp.show()
 
 
 
# Create a bunch of panels, one for each color channel
 
all = JPanel()
 
layout = BoxLayout(all, BoxLayout.Y_AXIS)
 
all.setLayout(layout)
 
 
 
# GUI to choose which stacks is shown in which channel
 
class Listener(ActionListener):
 
  def __init__(self, color, choice, imp):
 
    self.color = color
 
    self.choice = choice
 
    self.imp = imp
 
  def actionPerformed(self, event):
 
    global table
 
    table[self.color] = self.choice.getSelectedIndex()
 
    self.imp.updateAndRepaintWindow()
 
 
 
for color in colors:
 
  panel = JPanel()
 
  panel.add(JLabel(color))
 
  choice = JComboBox(names)
 
  choice.setSelectedIndex(table[color])
 
  choice.addActionListener(Listener(color, choice, ourImp))
 
  panel.add(choice)
 
  all.add(panel)
 
 
 
frame = JFrame("Channels")
 
frame.getContentPane().add(all)
 
frame.pack()
 
 
 
frame.setVisible(True)
 
</source>
 
 
 
== Tips and Tricks ==
 
 
 
=== Getting a list of all members in one package ===
 
 
 
You can use the Python function ''dir(<package>)'' to see the contents of a package:
 
 
 
<source lang="python">
 
import ij
 
print dir(ij)
 
</source>
 
 
 
'''Note:''' As of April 26nd, 2010, you need to start Fiji with
 
 
 
fiji -Dpython.cachedir.skip=false --
 
 
 
for ''dir(<package>)'' to work.
 
 
 
=== Specifying the encoding of the source ===
 
 
 
When your source code contains non-ASCII characters (such as umlauts), Jython will complain with a ''SyntaxError: Non-ASCII character in file '<iostream>', but no encoding declared''.
 
 
 
You can fix this issue by putting the line
 
 
 
# -*- coding: iso-8859-15 -*-
 
 
 
as first line into your source code (or if it starts with ''#!/usr/bin/python'', as second line), as suggested [http://docs.python.org/tutorial/interpreter.html#source-code-encoding here]. You might need to replace the string ''iso-8859-15'' by something like ''utf-8'' if your source code is encoded in UTF-8.
 
 
 
=== Changing the default encoding ===
 
 
 
By default, Jython encodes the standard output (and other streams) with the ASCII encoding. Often, this is not what you want. You can change the default encoding like this:
 
 
 
<source lang="python">
 
from org.python.core import codecs
 
codecs.setDefaultEncoding('utf-8')
 
</source>
 
 
 
=== Error handling with try / except / finally ===
 
 
 
See complete documentation at: [http://jythonpodcast.hostjava.net/jythonbook/chapter6.html jython book chapter 6].
 
 
 
<source lang="python">
 
x = 10
 
y = 0
 
 
 
try:
 
    z = x / y
 
except NameError, e1:
 
    print "A variable is not defined!", e1
 
except ZeroDivisionError, e2:
 
    print "Dividing by zero doesn't make any sense! Error:", e2
 
finally:
 
    print "This line will always print no matter what error occurs."
 
</source>
 
 
 
Which prints:
 
 
 
Dividing by zero doesn't make any sense! Error: integer division or modulo by zero
 
This line will always print no matter what errors occurs
 
 
 
To catch any kind of errors, use <i>sys.exc_info</i>:
 
 
 
<source lang="python">
 
import sys
 
 
 
try:
 
  z = x / z
 
except:
 
  print "Error: ", sys.exc_info()
 
</source>
 
 
 
Which prints:
 
 
 
Error:  (<type 'exceptions.NameError'>, NameError("name 'x' is not defined",), <traceback object at 0x2>)
 
 
 
 
 
To ensure that you see the stack trace, print it to the ImageJ log window instead of stdout (whathever the latter may be):
 
 
 
<source lang="python">
 
    IJ.log(str(sys.exc_info()))
 
</source>
 
 
 
=== Importing other ''.py'' scripts ===
 
 
 
If you want to import other files, you need to ''import'' them. This requires that the files are found in the so-called ''search path'', a list of directories in which Jython looks for the modules (''.py'' files) to import. You can easily extend the search path:
 
 
 
<source lang="python">
 
 
from sys import path
 
from sys import path
 
from java.lang.System import getProperty
 
from java.lang.System import getProperty
 
+
 
# extend the search path by $FIJI_ROOT/bin/
 
# extend the search path by $FIJI_ROOT/bin/
 +
# 'fiji.dir' works for plain ImageJ, too.
 
path.append(getProperty('fiji.dir') + '/bin')
 
path.append(getProperty('fiji.dir') + '/bin')
 
+
# an alternative can be the users home directory
# Now you can import $FIJI_ROOT/bin/compat.py
+
# path.append(getProperty('user.home') + '/JythonModules')
import compat
+
 +
# Now you can import $FIJI_ROOT/bin/myModule.py
 +
import myModule
 
</source>
 
</source>
  
A situation you are likely to encounter is when you have multiple jython scripts in a folder under the fiji plugins folder.
+
The function <code>getProperty()</code> accepts many more strings. A list can be found at [https://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html The Java Tutorials].
  
For example, suppose you have the folder ''my scripts'' under the fiji ''plugins'' folder, in which you have the script ''Filters.py'' with the following filter functions in it:
+
== Self written Jython packages for ImageJ ==
  
<source lang="python">
+
On the way to perfectly organize Jython code, [https://docs.python.org/2/tutorial/modules.html#packages packages] are the next step. A Jython package is a folder that contain a set of modules scripts together with a <code>__init__.py</code> file. This file can be empty. Below is a typical structure for the <code>Imagej.app/jars/Lib</code> folder:
# Script plugins/my scripts/Filters.py
 
from ij import IJ
 
from ij.plugin import Duplicator
 
  
def median(imp, radius):
+
<source>
  """ Apply a median filter to a copy
+
Imagej.app/jars/Lib/
      of the given ImagePlus, and return it. """
+
-- myModule.py
  copy = Duplicator().run(imp)
+
-- myPackage/
  IJ.run(copy, "Median...", "radius=" + str(radius))
+
  -- __init__.py
  return copy
+
  -- mathTools.py
 
+
  -- customFilters.py
def removeOutliers(imp, radius, threshold, bright):
+
  -- fftTools.py
  """ Apply a remove outliers filter to a copy
+
-- myPackage2/
      of the given ImagePlus, and return it. """
+
  -- __init__.py
  copy = Duplicator().run(imp)
+
  -- mathTools.py
  which = "Bright" if bright else "Dark"
+
  -- stackProcessing.py
  IJ.run(copy, "Remove Outliers...", "radius=" + str(radius) \
 
      + " threshold=" + str(threshold) + " which=" + which)
 
  return copy
 
 
</source>
 
</source>
  
And now you have a second script in which you want to use a function from the ''Filters.py'' script:
+
There are two packages and one module. The first package contains three modules and the second package contains two modules. We can import the modules on different ways:
  
<source lang="python">
+
<source lang='python'>
from ij import IJ
+
# Import the single module using the default name:
import sys
+
import myModule
from java.lang.System import getProperty
 
sys.path.append(getProperty("fiji.dir") + "/plugins/my scripts")
 
from Filters import median
 
  
imp = IJ.getImage()
+
# Import mathTools from the first package
medianFiltered = median(imp, 5.0)
+
import myPackage.mathTools
medianFiltered.show()
+
# Use a function from the imported module
</source>
+
myPackage.mathTools.aFunction()
  
[[Category:Scripting]]
+
# Import mathTools from the second package
[[Category:Jython]]
+
from myPackage2 import mathTools
 +
# Use a function from the imported module without prefixing the package
 +
mathTools.aFunction()
  
== Jython for plugins ==
+
# Import customFilters from the first package and rename it
 +
from myPackage import customFilters as filters
 +
# Use a function from customFilters.py
 +
filters.aFunction()
  
=== Using a jython script as a plugin ===
+
# Importing all module from a package
 
+
from myPackage2 import *
The simplest way is to place the jython script file into fiji/plugins/ folder or a subfolder, and it will appear in the menus after running "'''Plugins>Scripting>Refresh Jython Scripts'''" or "'''Help>Update Menus'''", or on restarting Fiji.
+
# The next line will fail
 
+
stackProcessing.aFunction()
=== Distributing jython scripts in a .jar file ===
 
 
 
PLEASE NOTE: there is no need to do the following -- unless you want to bundle a couple of scripts in one package. See entry above.
 
 
 
The easiest way to distribute a (single) Jython script is to start the [[Script Editor]], open the Jython script and make the bundle with ''File>Export as .jar''.
 
 
 
Alternatively -- or if you want to bundle multiple scripts -- you can do it the manual way:
 
 
 
The whole idea is to be able to distribute an entire collection of scripts in a single .jar file, for best convenience.
 
 
 
In this example, we create two jython scripts that we want to distribute in a .jar file as plugins:
 
 
 
The <i>printer.py</i> script:
 
 
 
<source lang="python">
 
IJ.log("Print this to the log window")
 
 
</source>
 
</source>
  
... and the <i>create_new_image.py</i> script:
+
The reason for the last import to fail is the empty <code>__init__.py</code>. We have to define which modules of the package are imported when using <code>import *</code>. This is done by setting the variable <code>__all__</code> in the <code>__init__.py</code>. For <code>myPackage2</code> this line of code is needed:
  
<source lang="python">
+
<source lang='python'>
ip = ByteProcessor(400, 400)
+
__all__ = ["mathTools", "stackProcessing"]
imp = ImagePlus("New", ip)
 
ip.setRoi(OvalRoi(100, 100, 200, 200))
 
ip.setValue(255)
 
ip.fill(ip.getMask())
 
imp.show()
 
 
</source>
 
</source>
  
Place both scripts under a folder named <i>scripts/</i> .
+
Besides setting this variable, the <code>__init__.py</code> file can contain normal Jython code that is executed upon import of the package.
  
You will need a tiny .java file specifying a launcher PlugIn, such as:
+
== Bundle packages in a JAR file ==
  
<source lang="java">
+
An interesting feature of Jython is to search for packages and modules inside of [https://en.wikipedia.org/wiki/JAR_(file_format) JAR files]. The folder structure from the last section can be modified by packing everything into a single <code>myPackages.jar</code>. The name of the JAR file doesn't matter. All imports work the same as explained before.
package my;
 
import ij.plugin.PlugIn;
 
import Jython.Refresh_Jython_Scripts;
 
  
public class Jython_Launcher implements PlugIn {
+
<source>
    public void run(String arg) {
+
Imagej.app/jars/Lib/
        new Refresh_Jython_Scripts().runScript(getClass().getResourceAsStream(arg));
+
-- myPackages.jar
    }
+
  -- myModule.py
}
+
  -- myPackage/
 +
      -- __init__.py
 +
      -- mathTools.py
 +
      -- customFilters.py
 +
      -- fftTools.py
 +
  -- myPackage2/
 +
      -- __init__.py
 +
      -- mathTools.py
 +
      -- stackProcessing.py
 
</source>
 
</source>
  
Notice we place the above file under directory <i>my/</i>, packaged.
+
The advantage of this approach is that you can share your packages easily. For example you can upload the JAR file to an [[Update_Sites|update site]].  
 
+
It is possible to upload .py scripts to update sites too, without packaging into a jar. The advantage of jar are that they allow to define dependencies more systematically.
To compile it:
 
 
 
$ javac -classpath .:ij.jar:../jars/fiji-scripting.jar:../plugins/Jython_Interpreter.jar my/Jython_Launcher.java
 
 
 
(check that the path to the three jars that you need is correct!)
 
 
 
 
 
Then we define the plugins.config file:
 
 
 
Plugins>My Scripts, "Print to log window", my.Jython_Launcher("/scripts/printer.py")
 
Plugins>My Scripts, "Create image with a white circle", my.Jython_Launcher("/scripts/create_new_image.py")
 
 
 
Finally, we put all files in a .jar file:
 
 
 
$ jar cf my_jython_scripts.jar plugins.config my/Jython_Launcher.class scripts/*py
 
 
 
Then, drop the jar file into fiji/plugins/ folder and run "Help - Update Menus", or restart fiji. Your scripts will appear under Plugins - My Scripts.
 
  
For clarity, this is a summary of the files in the folder:
+
'''NB''' : Script implementing "ImageJ menu macro" and "utilitary scripts" that are used as imported modules in other macros '''should be packed in separate jar files''' ! Indeed, if not explicitly mentioned, the jython interpreter only looks in the Jars/Lib folder to import module, so the .jar containing the "utilitary scripts" should be put there, while the jar containing the "ImageJ menu macro" can be put either in the Plugin or script/Plugin folder in order to appear in the ImageJ menu.  
my/Jython_Launcher.java
 
my/Jython_Launcher.class
 
scripts/printer.py
 
scripts/create_new_image.py
 
plugins.config
 
  
 +
Contrary to the scripts in Jars/Lib, the menu macro scripts are not compiled, and as explained above they can not be imported in other scripts since the Plugin folder do not reside in the Jython search path by default.
 +
     
 +
This is the reason why a given project is rather distributed in 2 different jar files as explained [http://forum.imagej.net/t/using-self-written-jython-modules-in-imagej/2280 here]. 
 +
 
 +
=== Using maven to build packages ===
  
Notice, though, that <b>you don't need to do the .jar packaging at all</b>. Just place the python scripts directly under fiji/plugins/My Scripts/ and they will appear in the menus as regular plugins.
+
Using maven you can automate the packaging of Jython code into JAR files. This approach is only recommended if you already use maven, as installing and learning how to use maven is not worth the time saving of automated packaging.
  
= Jython examples in Fiji =
+
At GitHub you will find an [https://github.com/m-entrup/imagej-jython-package example project] that you can use as a template. Just run <code>mvn package</code> and maven will generate a JAR file at the <code>target</code> directory.
  
*[http://pacific.mpi-cbg.de/cgi-bin/gitweb.cgi?p=fiji.git;a=blob;f=plugins/Examples/Find_Dimension_of_Raw_Image.py;hb=HEAD Find Dimension of Raw Image]
+
== Links ==
*[http://pacific.mpi-cbg.de/cgi-bin/gitweb.cgi?p=fiji.git;a=blob;f=plugins/Examples/Edit_LUT_As_Text.py;hb=HEAD Edit LUT As Text]
 
*[http://pacific.mpi-cbg.de/cgi-bin/gitweb.cgi?p=fiji.git;a=blob;f=plugins/Examples/Delayed_Snapshot.py;hb=HEAD Delayed Snapshot]
 
*[http://pacific.mpi-cbg.de/cgi-bin/gitweb.cgi?p=fiji.git;a=blob;f=plugins/Examples/Command_Launchers/Command_Launcher_Python.py;hb=HEAD Command Launcher GUI]
 
*[http://pacific.mpi-cbg.de/cgi-bin/gitweb.cgi?p=fiji.git;a=blob;f=plugins/Examples/list_all_threads.py;hb=HEAD List all threads]
 
*[http://pacific.mpi-cbg.de/cgi-bin/gitweb.cgi?p=fiji.git;a=blob;f=plugins/Examples/chess_.py;hb=HEAD Chess]
 
  
*[http://pacific.mpi-cbg.de/cgi-bin/gitweb.cgi?p=fiji.git;a=blob;f=plugins/Examples/TrakEM2_Example_Scripts/extract_stack_under_arealist.py;hb=HEAD Extract stack under AreaList] in TrakEM2.
+
* [[Jython_Scripting_Examples|Jython Scripting Examples]]
*[http://pacific.mpi-cbg.de/cgi-bin/gitweb.cgi?p=fiji.git;a=blob;f=plugins/Examples/TrakEM2_Example_Scripts/T2_set_all_transforms_to_identity.py;hb=HEAD Set all transforms to identity] for TrakEM2 objects.
+
* [[ImageJ2_Python_Scripts|ImageJ2 Python Scripts]]
*[http://pacific.mpi-cbg.de/cgi-bin/gitweb.cgi?p=fiji.git;a=blob;f=plugins/Examples/TrakEM2_Example_Scripts/T2_Select_All.py;hb=HEAD Select All] objects in TrakEM2.
+
* [https://www.ini.uzh.ch/~acardona/fiji-tutorial/ A Fiji Scripting Tutorial by Albert Cardona]
*[http://pacific.mpi-cbg.de/cgi-bin/gitweb.cgi?p=fiji.git;a=blob;f=plugins/Examples/TrakEM2_Example_Scripts/Measure_AreaLists.py;hb=HEAD Measure AreaList] in TrakEM2.
+
* [http://wiki.cmci.info/documents/120206pyip_cooking/python_imagej_cookbook Jython scripting cookbook]
  
= See also =
+
== References ==
*Albert Cardona's crash course in [http://www.ini.uzh.ch/~acardona/fiji-tutorial/ Jython scripting with Fiji].
+
<references />
*Jython for [[TrakEM2 Scripting]].
 

Latest revision as of 09:02, 12 June 2019

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
User input
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

Jython is an implementation of the Python programming language designed to run on the Java platform. [1] In ImageJ Jython is one of several supported languages.

When to use Jython

All scripting language supported by ImageJ can be used to access the ImageJ API. There are only differences in how the imports are handled and in the syntax of the selected language. Jython has a syntax that differs from most other language as indentations instead of brackets are used to group code blocks.

The following list will help you to decide if Jython is the right choice to create scripts for ImageJ:

  • If you have experience with Python you can easily use Jython for ImageJ scripting. But you have to keep in mind that tools commonly used in many Python projects (e.g. Numpy) are not available in Jython. By building your own modules you can create complex scripts that otherwise are only possible by writing ImageJ plugins in Java.
  • If don't have any experience in programming, the Python language is a good choice to start with. If your only aim is to write scripts for ImageJ, there are other languages you should try first (e.g. Groovy).
  • In Python many problems can be solved with less code than in other languages. Still the code is easy to read. Have a look at the examples on this page and decide if you want to start using Python for ImageJ scripting.

Explanation

The Java implementation of Python is limited to the standard library of Python 2.
It is not possible to use external python modules (like Numpy...) however any Java class residing in the Fiji installation can be used.

Even with the given limitations, Jython is a powerful language for ImageJ scripting. Hopefully the examples on this page can convince you of that.

Jython basics for ImageJ



Introduction

The aim of this page is not to teach how to program in Python. This purpose is much better fulfilled by the documentation of Python2. The focus of this page is to show how features of the Python language can be useful for ImageJ scripting.

That is why more complex examples are used that are fully functional. Just copy the code to the Script Editor and try them by yourself. Extensive in-line documentation is used to explain the implementation.

Hello World

- With print

There are 2 ways to print some information back to the user.
The first one is a classical python print statement, that will print some information to the console.
print "Hello world"
You can print any kind of variable and objects.
print "This is a string followed by an int", 10

NB1 : If used in a plugin, and no console window is open then the printed information will not be visible to the user (contrary to the log function below)

NB2 : Using numerous print statements might slow down the execution time when used in a plugin (not observed when executing from the script interpreter).


- With IJ.log()

from ij import IJ
IJ.log("Hello world")
IJ.log("This is a string followed by an int " + str(10))

Contrary to the print statement the log function display some output into a log window (newly open if not already open), and accept only a string as argument.

Image selection using the GenericDialog class

This example script will create up to 10 new images and create a GenericDialog to select 3 of them. Finally the names of the selected images are printed to the Log window. It is recommended to copy the code to the Script Editor and run it by yourself.

The following list links to documentation of the used Python features:


src/main/resources/script_templates/Tutorials/Wiki_Jython_Tutorial_1.py

'''Image selection using the GenericDialog class

This code is part of the Jython tutorial at the ImageJ wiki.
http://imagej.net/Jython_Scripting#Image_selection_using_the_GenericDialog_class
'''

# The module __future__ contains some useful functions:
# https://docs.python.org/2/library/__future__.html
from __future__ import with_statement, division
# This imports the function random from the module random.
from random import random
# Next we import Java Classes into Jython.
# This is how we can acces the ImageJ API:
# https://imagej.nih.gov/ij/developer/api/allclasses-noframe.html
from ij import IJ, WindowManager
from ij.gui import GenericDialog

# A function is created with the def keyword.
# This function does not need any parameters.
def create_test_image():
    # Python uses indentation to create code blocks

    # Local variables are assigned.
    # We can assign the same value to more than one variable.
    image_width = image_height = 512
    box_width = box_height = 128
    offset_x = offset_y = 192
    counts = 64
    stdv = 16
    # The build in function int() is used to convert float to int.
    # The variable random contains a function that is called by adding parentheses.
    offset_x = int(2 * random() * offset_x)
    offset_y = int(2 * random() * offset_y)
    # We can define a function inside a function.
    # Outside of create_test_image() this function is not available.
    # By adding an asterisk to a parameter, all given parameters are combined to a tuple.
    def make_title(*to_concat):
        prefix = 'TestImage'
        # To create a tuple with a single entry the comma is necessary.
        # The 2 tuples are concatenated by using the + operator.
        to_join = (prefix,) + to_concat
        # We create a generator that converts every singe entry of the tuple to a string.
        strings_to_join = (str(arg) for arg in to_join)
        # The string ',' has a join method to concatenate values of a tuple with the string as seperator.
        # The result is a string.
        return ','.join(strings_to_join)
    def check_existence(title):
        if WindowManager.getIDList() is None:
            return False
        image_titles = (WindowManager.getImage(id).getTitle() for id in WindowManager.getIDList())
        return title in image_titles
    # To negate an expression put not in front of it.
    if not check_existence(make_title(offset_x, offset_y)):
        # The following code has been created by using the Recorder of ImageJ, set to output Java code.
        # By removing the semicolons, the code can be used in Jython.
        # The parameters can be modified by using variables and string concatenation.
        imp = IJ.createImage(make_title(offset_x, offset_y), "8-bit black", image_width, image_height, 1)
        # The build in function str() is used to convert int to string.
        IJ.run(imp, "Add...", "value=" + str(counts))
        imp.setRoi(offset_x , offset_y, box_width, box_height)
        IJ.run(imp, "Add...", "value=" + str(counts))
        IJ.run(imp, "Select None", "")
        IJ.run(imp, "Add Specified Noise...", "standard=" + str(stdv));
        # We don't want to confirm when closing one of the newly created images.
        imp.changes = False
        imp.show()

# This function uses parameters.
# A default value is given to the third parameter.
def create_selection_dialog(image_titles, defaults, title='Select images for processing'):
    gd = GenericDialog(title)
    # The build in function enumerate() returns two values:
    # The index and the value stored in the tuple/list.
    for index, default in enumerate(defaults):
        # for each loop we add a new choice element to the dialog.
        gd.addChoice('Image_'+ str(index + 1), image_titles, image_titles[default])
    gd.showDialog()
    if gd.wasCanceled():
        return None
    # This function returns a list.
    # _ is used as a placeholder for values we don't use.
    # The for loop is used to call gd.getNextChoiceIndex() len(defaults) times.
    return [gd.getNextChoiceIndex() for _ in defaults]

# It's best practice to create a function that contains the code that is executed when running the script.
# This enables us to stop the script by just calling return.
def run_script():
    while WindowManager.getImageCount() < 10:
        create_test_image()

    image_titles = [WindowManager.getImage(id).getTitle() for id in WindowManager.getIDList()]
    # range(3) will create the list [0, 1, 2].
    selected_indices = create_selection_dialog(image_titles, range(3))
    # The script stops if the dialog has ben canceld (None was returned from create_selection_dialog).
    if selected_indices is None:
        print('Script was canceld.')
        return
    # We have to get the corresponding IMagePlus objects.
    selected_imps = [WindowManager.getImage(id) for id in [WindowManager.getIDList()[index] for index in selected_indices]]
    # The previous line can be split into 2 lines:
    # selected_ids = [WindowManager.getIDList()[index] for index in selected_indices]
    # selected_imps = [WindowManager.getImage(id) for id in selected_ids]

    for imp in selected_imps:
        # Strings can be formated using the % operator:
        # http://www.learnpython.org/en/String_Formatting
        IJ.log('The image \'%s\' has been selected.' % imp.getTitle())

# If a Jython script is run, the variable __name__ contains the string '__main__'.
# If a script is loaded as module, __name__ has a different value.
if __name__ in ['__builtin__','__main__']:
    run_script()

Using Scripting Parameters

The second example is inspired by atomic resolution images recorded with an Transmission Electron Microscope (TEM). Such images show a regular structure (a crystal), but the images are noisy because of the low signal. By using a Fourier filter the contrast can be enhanced.

The script will create a periodic structure and add some random noise. The user can control the parameters of the created image. This is realized using Script parameters. The Fourier filtering has been created by using the Recorder. Finally a simple image calculator is used to show that functions can be passed as parameters.

This list links to the documentation of Python features that are introduced with this example:


src/main/resources/script_templates/Tutorials/Wiki_Jython_Tutorial_2.py

#@ String(value='Please set some parameters.', visibility='MESSAGE') message
#@ Short(label='Image size', value=512, min=128, max=2048, stepSize=128, style="slider") img_size
#@ Double(label='Image amplitude', value=100) amplitude
#@ Short(label='Spacing', value=16, min=8) spacing

'''Using Scripting Parameters

This code is part of the Jython tutorial at the ImageJ wiki.
http://imagej.net/Jython_Scripting#Using_Scripting_Parameters
'''

# The parameters in front of this comment are populated before the script runs.
# Details on Script parameters can be found at
# http://imagej.net/Script_parameters

# The module __future__ contains some useful functions:
# https://docs.python.org/2/library/__future__.html
from __future__ import with_statement, division

# It's best practice to create a function that contains the code that is executed when running the script.
# This enables us to stop the script by just calling return.
def run_script():
    # We can use import inside of code blocks to limit the scope.
    import math
    from ij import IJ, ImagePlus
    from ij.process import FloatProcessor
    blank = IJ.createImage("Blank", "32-bit black", img_size, img_size, 1)
    # This create a list of lists. Each inner list represents a line.
    # pixel_matrix[0] is the first line where y=0.
    pixel_matrix = split_list(blank.getProcessor().getPixels(), wanted_parts=img_size)
    # This swaps x and y coordinates.
    # http://stackoverflow.com/questions/8421337/rotating-a-two-dimensional-array-in-python
    # As zip() creates tuples, we have to convert each one by using list().
    pixel_matrix = [list(x) for x in zip(*pixel_matrix)]
    for y in range(img_size):
        for x in range(img_size):
            # This function oszillates between 0 and 1.
            # The distance of 2 maxima in a row/column is given by spacing.
            val = (0.5 * (math.cos(2*math.pi/spacing*x) + math.sin(2*math.pi/spacing*y)))**2
            # When assigning, we multiply the value by the amplitude.
            pixel_matrix[x][y] = amplitude * val
    # The constructor of FloatProcessor works fine with a 2D Python list.
    crystal = ImagePlus("Crystal", FloatProcessor(pixel_matrix))
    # Crop without selection is used to duplicate an image.
    crystal_with_noise = crystal.crop()
    crystal_with_noise.setTitle("Crystal with noise")
    IJ.run(crystal_with_noise, "Add Specified Noise...", "standard=%d" % int(amplitude/math.sqrt(2)))
    # As this is a demo, we don't want to be ask to save an image on closing it.
    # In Python True and False start with capital letters.
    crystal_with_noise.changes = False
    crystal.show()
    crystal_with_noise.show()
    filtered = fft_filter(crystal_with_noise)
    # We create a lambda function to be used as a parameter of img_calc().
    subtract = lambda values: values[0] - values[1]
    # This is a short form for:
    # def subtract(values):
    #   return values[0] - values[1]

    # The first time we call img_calc with 2 images.
    difference = img_calc(subtract, crystal, filtered, title="Difference of 2")
    difference.show()
    # The first time we call img_calc with 3 images.
    minimum = img_calc(min, crystal, filtered, crystal_with_noise, title="Minimum of 3")
    minimum.show()
    for imp in (crystal, crystal_with_noise, filtered, difference, minimum):
        IJ.run(imp, "Measure", "")

# Functions can be defined after they are used.
# This is only possible if the main code is encapsulated into a function.
# The main function has to be called at the end of the script.

def img_calc(func, *imps, **kwargs):
    """Runs the given function on each pixel of the given list of images.
    An additional parameter, the title of the result, is passed as keyword parameter.
    We assume that each image has the same size. This is not checked by this function.
    """
    # If the keyword parameter is not passed, it is set to a default value.
    if not kwargs['title']:
        kwargs['title'] = "Result"
    # This is a 2D list: list[number of images][pixels per image] .
    pixels = [imp.getProcessor().getPixels() for imp in imps]
    # The function is called pixel by pixel.
    # zip(*pixels) rotates the 2D list: list[pixels per image][mumber of images].
    result = [func(vals) for vals in zip(*pixels)]
    # result is a 1D list and can be used to create an ImagePlus object.
    from ij import ImagePlus
    from ij.process import FloatProcessor
    return ImagePlus(kwargs['title'], FloatProcessor(img_size, img_size, result))

def split_list(alist, wanted_parts=1):
    """Split a list to the given number of parts."""
    length = len(alist)
    # alist[a:b:step] is used to get only a subsection of the list 'alist'.
    # alist[a:b] is the same as [a:b:1].
    # '//' is an integer division.
    # Without 'from __future__ import division' '/' would be an integer division.
    return [ alist[i*length // wanted_parts: (i+1)*length // wanted_parts]
             for i in range(wanted_parts) ]

def fft_filter(imp):
    """ Removing noise from an image by using a FFT filter
    This are operations copied from the ImageJ macro recorder.
    Jython does not complain when you forget to remove the semicolons.
    """
    from ij import IJ
    IJ.run(imp, "FFT", "");
    # No ImagePlus is returned by the FFT function of ImageJ.
    # We need to use the WindowManager to select the newly created image.
    from ij import WindowManager as wm
    fft = wm.getImage("FFT of " + imp.getTitle())
    IJ.run(fft, "Find Maxima...", "noise=64 output=[Point Selection] exclude");
    # Enlarging the point selectins from Find Maxima.
    IJ.run(fft, "Enlarge...", "enlarge=2");
    # Inverting the selection.
    IJ.run(fft, "Make Inverse", "");
    IJ.run(fft, "Macro...", "code=v=0");
    IJ.run(fft, "Inverse FFT", "");
    fft.changes = False
    fft.close()
    imp_filtered = wm.getImage("Inverse FFT of " + imp.getTitle())
    imp_filtered.setTitle("Filtered " + imp.getTitle())
    imp_filtered.changes = False
    return imp_filtered


# If a Jython script is run, the variable __name__ contains the string '__main__'.
# If a script is loaded as module, __name__ has a different value.
if __name__ in ['__builtin__','__main__']:
    run_script()

A batch opener using os.walk()

We have yet introduced some powerful functions build into Python. Another one is walk() from the os module. It can be used to go through a directory structure and process the contained files. In this example walk() is used to batch open images with ImageJ's function openImage().

To read more about the used features, the following list provides links to additional information:


src/main/resources/script_templates/Tutorials/Wiki_Jython_Tutorial_3.py

#@ File(label='Choose a directory', style='directory') import_dir
#@ String(label='File types', value='tif;png') file_types
#@ String(label='Filter', value='') filters
#@ Boolean(label='Recursive search', value=True) do_recursive

'''A batch opener using os.walk()

This code is part of the Jython tutorial at the ImageJ wiki.
http://imagej.net/Jython_Scripting#A_batch_opener_using_os.walk.28.29
'''

# We do only include the module os,
# as we can use os.path.walk()
# to access functions of the submodule.
import os
from java.io import File

from ij import IJ

def batch_open_images(path, file_type=None, name_filter=None, recursive=False):
    '''Open all files in the given folder.
    :param path: The path from were to open the images. String and java.io.File are allowed.
    :param file_type: Only accept files with the given extension (default: None).
    :param name_filter: Only accept files that contain the given string (default: None).
    :param recursive: Process directories recursively (default: False).
    '''
    # Converting a File object to a string.
    if isinstance(path, File):
        path = path.getAbsolutePath()

    def check_type(string):
        '''This function is used to check the file type.
        It is possible to use a single string or a list/tuple of strings as filter.
        This function can access the variables of the surrounding function.
        :param string: The filename to perform the check on.
        '''
        if file_type:
            # The first branch is used if file_type is a list or a tuple.
            if isinstance(file_type, (list, tuple)):
                for file_type_ in file_type:
                    if string.endswith(file_type_):
                        # Exit the function with True.
                        return True
                    else:
                        # Next iteration of the for loop.
                        continue
            # The second branch is used if file_type is a string.
            elif isinstance(file_type, string):
                if string.endswith(file_type):
                    return True
                else:
                    return False
            return False
        # Accept all files if file_type is None.
        else:
            return True

    def check_filter(string):
        '''This function is used to check for a given filter.
        It is possible to use a single string or a list/tuple of strings as filter.
        This function can access the variables of the surrounding function.
        :param string: The filename to perform the filtering on.
        '''
        if name_filter:
            # The first branch is used if name_filter is a list or a tuple.
            if isinstance(name_filter, (list, tuple)):
                for name_filter_ in name_filter:
                    if name_filter_ in string:
                        # Exit the function with True.
                        return True
                    else:
                        # Next iteration of the for loop.
                        continue
            # The second branch is used if name_filter is a string.
            elif isinstance(name_filter, string):
                if name_filter in string:
                    return True
                else:
                    return False
            return False
        else:
        # Accept all files if name_filter is None.
            return True

    # We collect all files to open in a list.
    path_to_images = []
    # Replacing some abbreviations (e.g. $HOME on Linux).
    path = os.path.expanduser(path)
    path = os.path.expandvars(path)
    # If we don't want a recursive search, we can use os.listdir().
    if not recursive:
        for file_name in os.listdir(path):
            full_path = os.path.join(path, file_name)
            if os.path.isfile(full_path):
                if check_type(file_name):
                    if check_filter(file_name):
                        path_to_images.append(full_path)
    # For a recursive search os.walk() is used.
    else:
        # os.walk() is iterable.
        # Each iteration of the for loop processes a different directory.
        # the first return value represents the current directory.
        # The second return value is a list of included directories.
        # The third return value is a list of included files.
        for directory, dir_names, file_names in os.walk(path):
            # We are only interested in files.
            for file_name in file_names:
                # The list contains only the file names.
                # The full path needs to be reconstructed.
                full_path = os.path.join(directory, file_name)
                # Both checks are performed to filter the files.
                if check_type(file_name):
                    if check_filter(file_name):
                        # Add the file to the list of images to open.
                        path_to_images.append(full_path)
    # Create the list that will be returned by this function.
    images = []
    for img_path in path_to_images:
        # IJ.openImage() returns an ImagePlus object or None.
        imp = IJ.openImage(img_path)
        # An object equals True and None equals False.
        if imp:
            images.append(imp)
    return images

def split_string(input_string):
    '''Split a string to a list and strip it
    :param input_string: A string that contains semicolons as separators.
    '''
    string_splitted = input_string.split(';')
    # Remove whitespace at the beginning and end of each string
    strings_striped = [string.strip() for string in string_splitted]
    return strings_striped

if __name__ in ['__builtin__','__main__']:
    # Run the batch_open_images() function using the Scripting Parameters.
    images = batch_open_images(import_dir,
                               split_string(file_types),
                               split_string(filters),
                               do_recursive
                              )
    for image in images:
        # Call the toString() method of each ImagePlus object.
        print(image)

Importing Java module and classes

Another great feature of Jython is the possibility to use functions from Java jar package that resides in the jar folder of imageJ.

ImageJ and Fiji API

The following API documentation lists all available module and functions :

Those package are built-in with Fiji, but any package that resides in the jars folder can be imported provided you know the path to the class.

Let's show one example with the ImageJ package and the class RoiManager. According to the javadoc the RoiManager class resides in ij.plugin.frame. Therefore the code will look like :

from ij.plugin.frame import RoiManager
RM = RoiManager()        # we create an instance of the RoiManager class
rm = RM.getRoiManager()  # "activate" the RoiManager otherwise it can behave strangely 

Using openCV in Jython

It is even possible to use most of opencv functionalities within Jython/Fiji. There are several options (see the wiki page about opencv), yet the most straight forward is probably IJ-OpenCV which is available via the update sites. It will automatically download the necessary packages and dependencies in your Fiji installation.

A manual installation is also possible by putting the jar packages in the jar folder of imageJ. They are avalaible on the IJopenCV github, which even provides a maven option.

Matrices

The first thing to know about OpenCV is that most functions work with an OpenCV matrix object. Fortunately, the IJ-OpenCV project provides some converters :

#@ ImagePlus ImP 
from ijopencv.ij      import ImagePlusMatConverter
from ijopencv.opencv  import MatImagePlusConverter
from ij               import ImagePlus

# Convert ImagePlus (actually the contained ImageProcessor) to Matrix object
imp2mat = ImagePlusMatConverter()
ImMat = imp2mat.toMat(imp.getProcessor())
print ImMat

# Convert Matrix object to ImageProcessor
mat2ip = MatImagePlusConverter()
NewIP  = mat2ip.toImageProcessor(ImMat)
NewImp = ImagePlus("Matrix converted back to ImagePlus", NewIP)
print NewImP

Such kind of converter is also available for PointRoi to opencv keypoints...

Now to use opencv function, we use the JavaCPP API that contains almost all functions of opencv.

from org.bytedeco.javacpp.opencv_core	import Mat, CvMat, vconcat

## Typical matrices ##

# Identity Matrix of size (3x3) and type 8-bit
Id = Mat().eye(3,3,0).asMat()
print Id
print CvMat(Id) # handy to visualise the matrix

# Matrix of ones (3x3) 
One = Mat().ones(3,3,0).asMat()

# Matrix of zeros (3x3) 
Zero = Mat().zeros(3,3,0).asMat()

# Custom Matrices
# 1D-Matrix can be initialize from a list
# For 2D (or more) we have to concatenate 1D-Matrices

Row1 = Mat([1,2,3,4,5]) # 1D matrix 
Row2 = Mat([6,7,8,9,10])

TwoColumn = Mat()              # initialise output
vconcat(Col1, Col2, TwoColumn) # output stored in TwoColumn
print CvMat(TwoColumn)



Similarly there is some apparent redudancy for the function in the javacpp API.

ex : Transform exists in 3 different places :

  • org.opencv.core.Core.transform

This one takes org.opencv.core.Mat as input. It is currently challenging to have such object in Fiji.

  • org.bytedeco.javacpp.opencv_core.cvTransform

using CvArr as input, but even if you manage to convert your input as a CvArr it crashes Fiji. Apparently it is a deprecated version.

  • org.bytedeco.javacpp.opencv_core.transform

That's the one to use ! It takes only org.bytedeco.javacpp.opencv_core.Mat as input, which is the most approriate in Fiji/Jython


Scalar

In addition to Matrices, opencv allows to use Scalar objects A scalar is a 4 item element (v0, v1, v2, v3). If v1=v2=v3=0 then the Scalar is real.

from org.bytedeco.javacpp.opencv_core	import Scalar

# Real scalar can be initiated with a float parameters
Number = Scalar(5.0)
Number = Scalar(float(5))
print Number

# Using an integer as parameter has a different meaning
Empty = Scalar(5) # This initiate an empty Scalar object of size 5
print Empty

# Alternatively one can set the other values of the Scalar
Complex = Scalar(1,2,3,4)
print Complex


Operations

It is possible to perform some operations between matrices, or between Scalar and matrices.

from org.bytedeco.javacpp.opencv_core	import Scalar, Mat, subtract

A = Mat([1,2,3,4,5])
B = Mat([1,2,-3,-4,0])

Number = Scalar(10.0)

## Number - B ( B-Number is also possible)
Expr = subtract(Number,B)
print CvMat(Expr.asMat())

## A - B
Out = Mat() 
subtract(A,B,Out)
print CvMat(Out)

Self written Jython modules for ImageJ

In Jython you can write all commands line by line in a single file and execute it. To create a neat program, functions and classes can be used to structure code. To prevent using copy&past for regularly used functions and classes, modules are the way to choose. Modules are files that contain functions and classes to import into other files.

To load modules, one has to save them to a directory where Jython will find them. Two lines of code will reveal these directories to you:

from sys import path
print(path)

When running this code the result is an output like

['/home/michael/Software/ImageJ.app/jars/Lib', '/home/michael/Software/ImageJ.app/jars/jython-shaded-2.7.0.jar/Lib', '__classpath__', '__pyclasspath__/']

This tells us that the folder jars/Lib/ inside our ImageJ/Fiji directory is the right place to save modules. As Lib/ does not exist by default, we have to create it.

When a module is imported for the first time, Jython will compile it to Java code. If there is a module named myModule.py, Jython will create a file called myModule$py.class. The next time the module is imported, the jython interpreter uses the .class file instead of the .py file, even if this .py file was modified.

To force the interpreter to use the last version of the py script there are 2 possibilities :

  • Close Fiji, delete the myModule$py.class file and restart Fiji
  • Use the following lines of code (found at stackoverflow) that will force Jython to recompile all modules
# Use this to recompile Jython modules to class files.
from sys import modules
modules.clear()
# Imports of Jython modules are placed below:
import myModule

Adding a custom directory

If you don't want to use jars/Lib/ to save your modules, you have to extend the array sys.path:

from sys import path
from java.lang.System import getProperty
 
# extend the search path by $FIJI_ROOT/bin/
# 'fiji.dir' works for plain ImageJ, too.
path.append(getProperty('fiji.dir') + '/bin')
# an alternative can be the users home directory
# path.append(getProperty('user.home') + '/JythonModules')
 
# Now you can import $FIJI_ROOT/bin/myModule.py
import myModule

The function getProperty() accepts many more strings. A list can be found at The Java Tutorials.

Self written Jython packages for ImageJ

On the way to perfectly organize Jython code, packages are the next step. A Jython package is a folder that contain a set of modules scripts together with a __init__.py file. This file can be empty. Below is a typical structure for the Imagej.app/jars/Lib folder:

Imagej.app/jars/Lib/
-- myModule.py
-- myPackage/
   -- __init__.py
   -- mathTools.py
   -- customFilters.py
   -- fftTools.py
-- myPackage2/
   -- __init__.py
   -- mathTools.py
   -- stackProcessing.py

There are two packages and one module. The first package contains three modules and the second package contains two modules. We can import the modules on different ways:

# Import the single module using the default name:
import myModule

# Import mathTools from the first package
import myPackage.mathTools
# Use a function from the imported module
myPackage.mathTools.aFunction()

# Import mathTools from the second package
from myPackage2 import mathTools
# Use a function from the imported module without prefixing the package
mathTools.aFunction()

# Import customFilters from the first package and rename it
from myPackage import customFilters as filters
# Use a function from customFilters.py
filters.aFunction()

# Importing all module from a package
from myPackage2 import *
# The next line will fail
stackProcessing.aFunction()

The reason for the last import to fail is the empty __init__.py. We have to define which modules of the package are imported when using import *. This is done by setting the variable __all__ in the __init__.py. For myPackage2 this line of code is needed:

__all__ = ["mathTools", "stackProcessing"]

Besides setting this variable, the __init__.py file can contain normal Jython code that is executed upon import of the package.

Bundle packages in a JAR file

An interesting feature of Jython is to search for packages and modules inside of JAR files. The folder structure from the last section can be modified by packing everything into a single myPackages.jar. The name of the JAR file doesn't matter. All imports work the same as explained before.

Imagej.app/jars/Lib/
-- myPackages.jar
   -- myModule.py
   -- myPackage/
      -- __init__.py
      -- mathTools.py
      -- customFilters.py
      -- fftTools.py
   -- myPackage2/
      -- __init__.py
      -- mathTools.py
      -- stackProcessing.py

The advantage of this approach is that you can share your packages easily. For example you can upload the JAR file to an update site. It is possible to upload .py scripts to update sites too, without packaging into a jar. The advantage of jar are that they allow to define dependencies more systematically.

NB : Script implementing "ImageJ menu macro" and "utilitary scripts" that are used as imported modules in other macros should be packed in separate jar files ! Indeed, if not explicitly mentioned, the jython interpreter only looks in the Jars/Lib folder to import module, so the .jar containing the "utilitary scripts" should be put there, while the jar containing the "ImageJ menu macro" can be put either in the Plugin or script/Plugin folder in order to appear in the ImageJ menu.

Contrary to the scripts in Jars/Lib, the menu macro scripts are not compiled, and as explained above they can not be imported in other scripts since the Plugin folder do not reside in the Jython search path by default.

This is the reason why a given project is rather distributed in 2 different jar files as explained here.

Using maven to build packages

Using maven you can automate the packaging of Jython code into JAR files. This approach is only recommended if you already use maven, as installing and learning how to use maven is not worth the time saving of automated packaging.

At GitHub you will find an example project that you can use as a template. Just run mvn package and maven will generate a JAR file at the target directory.

Links

References

  1. Wikipedia entry on Jython. Accessed: 2016-08-30