Headless mode error in subtracting image mean & adding calibration bar

I am trying to run an ImageJ macro headlessly.
I want the macro to do the following:

  1. Subtract the image mean from image

  2. Add a calibration bar

I ran Fiji headlessly from the console with the following command:
C:\Users\user\Fiji.app\fiji-win64.exe --headless --console --run "testMacro.ijm"

I came across the following errors:

  1. Instead of the image mean, a fixed value of 25 is subtracted

  2. The calibration bar is not added

What is wrong?
I would appreciate any help concerning this matter.

OS: Windows 10, 64-bit
Fiji: ImageJ 1.52p

Below is the macro code:


// open image

// 1st process

getStatistics(area, mean);
print("mean=" + mean)

run("Subtract...", "value=mean"); // mean = 25 in headless

// save image
saveAs("Tiff", "result1.tif");

// 2nd process

run("Calibration Bar...", "location=[Upper Right] fill=White label=Black number=5 decimal=0 font=12 zoom=1 overlay"); // ignored in headless

// save image
saveAs("Tiff", "result2.tif");

image.tif (976.8 KB) testMacro.ijm (408 Bytes)

instead try
run(“Subtract…”, “value=" + mean);
as in your print command.

Thank you! It solved the mean error.
However, the calibration bar is still not added when run in headless mode.

Any solutions? Or is this a bug in headless mode?

Here is the simplified code:


// open image

// add calibration bar
run("Calibration Bar...", "location=[Upper Right] fill=White label=Black number=5 decimal=0 font=12 zoom=1 overlay"); // ignored in headless

// save image
saveAs("Tiff", "result.tif");

Hallo Sh_Ty,
the ‘mean error’ has nothing to do with the ‘bar error’.

I can confirm this.

In the console I get the error message:

C:\Users\USER701\Desktop>C:\\Prog\\Fiji.app\\ImageJ-win64.exe --headless --console --run "Macro1.ijm"
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
[INFO] Overriding BIOP Run Macro...; identifier: command:ch.epfl.biop.macrorunner.B_Run_Macro; jar: file:/C:/Prog/Fiji.app/plugins/BIOP/B_Run_Macro-1.0.0-SNAPSHOT.jar
[INFO] Overriding Get Spine From Circle Rois; identifier: command:Cirlces_Based_Spine; jar: file:/C:/Prog/Fiji.app/plugins/Max_Inscribed_Circles-1.1.0.jar
        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 java.awt.Frame.<init>(Frame.java:385)
        at ij.gui.GUI.createBlankImage(GUI.java:148)
        at ij.plugin.CalibrationBar.addText(CalibrationBar.java:344)
        at ij.plugin.CalibrationBar.drawBarAsOverlay(CalibrationBar.java:240)
        at ij.plugin.CalibrationBar.calculateWidth(CalibrationBar.java:419)
        at ij.plugin.CalibrationBar.updateColorBar(CalibrationBar.java:168)
        at ij.plugin.CalibrationBar.run(CalibrationBar.java:104)
        at ij.IJ.runPlugIn(IJ.java:202)
        at ij.Executer.runCommand(Executer.java:150)
        at ij.Executer.run(Executer.java:68)
        at ij.IJ.run(IJ.java:315)
        at ij.IJ.run(IJ.java:326)
        at ij.macro.Functions.doRun(Functions.java:627)
        at ij.macro.Functions.doFunction(Functions.java:98)
        at ij.macro.Interpreter.doStatement(Interpreter.java:275)
        at ij.macro.Interpreter.doStatements(Interpreter.java:261)
        at ij.macro.Interpreter.run(Interpreter.java:157)
        at ij.macro.Interpreter.run(Interpreter.java:91)
        at ij.macro.Interpreter.run(Interpreter.java:102)
        at ij.plugin.Macro_Runner.runMacro(Macro_Runner.java:161)
        at ij.IJ.runMacro(IJ.java:151)
        at ij.IJ.runMacro(IJ.java:140)
        at net.imagej.legacy.IJ1Helper$3.call(IJ1Helper.java:1108)
        at net.imagej.legacy.IJ1Helper$3.call(IJ1Helper.java:1104)
        at net.imagej.legacy.IJ1Helper.runMacroFriendly(IJ1Helper.java:1055)
        at net.imagej.legacy.IJ1Helper.runMacro(IJ1Helper.java:1104)
        at net.imagej.legacy.plugin.IJ1MacroEngine.eval(IJ1MacroEngine.java:147)
        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)

Thank you, phaub, for your support.

Yes, I am fully aware of that, as just simply trying to add a calibration bar (see testMacro2.ijm) fails in headless mode.

Does the “HeadlessException” in the error message mean that this feature is not implemented in headless mode?

This is a bug that is fixed in the latest ImageJ daily build (1.52v22) and in the ImageJ 1.52v release candidate.


1 Like

Thank you for the info! I’m glad it is fixed.