Rewriting the Jython Scripting wiki page

Tags: #<Tag:0x00007fd540b6de70> #<Tag:0x00007fd540b6dd08> #<Tag:0x00007fd540b6dba0>


As there are some people who are unsatisfied with the Jython Scripting wiki page (including me), I want to rewrite it. Before I start, this post will outline my concept of the new wiki page.

The current wiki page
The page is long and contains a lot of examples. But these examples are not specific to python. All the cool stuff, you can do with python, is missing. It’s code you can easily be ported to JavaScript or Java. My plan is to move all these examples to a new page.

There is some information about some specifics of Jython compared to CPython. I will keep this in a more compact form. Interaction between Jython and ImageJ/Java is important, too. This will still be part of the new wiki page.

The new wiki page
I want to start with a section called When to use Jython Scripting. In my opinion Jython Scripting is for users that already know Python or for beginners that want to learn Python to use it outside of ImageJ, too. All other users I want to redirect to JavaScript and BeanShell, as these languages are supported by the Recorder and both are better integrated into Java.

Next step will be a short introduction to the language. I don’t want to write a tutorial on how to program in general, but rather highlight what is special about python (list comprehension, generators, first class functions) and how to use them in ImageJ.

Another section will be on using modules, where I will describe my solution for this. I will write a step by step guide on how to use the minimal-ij1-plugin to build a Jar-File from a package of Jython modules.

In my opinion it’s best to use minimal examples to explain some tricks. For example create_selection_dialog() from my module can be used to explain default parameters, the enumerate function, list comprehension and how to handle lists as return values. I don’t want to write examples that are more complex than this one.

Where did the old Jython_Scripting page go

I agree that these useful examples could be moved, potentially split up into sub-topics, and we could also reconcile it with other task-specific (as opposed to language-specific) examples from the Scripting Toolbox I once created.

I have to say I really started to like Groovy as yet another alternative. It’s compatible with both the JavaScript and BeanShell recorded by the recorder, but offers some nice language features similar to Python. Maybe we could use your work on the Jython Scripting page to draft a similar one for Groovy Scripting, highlighting just the language-specific features, and directing to a general Scripting Examples page or some such for specific uses cases and tasks.

Fully agreed. Maybe we could add the GitHub extension to our mediawiki installation to be able to embed example code more easily and to be able to assure that the examples work in each current version of Fiji/ImageJ. @ctrueden what do you think about that?
(While we’re at it, I’d also suggest adding the TreeAndMenu extension for visualizing tree-like menu structures etc.)

Thank you very much for your initiative!

GitHub MediaWiki extension

I installed this, and created a Tree and Menu Demo page illustrating its usage.

Installing that one is more involved, so I did not do it yet. But I agree it would be nice.

FWIW, Groovy is currently my favorite scripting language by a large margin. Relatedly: right now, @etarena is working on unifying all the SciJava tutorials at various layers (ImageJ, SCIFIO, ImgLib2, etc.) into a collection of Beaker notebooks, all using Groovy. We chose Groovy partly because it is succinct and Java-like, and partly because of Groovy Grape which makes dependency management from within scripts a breeze. Current work (experimental!) can be seen here.

The problems with JavaScript are: 1) Java 8 ships it as a platform extension, so it doesn’t work in some environments such as MATLAB, or Scala Jupyter Notebooks, without some hackery; 2) The new Nashorn is backwards-incompatible with Rhino in some ways, which has caused us pain when moving to Java 8.

The problem with BeanShell is that it is really in deep maintenance mode now. It was nearly dead for a while, but some new people picked up the maintenance burden, so at least it should get bug-fixes moving forward (it didn’t for many years). But IMHO, Groovy does everything BeanShell does, but better.

Note that ImageJ 1.x’s support for recording these languages is case-specific and hacky. We are planning to make a language-agnostic command history that lets you convert any sequence of past command executions into a script in any SciJava scripting language. This will be straightforward to do but I currently have no bandwidth to do it. Maybe some time in 2017.

