<< Main Manual

Tutorial #1. Python scripting control of ImageJ/PhysImage

    IMPORTANT NOTE: These usage instructions are most beneficial if one already has a basic familiarity of the Python programming language. In lieu of that knowledge though, we suggest you start with a brief crash-course in basic Python programming found here before you proceed.

To facilitate the integration between raw imaging data, analyzed imaging data, and electrophysiology data, we forked the ImageJ code. This was necessary so that we could provide a central mechanism for "gluing" these types of disparate data together. To accomplish this we integrated the high-level "glue" language, Python, into the ImageJ interface by incorporating the Jython package (a Java implementation of the Python scripting language).

    Note: for clarity, we will continue to refer to the programming language as 'Python' but when referring to the console integrated into        
        PhysImage, we will call it the 'Jython Console' as the window is labelled.

    Python integration provides a central mechanism for controlling nearly all the functionality of PhysImage/ImageJ because of Jython's ability to control Java code alongside the relative simplicity of Python syntax and simpler coding behavior compared to standard Java. This integration also allows us to utilize standard ImageJ plugins, such as Micro-Manager, as well as customized/downloadable plugins, and additionally ImageJ macros. Beyond all these capabilities, the integration provides the opportunities for new and novel behavior that can arise from custom code taking advantage of Jython's ability to mix Java and Python code cooperatively and nearly seamlessly.
    These examples show a couple simple things that can be done to those ends.

    Note: We highly recommend you proceed through these tutorials in order as they build upon one another's new knowledge (but should be autonomously useful if you already have the knowledge of the previous tutorials).

Example #1

Type in the following code in the Jython Console window to generating new images:

>>> width = 125
>>> height = 250
>>> ip = ByteProcessor(width, height)
>>> for i in range(width):
...    for j in range(height):
...       ip.set(i, j, int(Math.sin(0.1*i)*256))
...
>>> image = ImagePlus("NewImage1", ip)
>>> image.show()
>>> ip = ByteProcessor(width, height)
>>> for i in range(width):
...    for j in range(height):
...       ip.set(i, j, int(Math.sin(0.1*i)*Math.cos(0.1*j)*256))
...
>>> image = ImagePlus("NewImage2", ip)
>>> image.show()
>>>


This should display the following two images:
NewImage1NewImage2

Example #2

The next example shows how we can save our scripts in files and run them dynamically within the Jython console:

/Users/john/testScript.py:
for i in range(10):
   print i

IJ.showMessage("Message from inside the script!")

Then to run the script within the Jython console type the following (changing the path to 'testScript.py' as appropriate for your system):

>>> filename = "/Users/john/testScript.py"
>>> IJ.open(filename)

Other important uses of scripts

Most PhysImage graphical operations will result in the corresponding scripting commands being sent to the Jython Console. This is a useful starting point for your custom scripts and can be copied from the console using the "Copy" button.

In general, .py files in the PhysImage/plugins tree will appear as plugins on subsequent starts of PhysImage. One very useful feature of this is that the plugins' implementations can actually be changed without a restart of PhysImage (you just have to restart when creating a new one). Furthermore, .py files in the "base directory" or "processing directory" can be run by going to "File->Open Base Directory Script" and "File->Open Processing Directory Script" and selecting the appropriate .py file and the list of available scripts can be updated by selecting "Refresh list" (including newly added scripts).

>> Tutorial #2. Chart generation of time-series data

<< Main Manual