Posted by
Albert Cardona on
Apr 10, 2009; 7:46am
URL: http://imagej.273.s1.nabble.com/Fiji-Jython-Interpreter-how-does-it-work-tp3692998p3692999.html
maringa wrote:
> I am familiar with ImageJ, I can write macros and I know the basics of
Java. To make my software more userfriendly I wanted to write plugins
instead, and I am now using Fiji (much more extensive!). I read that
Jython was a very easy language to use, and that Fiji had a Jython
Interpreter. My problem is, that I have no idea how to use it and I
haven't found any tutorials. I tried to write MacroJ in the Interpreter,
didn't work, I tried to write Java in the Interpreter, didn't work. And
how do I save the code, seems like the interpreter just runs and
compiles at once?
>
> I would really appreciate some basic introduction, my main questions:
> - what language can I use?
The language is python. The documentation is that for python 2.5, as
available in the python.org website:
http://www.python.org/doc/2.5.4/In addition to python's modules, classes and built-in functions, you may
access java classes directly. Hence all classes as described in the java
documentation and the ImageJ documentation are valid:
Java standard library documentation:
http://java.sun.com/j2se/1.5.0/docs/api/ImageJ Java classes documentation:
http://rsb.info.nih.gov/ij/developer/api/index.htmlTo see how it compares to Java code, see the scripting comparisons page,
which will give you a reasonable idea:
http://pacific.mpi-cbg.de/wiki/index.php/Scripting_comparisons> - where can I save the code? copy-paste into a textdocument and save
as .java?
The interpreter is meant for dynamic interaction with ImageJ.
Keybindings for the interpreter are listed here:
http://pacific.mpi-cbg.de/wiki/index.php/Scripting_HelpTo save the code, you may do either of:
* Select the text on the interpreter running screen and choose "Copy"
from the popup menu.
* Write jython code directly to a text file, save it as a file with an
underscore in its name and with extension .py, and drop it into fiji
plugins folder or subfolder. Then run "Plugins > Scripting > Refresh
Jython Scripts" and it will appear as new menu item, like any other
plugin.
Read this section in detail:
http://pacific.mpi-cbg.de/wiki/index.php/Jython_Scripting#Workflow_for_creating_Jython_scripts> - tutorials?
There is a brief jython for ImageJ tutorial here:
http://albert.rierol.net/jython_imagej_examples.htmlThe Jython Scripting page also contains some examples:
http://pacific.mpi-cbg.de/wiki/index.php/Jython_ScriptingThe tutorials above assumes you know a bit of python the language. If
you need a deep introduction to python the language, see the python
tutorial:
http://www.python.org/doc/2.5.4/tut/tut.htmlThere are numerous python scripts in the plugins/Examples folder of
fiji. You will identify them by their '.py' file name extension.
Also, the Jython developers have a page with documentation and examples:
http://www.jython.org/Project/... particularly here:
http://www.jython.org/Project/userguide.html#interaction-with-java-packagesand here on accessing properties of an object instance:
http://www.jython.org/Project/userguide.html#javabean-propertiesand here on java arrays:
http://www.jython.org/Project/userguide.html#java-arraysand here on creating your own classes derived from java classes:
http://www.jython.org/Project/userguide.html#subclassing-java-classes-in-jythonand here on accessing databases:
http://www.jython.org/Project/userguide.html#database-connectivity-in-jython> - how does the interpreter work
Details on key bindings are here:
http://pacific.mpi-cbg.de/wiki/index.php/Scripting_HelpBasically: type text at the prompt (bottom text area), then push enter
to execute it.
To recall previously executed text, use the up/down arrows.
To type in multilines, use shift+enter to open a new line within the
prompt text area.
To move up and down multilines, use shift+up arrow and shift+down arrow.
If you mean how does it work internally, it's just a python language
engine written in pure Java, which gets compiled on the fly to bytecode.
But you shouldn't worry about that.
As I said, the interpreter is for dynamic interaction with ImageJ. Here
is an example.
First open an image, then, from the Jython interpreter:
>>> imp = IJ.getImage()
>>> print imp
imp[AuPbSn40.jpg 600x412x1]
>>> print imp.width, imp.height
600 412
>>> radius = 100
>>> roi = OvalRoi( imp.width/2 - radius/2, imp.height/2 -radius/2,
radius, radius )
>>> imp.setRoi(roi)
... and observe how now your image has an oval ROI centered on it.
If something goes wrong, you'll get a printout of an Exception.
The first and second lines of the Exception stack trace are usually the
most useful ones.
> Thanks in advance!
You are welcome.
Albert
--
Albert Cardona
http://albert.rierol.net