Recipe 11.15. Implementing an ImageJ Plug-in in JythonCredit: Ferdinand Jamitzky, Edoardo "Dado" Marcora ProblemYou perform image processing using the excellent free program ImageJ and need to extend it with your own plug-ins, but you want to code those plug-ins in Jython rather than in Java. SolutionJython can do all that Java can, but with Python's elegance and high productivity. For example, here is an ImageJ plug-in that implements a simple image inverter: import ij class Inverter_py(ij.plugin.filter.PlugInFilter): def setup(self, arg, imp): """@sig public int setup(String arg, ij.ImagePlus imp)""" return ij.plugin.filter.PlugInFilter.DOES_8G def run(self,ip): """@sig public void run(ij.process.ImageProcessor ip)""" pixels = ip.getPixels( ) width = ip.getWidth( ) r = ip.getRoi( ) for y in range(r.y, r.y+r.height): for x in range(r.x, r.x+r.width): i = y*width + x pixels[i] = 255-pixels[i] DiscussionTo make this plug-in usable from ImageJ, all you need to do is compile it into a Java bytecode class using the jythonc command with the appropriate command-line option switches. For example, I use IBM's open source Java compiler, jikes, and I have placed it into the C:\ImageJ directory, which also holds the plugins and jre subdirectories. So, in my case, the command line to use is: # jythonc -w C:\ImageJ\plugins\Jython -C C:\ImageJ\jikes -J "-bootclasspath C:\ImageJ\jre\lib\rt.jar -nowarn" If you use Sun's Java SDK, or other Java implementations, you just change the -C argument, which indicates the path of your Java compiler and the -J argument, which specifies the options to pass to your Java compiler. See AlsoImageJ is at http://rsb.info.nih.gov/ij/; Jython is at http://www.jython.org; jikes is at http://www-124.ibm.com/developerworks/oss/jikes/; for more on using Jython with Imagej, http://marcora.caltech.edu/jython_imagej_howto.htm. |