Grid/Collection Stitching in headless mode?

I’m trying to use a script to perform tile stitching using the (awesome) “Grid/Collection Stitching” plugin. After figuring out how to run a simple macro headless, I am now having some trouble with this plugin, which raises “java.awt.HeadlessException.”

I’m concerned that the plugin may not be compatible with headless mode, if it always instantiates a UI element. Does anyone know if this is the case?

Thank in advance,
Ben

Here is the script:

    from ij import IJ
    stitchOptions  = "type=[Grid: row-by-row]"
    stitchOptions += " order=[Right & Down                ]"
    stitchOptions += " grid_size_x=2 grid_size_y=2"
    stitchOptions += " tile_overlap=20" 
    stitchOptions += " first_file_index_i=1"
    stitchOptions += " directory=[/Users/bbraun/Box Sync/colony microscopy/10xObj/BSB 384 Experiment-237/Experiment-237-tif]"
    stitchOptions += " file_names=Experiment-237_z01m{i}.tif"
    stitchOptions += " output_textfile_name=TileConfiguration_script.txt"
    stitchOptions += " fusion_method=[Linear Blending]"
    stitchOptions += " regression_threshold=0.30"
    stitchOptions += " max/avg_displacement_threshold=4"
    stitchOptions += " absolute_displacement_threshold=3"
    stitchOptions += " compute_overlap"
    stitchOptions += " ignore_z_stage"
    stitchOptions += " computation_parameters=[Save computation time (but use more RAM)]"
    stitchOptions += " image_output=[Write to disk]"
    stitchOptions += " output_directory=[/Users/bbraun/Box Sync/colony microscopy/10xObj/BSB 384 Expt-257/scripts/]"`

    #print stitchOptions

    IJ.run("Grid/Collection stitching", stitchOptions);

Here are the warnings and errors:

    Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=128m; support was removed in 8.0
    Java HotSpot(TM) 64-Bit Server VM warning: Using incremental CMS is deprecated and will likely be removed in a future release
    Jan 19 16:31:33 PEDS-1CJF6T6-LT ImageJ-macosx[8746] <Warning>: CGSConnectionByID: 0 is not a valid connection ID.
    Jan 19 16:31:33 PEDS-1CJF6T6-LT ImageJ-macosx[8746] <Warning>: CGSConnectionByID: 0 is not a valid connection ID.
    java.awt.HeadlessException
    	at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:204)
    	at java.awt.Window.<init>(Window.java:536)
    	at java.awt.Frame.<init>(Frame.java:420)
    	at ij.plugin.frame.PlugInFrame.<init>(PlugInFrame.java:13)
    	at ij.plugin.frame.Editor.<init>(Editor.java:94)
    	at ij.Executer.openExample(Executer.java:173)
    	at ij.Executer.runCommand(Executer.java:149)
    	at ij.Executer.run(Executer.java:66)
    	at ij.IJ.run(IJ.java:297)
    	at ij.macro.Functions.doRun(Functions.java:601)
    	at ij.macro.Functions.doFunction(Functions.java:96)
    	at ij.macro.Interpreter.doStatement(Interpreter.java:230)
    	at ij.macro.Interpreter.doStatements(Interpreter.java:218)
    	at ij.macro.Interpreter.run(Interpreter.java:115)
    	at ij.macro.Interpreter.run(Interpreter.java:85)
    	at ij.macro.Interpreter.run(Interpreter.java:96)
    	at ij.plugin.Macro_Runner.runMacro(Macro_Runner.java:155)
    	at ij.IJ.runMacro(IJ.java:134)
    	at ij.IJ.runMacro(IJ.java:123)
    	at net.imagej.legacy.IJ1Helper$3.call(IJ1Helper.java:1039)
    	at net.imagej.legacy.IJ1Helper$3.call(IJ1Helper.java:1035)
    	at net.imagej.legacy.IJ1Helper.runMacroFriendly(IJ1Helper.java:986)
    	at net.imagej.legacy.IJ1Helper.runMacro(IJ1Helper.java:1035)
    	at net.imagej.legacy.LegacyCommandline$Run.handle(LegacyCommandline.java:258)
    	at org.scijava.console.DefaultConsoleService.processArgs(DefaultConsoleService.java:93)
    	at net.imagej.legacy.LegacyConsoleService.processArgs(LegacyConsoleService.java:81)
    	at org.scijava.AbstractGateway.launch(AbstractGateway.java:95)
    	at net.imagej.Main.launch(Main.java:62)
    	at net.imagej.Main.main(Main.java:68)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.lang.reflect.Method.invoke(Method.java:497)
    	at net.imagej.launcher.ClassLauncher.launch(ClassLauncher.java:279)
    	at net.imagej.launcher.ClassLauncher.run(ClassLauncher.java:186)
    	at net.imagej.launcher.ClassLauncher.main(ClassLauncher.java:77)

Just solved my own problem after banging my head just right on my desk.

No problems when called via macro. Fails only when called via script. Presumably the function used by the plugin to check if it is in macro mode does not recognize if being run in a script.

Thanks to the authors for making this possible! I might suggest that this tip for running headless be added to the documentation.

1 Like

Great that you found a solution! I still think it should also be possible to call plugins like this one from any script language without getting a HeadlessException


It would be great if you even do that yourself: it’s a wiki, and maintaining the documentation is a community effort. So feel free to register a wiki account (if you haven’t done so yet) and edit the Image Stitching page. :slight_smile:

1 Like

Good idea - I did not know it was a wiki. However, I’m not certain I was not making a more general error with regard to scripting, so I will not do this quite yet. Thanks!