ImageJ2 Python Scripts

A post was split to a new topic: Setup Jython scripting in Eclipse


I just picked JavaScript and BeanShell because of the Recorder. Even for me with more than 10 years experience in Java programming it’s not easy to say which scripting language can use the commands the Recorder displays. For a beginner it will be important that the scripting language he uses is selectable in the Recorder.

This sounds great. But until this is finished, adding all compatible scripting languages to the Recorder would be nice. If Groovy uses the same syntax as BeanShell (at least for the recorded commands), this should be an easy task.



That is unlikely to ever happen, because the Recorder is an ImageJ 1.x component, and ImageJ 1.x does not support most of the SciJava scripting languages… nor should it in my opinion, since the way it supports JavaScript and BeanShell is incompatible with the way the SciJava scripting framework does it. (ImageJ 1.x employs a home-grown “download on demand” scheme for script language JARs, which duplicates already-present libraries of ImageJ2.)

It really will be much easier to just recreate the functionality of the Recorder in a new History component which provides the same capabilities and more.


Next week I will start the new wiki page about Jython Scripting.

My plan is to copy the current content to a new page and add a remark that this page was copied and will be stripped-down.
The new page will start with a remark that links to the old contend. As soon as the new page is finished, this remark will be removed. I think that i will need few weeks to finish the new page, as I can only spend a few hours per week to work on it.


Sounds like a good plan, @m-entrup. That way, others here can review the content, and when we have a consensus that it improves upon the old page, we can merge it back to the original. Is that what you had in mind?


I have finished the first part of the new Jython Scripting page. I have tried to keep the Introduction and When to use Jython as short as possible. My main focus was on the first example that contains a lot of in-line comments to explain how it works. I think I have included more Python specific features into this example then old wiki page contains.

There are two more section titles for the content I want to add next. I will wait for some comments on what I have written so far before proceeding.


Hey guys, hi @m-entrup,

nice to see that there is some effort ongoing to make the life easier for newbies. I remember how hard it was to get my first jython scripts running.
When I just looked at the new page, I was just wondering: Wouldn’t it make sense to have the first example as easy as possible? I mean something like a hello world program running in the script editor… I’m afraid that a real newbie, who has never programmed inside ImageJ will just be overwhelmed by 100 lines of code even if they are nicely documented. And we don’t want to scare off the newbies, right? :wink:

Just my two cents…



Hi Robert,

the concept of the new page on Jython Scripting is to show how programming concepts of Python can be used for ImageJ. I don’t want to write a tutorial on how to program. Therefore the page targets people that already know Python.

For beginners the Macro Language page is a good place to start with. There you can learn the basics on how to program a macro for ImageJ. A comparable page for a Scripting language would be nice. As Groovy is poorly documented, one could start there to write a beginners tutorial. @ctrueden would be pleased if someone is willing to do that, as he mentioned that Groovy is his favorite scripting language. Focusing on one language for beginners might be a good idea, as it’s better to have one good tutorial then a lot of bad tutorials. For all other scripting languages the wiki pages should only highlight how language specific features can be used, like I try for Jython.

Currently there is a problem with the wiki’s structure. If you go to the starting page, click LEARN and select Scripting on the next page, it will lead you to this page. A beginner can’t decide with which scripting language he should start.


Hi @m-entrup,

no big deal. I was just suggesting that, because I believe any tutorial on any technology or programming language or whatever should start with an easy example at the very beginning, like here:

This is just to attract people to read further. The motto is “Look how easy it can be,… and now I explain you how complicated things can be accomplished…” Afterwards the gain of complicatedness may be different depending on the target audience…



I fleshed out the list:

It now classifies the options into “recommended” and “other” in a reasonable order, with short explanation about each language, who uses it, why you might want to learn it, etc. Feel free to edit and tweak.

