Attempting to run a jython script headless, and getting stuck on img.show()

Hello all,

I have been struggling with this issue for a a few days now, and am trying to find a workaround.

I have a Jython script that I would like to run headless, but one of the operations is displaying the image using img.show() which appears to break the rules for running headless in Java. Is there another way to save a stack of images that Jython has in memory?

I have seen a few threads about virtual machines that may fix the problem, but I haven’t found a good example of how to set that up.

In the end, I am trying to save a tiff stack of images that has been extracted from a .czi file. I am working on a Windows machine if that makes any difference.
Thanks!

Error code from cmd:

File "C:\Users\User\Desktop\temp_python_script.py", line 251, in <module>
    imp.show()
        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.gui.ImageWindow.<init>(ImageWindow.java:68)
        at ij.gui.StackWindow.<init>(StackWindow.java:28)
        at ij.gui.StackWindow.<init>(StackWindow.java:24)
        at ij.ImagePlus.show(ImagePlus.java:436)
        at ij.ImagePlus.show(ImagePlus.java:412)
        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:498)
java.awt.HeadlessException: java.awt.HeadlessException

        at org.python.core.Py.JavaError(Py.java:552)
        at org.python.core.Py.JavaError(Py.java:543)
        at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:190)
        at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:206)
        at org.python.core.PyObject.__call__(PyObject.java:480)
        at org.python.core.PyObject.__call__(PyObject.java:484)
        at org.python.core.PyMethod.__call__(PyMethod.java:126)
        at org.python.pycode._pyx0.f$0(C:/Users/chapmamg/Desktop/temp_python_script.py:281)
        at org.python.pycode._pyx0.call_function(C:/Users/chapmamg/Desktop/temp_python_script.py)
        at org.python.core.PyTableCode.call(PyTableCode.java:171)
        at org.python.core.PyCode.call(PyCode.java:18)
        at org.python.core.Py.runCode(Py.java:1614)
        at org.python.core.__builtin__.eval(__builtin__.java:497)
        at org.python.core.__builtin__.eval(__builtin__.java:501)
        at org.python.util.PythonInterpreter.eval(PythonInterpreter.java:259)
        at org.python.jsr223.PyScriptEngine.eval(PyScriptEngine.java:57)
        at org.python.jsr223.PyScriptEngine.eval(PyScriptEngine.java:31)
        at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
        at org.scijava.script.ScriptModule.run(ScriptModule.java:160)
        at org.scijava.module.ModuleRunner.run(ModuleRunner.java:168)
        at org.scijava.module.ModuleRunner.call(ModuleRunner.java:127)
        at org.scijava.module.ModuleRunner.call(ModuleRunner.java:66)
        at org.scijava.thread.DefaultThreadService.lambda$wrap$2(DefaultThreadService.java:228)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)
Caused by: 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.gui.ImageWindow.<init>(ImageWindow.java:68)
        at ij.gui.StackWindow.<init>(StackWindow.java:28)
        at ij.gui.StackWindow.<init>(StackWindow.java:24)
        at ij.ImagePlus.show(ImagePlus.java:436)
        at ij.ImagePlus.show(ImagePlus.java:412)
        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:498)
        at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:188)
        ... 24 more

Hi @MGChapman welcome to the forum,
Did you try the ImageJ command IJ.saveAsTiff(ImagePlus, outPath) ?

Use from ij import IJ to import the package :slight_smile:

Thanks a ton for the reply @LThomas, I dismissed IJ.saveAsTiff() initially because there wasn’t an option to save an Image Sequence (I am trying to save a stack), but your response made me realize that I should just isolate the individual images using python and use IJ.saveAsTiff().

Oups sorry I was not aware of that but it seems the one from the FileSaver class can do it

https://imagej.nih.gov/ij/developer/api/ij/io/FileSaver.html#saveAsTiff-java.lang.String-