Python scripting

I have experience with python and have chosen it as my language of choice for scripting within imageJ. However I have struggled to find many code examples on the web. Therefore I hope that this forum will become a good place to come for knowledge and examples.
I am particularly keen to know if anyone has found a workaround to use numpy within imageJ scripts, and to know which python packages people are using within their imageJ scripts.

2 Likes

Our goal is to make tutorials and examples accessible from the Script Editor’s Templates ▷ Tutorials menu, and put accompanying documentation on the wiki as needed. Anything of general interest that arises on this forum can be added there afterward, so it stays easy to find.

Well, Python libraries such as numpy which are backed by native code cannot be used from Jython out of the box.

However, there are a couple of projects to try and/or watch:

  • JyNI. From their site: “JyNI is a compatibility layer with the goal to enable Jython to use native CPython extensions like NumPy or SciPy.” From their site, it looks like it is not yet a turnkey solution (it’s still in alpha), but it aims to be by version 1.0.
  • scripting-cpython. A CPython SciJava script language usable from the Script Editor, created by LeeKamentsky of CellProfiler fame. The code is still early, but it’s got promise.
2 Likes

OK, this is the part where I chime in. Jython can’t access Numpy and Numpy is absolutely fantastic for writing image processing algorithms. I know of three ways to do it, all of them have positive and negative aspects:

  • https://github.com/scijava/scripting-cpython is a scripting engine that uses CellProfiler’s Javabridge (available as pip install javabridge for your Python environment. There is some machinery to start the Python interpreter from Java and after that, you can play around in both Java and Python. See scdemo.py for an example. It worked the last time I tried it, but it needs a champion to maintain it.
  • CellProfiler’s RunImageJ module. This works in the other direction. You can run a CellProfiler pipeline and either call an ImageJ command or write a script to access ImageJ. You can pass images into ImageJ. You can write your algorithm as a CellProfiler module in Python and go back and forth to ImageJ and its libraries as needed.
  • The ImageJ pipeline plugin using the KnimeBridge: https://github.com/CellProfiler/knime-bridge. This plugin runs a CellProfiler pipeline using images from ImageJ as inputs and produces measurements as outputs. You can, again, write your code as a CellProfiler module. This also might need a champion to maintain it - it does a nice job of supplying images to the pipeline as inputs, but it doesn’t allow a user to harvest images from CellProfiler as outputs and it doesn’t allow you to access CellProfiler’s (IMHO excellent) segmentation of images as outputs. It accesses CellProfiler and Python via a TCP connection.

Finally, I am hoping at some point in the future to champion a generalized web interface for imaging algorithms which would allow developers to write in whatever language they wanted, in the context of a web server, and let users use integration frameworks like ImageJ, CellProfiler and Knime to link these up via a common API. Again, this needs a champion.

If I were you, I would start with either the cpython scripting or I would be bold and launch ImageJ from Javabridge using your own solution. Happy Hacking.

5 Likes