How to install and use Jython to write an ImageJ plugin

by Edoardo "Dado" Marcora (last updated on 2005-10-31)

Table of Contents

  1. Introduction
  2. Step 1: Install ImageJ
  3. Step 2: Install Jython
  4. Step 3: Make ImageJ aware of Jython
  5. Step 4: Write an ImageJ plugin using Jython
  6. Related resources

Introduction

Step 1: Install ImageJ

Step 2: Install Jython

Step 3: Make ImageJ aware of Jython

Step 4: Write an ImageJ plugin using Jython

# import and use ImageJ (Java) classes as if they were Python classes
from ij import IJ
IJ.showMessage("Hello World!")
import ij.plugin.PlugIn;
import org.python.util.PythonInterpreter;
public class Hello_World implements PlugIn {
  public void run(String arg) {
    // create a Python interpreter
    PythonInterpreter py = new PythonInterpreter();
    // execute the Python file containing the source code for this ImageJ plugin
    py.execfile("C:\\ImageJ\\plugins\\Hello_World.py");
  }
}
A similar approach is used to write an ImageJ plugin filter...
# "ip" is the image processor of the current image that is passed in from ImageJ
ip.invert()
import ij.plugin.filter.PlugInFilter;
import ij.ImagePlus;
import ij.process.ImageProcessor;
import org.python.util.PythonInterpreter;
public class Invert_Filter implements PlugInFilter {
  public int setup(String arg, ImagePlus imp) {
    // inform ImageJ that this plugin filter only handles 8 bit grayscale images
    return DOES_8G;
  }
  public void run(ImageProcessor ip) {
    // create a Python interpreter
    PythonInterpreter py = new PythonInterpreter();
    // pass the image processor of the current image to the Python interpreter
    py.set("ip", ip);
    // execute the Python file containing the source code for this ImageJ plugin filter
    py.execfile("C:\\ImageJ\\plugins\\Invert_Filter.py");
  }
}

Related resources

Belorussian translation