Import functions with Jython

Hello,

I struggle with a very basic function of Jython.
How do you know what to import before using a specific function using Jython ?

For exemple :
I want to use “if (selectionType() != 0)” , but I don’t know what to import in order to be able to use selectionType() function.
In a more general way : How do you know what to import in order to use specific imageJ / macro language functions in Jython ?

2nd question : I want to make the user select his saving folder / select his saving folder and giving a name to his file. I did not manage to find an easy solution, as I had using macro language.

Thank you !

Hello Syra,
I think your frustration is common when transitioning from the macro language to Jython.
Here are a few links to help you.

First the ImageJ API documentation.
It contains all the functions and classes that you can call from the scripting languages.
https://imagej.nih.gov/ij/developer/api/overview-summary.html

Then the Jython scripting wiki page should help you to figure out how to use it practically.

To select a directory you can use either the script parameters or the classes GenericDialog or DialogChooser from the ImageJ API.

1 Like

Hi Syra,
@LThomas provided a nice starting point. However, please check out @albertcardona’s Jython tutorials as well https://www.ini.uzh.ch/~acardona/fiji-tutorial/ and another here https://albert.rierol.net/jython_imagej_examples.html

As far as knowing what you need to import, you have access to ImageJ’s Java classes and methods directly from Jython. This doesn’t mean much if you aren’t a Java programmer, but I wrote this rough explanation https://github.com/Macklin-Lab/imagej-microscopy-scripts/blob/master/scripting-with-java-classes.org#reading-images-and-working-with-java-classes last year when I finally figured out how search for and work with the underlying Java classes from python. Hope this helps.

1 Like

Hi @Syra,

The macro language is rather ad-hoc and user-oriented in a way that no scripting language can ever aspire to be.

What you want to do, in jython, would take making explicit a lot of operations that the macro language keeps hidden away, implicit.

For example, see in @LThomas 's tutorial how to request the user to choose a file name for saving in a specific directory. He shows two ways: one rather magical and macro-like, using script parameters (I advice against using these, for they will prevent you from learning the underlying logic), and the explicit way using a DirectoryChooser and OpenDialog classes, as documented here: https://imagej.nih.gov/ij/developer/api/index.html

For basics of how to do the above, see this part of my fiji scripting tutorial in jython:

1 Like

Thank you all !

@LThomas : Thank you, I did use DirectoryChooser. It is a nice tool even if I did not find how to add a message as for exemple “Please, select an input folder.”. But I am searching & I’m sure I will find.

@Nick_George : Yes, A.Cardona provide us a very nice tutorial, and I used it even in this script. Thank you for your link concerning java&jython relationship.

@albertcardona : Thanks a lot ! Your explanations are very useful as always. I also used “for filename in os.listdir(inputfolder):” that enable iteration over multiple pictures in a folder for future people. I learnt the os.listdir() function on your website, and am very grateful for this help.