Running plugins/macros from the command line

I have searched the forum and mailing list archive, and have not found an answer to this question, so I apologise if it has been asked before, or has an obvious answer,

I am using Fiji on a Windows system, and I want to automatically perform some processing on an image and save the modified image, all started from the command line. I might want to run in headless mode eventually, but I am happy for the UI to appear during development.

I have written a plugin that does the image processing that I need, but I cannot work out:

  • How to start this plugin from the command line
  • How to pass the input and output file names into the plugin for it to load and save the images
  • How to close ImageJ automatically when it is finished

Because there is not much calculation in my plugin, I think that the image processing could be done in a script rather than a plugin if that makes my goal more achievable.

Any pointers to solving this would be greatly appreciated.

Hi @RichardBytheway

You are probably looking for the Headless mode of Fiji.
Check this, it worked beautifully for me:

3 Likes

Thanks. I had seen that page before, but I think that re-reading it helped send me on to find other resources, and I think I have it working now.

With this macro saved as C:\Fiji.app\macros\Test.ijm:

filelist = getArgument();
file = split(filelist,'#');

open(file[0]);
run("Invert");
saveAs("Tiff", file[1]);
close();
run("Quit");

and this command line:

java -jar c:\Fiji.app\jars\ij-1.50b.jar -ijpath c:\Fiji.app -macro Test D:\In.tif#D:\Out2.tif

It runs the macro, which loads D:\In.tif, inverts it, saves it as D:\Out2.tif, and then closes ImageJ/Fiji.

I think the key was to start Java and point it at the ImageJ jar file, rather than start ImageJ-win64.exe. I will need to edit the macro to run my plugin, or rewrite my plugin as a macro, but I hope that is a lesser problem!

Thanks again.

Hi @RichardBytheway
That’s surprising. I could launch macros and python scripts directly with the ImageJ-win64.exe executable.

Plus it did not launch the GUI, which accelerated things.

What did not work for you?

@tinevez As I described in the first post, I couldn’t get it to pass the argument, and it wouldn’t close, although I just tried

ImageJ-win64.exe -macro Test D:\In.tif#D:\Out3.tif

and it worked perfectly.

I have no idea what I have done differently, but it is behaving as expected now. Aren’t computers wonderful?

2 Likes

They certainly are.

Here is an example of a macro launch with parameters:

./ImageJ-win64.exe --headless --console -macro ./RunBatch.ijm 'folder=../folder1 parameters=a.properties output=../samples/Output'

and in the RunBatch.ijm file, I have the following to capture command parameters:

arg = getArgument()
print("Running batch analysis with arguments:")
print(arg)
run("Batch process", arg )
print("Done.")
eval("script", "System.exit(0);"); 

Tell us if it is any good.

2 Likes

Yes, that helps. It is almost exactly what I have in my test macro, but it confirms that I am doing the right thing.

Is “Batch Process” another macro, or a plugin?

If it is a plugin, how do you get at the arguments within it?

“Batch Process” is actually an ImageJ command. Like “Close All…” etc…
It is meant to call a plugin run() method.

You define the mapping between command and plugins in the plugins.config file of your plugin. For instance, for TrackMate, a line of this file look like:

Plugins>Tracking, "TrackMate", fiji.plugin.trackmate.TrackMatePlugIn_

which defines the command “TrackMate” and associate it to the java PlugOn class
fiji.plugin.trackmate.TrackMatePlugIn_

1 Like

Thanks, that is also good information. I didn’t know about the plugins.config file.

@RichardBytheway For details, have a look at the documentation on the wiki.