- Javaclass : can't make it work. However, seems to need work in order to pass data between Java and Python worlds.
- JCC : a little bit clumsy, it particular it needs to add very large options when launching Python.
- JPype : the simplest. A Python module with an easy usage.
JPype installation
It is quite regular:unzip JPype-0.5.4.1.zip
cd JPype-0.5.4.1
sage -python setup.py buildHere a bug usually appears. JPype rely on the sets module, which is neither included in recent Python. There is not a lot of modification. You can download a modified version here. Beware, you must still follow the README to provide the path to your JDK installation. If the compilation is OK (you can send me a mail if not), the module is installed with:
sage -python setup.py installNow we can work inside a Sage notebook (downloadable here).
JPype use
Quite simple:- import the module,
- launch a java virtual machine,
- import one or more Java packages.
startJVM() is the path to the dynamical library of the JVM. To find it, you can do:
locate libjvm.soThe second parameter allows to extend the classpath in order to add libraries, here Image. At this point, all the methods of Java-package
ij are available, directly. We can by example load an image and display it in the notebook:
JPype allows to convert Java objects in Python objects (and vis versa), very easily. Thus a Python object is constructed from a Java one, all the methods available:
<jpype._jclass.ij.process.ByteProcessor object at 0xb7ad52c> <jpype._jclass.ij.process.ByteProcessor object at 0xb7ad52c> |
We can now find Lena edges by calling a ByteProcessor method:
<jpype._jclass.ij.process.ByteProcessor object at 0xb313652c> <jpype._jclass.ij.process.ByteProcessor object at 0xb313652c> |
To display side by side Lena and its edges, i need to get the pixel array of each ImageProcessor, by calling getIntArrray():
<jpype._jarray.int[][] object at 0xb313a20c> <jpype._jarray.int[][] object at 0xb313a20c> |
This object is Python indexable, as a regular array. I can give it directly to the Python functions that display images. Here, the implicit conversion between primitive types is realized by JPype:
|
|