However, I fear this “choice paralysis” will always be a problem, because our community will never decide on one language upon which to standardize. The best we can do is summarize the pros and cons.


@m-entrup I reformatted the two current Jython pages a bit, so the right-hand scripting sidebar is consistent with all the other scripting pages.

A couple of comments regarding the new content:

  1. I noticed your example focuses heavily on the ImageJ 1.x GenericDialog class. I would really rather not focus on this legacy method of harvesting parameters from users. We now have the Script parameters approach is which is a huge step toward transitioning away from non-headless-compatible ImageJ-1.x-specific code, in favor of UI-agnostic SciJava code, which will work in other SciJava tools: KNIME, CellProfiler, OMERO, etc.

    So my thought is: as long as you are spending all this time writing new content, why not write content which promotes current best practices for scripting?

  2. On the wiki, you wrote:

    Just copy the code to the Script Editor and try them by yourself.

    Have you considered the idea of shipping these examples with ImageJ, in the Script Editor’s Templates menu? This has many advantages, including: A) code is in a repository on GitHub; B) we can more easily create CI jobs that validate that the examples still function (especially if you use the @ parameter syntax instead of GenericDialog); C) more convenient for users to browse the examples, and also avoid copy/paste errors.

Thanks again for taking the time to work on this project.


The ImageJ-API is the same for every Scripting language. Creating a basic tutorial for every language would lead to a lot of duplicated content. Maybe we should link the tutorial by Albert Cardona.

I like this new list. :+1:

The first example is based on existing code. I don’t know how to use Script parameters to harvest a varying number of images, that is why I don’t use them for this example. The next 2 examples will use Script parameters.

I will create a pull request, as soon as the wiki page is finished.


We are currently trying to standardize everything in Groovy, in a single repository, as Beaker notebooks. See the work in progress here.

That tutorial is already linked from several places on the wiki:

$ git grep 'acardona/fiji-tutorial/'*[ This page] prepared by Albert Cardona is a link to Albert Cardona's [ Fiji scripting tutorial] using python (jython) script uses [[Imglib]] scripting to normalize images and generate the color composite. See this [ imglib scripting tutorial] for in-depth explanations.*Albert Cardona's crash course in [ Jython scripting with Fiji].* Albert Cardona's comprehensive [ Fiji Jython tutorial] (please note that it is better idea to contribute tutorials to the ImageJ/Fiji wiki directly).

But the primary set of tutorials needs to be part of the central set of resources: on GitHub, the wiki, and inside ImageJ itself—not an external link.

Support in the UI for lists/collections/arrays as parameters is something I have wanted for a long time, but never got around to doing. I think this would address your use case though, no? If you agree, let’s finally file an issue for it in the scijava-common repository.

Great. For ImageJ-specific script templates, you can file it against the imagej-scripting repository.


I have added a second example to Jython Scripting. I decided not to use IJ Ops, as I was not able to find a documentation that will help me to implement a Fourier filter in Python. I make use of some Python features that I was not able to integrate into the first example. And now [Script Parameters] are used.
It’s to bad that callback functions are not available for for scripts. Lately I use them frequently with @Parameters in Java.


The relevant issue to watch is scijava/scijava-common#200. But I will not have time to implement it anytime soon.


I have nearly finished the rewrite of the Jython Scripting wiki page. The last thing I have to do is adding GitHub repositories. The code of my examples will be added to the Script Editor’s Templates and the maven template for creating a JAR-package will get its own repository (similar to the minimal imagej plugin).

At I started to write an tutorial on scripting basics that does not use language specific features. By now only selecting an image and performing an action using is explained.


I have created a pull request that adds the examples from the wiki article to the imagej-scripting repository. I have chosen file names that should indicate their belonging to the wiki article.

To demonstrate the creation of jython packages with maven, I have created a small repository that can be used as a template.

I want to add a fourth example that shows how to use inheritance from Java classes in Jython. Maybe something with multithreading would be nice.