Jython problem with running imageCalculator

Hello, I am trying to run a jython script to subtract the background from a batch of images.
I am pretty new to the jpython and could not figure out how to do a subtraction step. IJ.imageCalculator command does not work. Could you please help me to fix it?

from ij import IJ
from ij.io import FileSaver
import os
from glob import glob


for file in file_list:

	IJ.run("Gaussian Blur...", "sigma=2 scaled")
	IJ.run("Duplicate...", "title={}".format(bg))
	IJ.run("Gaussian Blur...", "sigma=50 scaled")
	IJ.imageCalculator("Subtract create 32-bit stack", orig,bg)

Thank you very much!

When scripting in Jython/Python (or in fact in any scripting language other than the IJ1 macro language), you’re interacting with the Java API directly. In many cases the ij.IJ utility class (that you import with from ij import IJ) offers methods that are similar to the available macro functions, such as e.g. run() and IJ.run().

However, there’s no method called IJ.imageCalculator. See the javadoc for the IJ class here:


If you don’t know which commands are available, I recommend using the macro recorder. The Javascript and/or Beanshell modes are fine (as they’re all just recording the calls to Java API anyways, so they’re the same in Python, apart from the trailing ; the can be left out).

NB: In newer versions of ImageJ1 (you can update the IJ1 component by running Help > Update ImageJ ), you also have a Python mode directly in the recorder, thanks to @Christian_Tischer.

This is the command that gets recorded when using the image calculator:

ic = new ImageCalculator();
imp1 = WindowManager.getImage("Title1");
imp2 = WindowManager.getImage("Title2");
imp3 = ic.run("Add create stack", imp1, imp2);

In order to make it work in Jython, you need to add some imports, and leave away the new keyword when initializing the class:

fro ij import WindowManager
from ij.plugin import ImageCalculator

ic = ImageCalculator()

Thank you for your help! Especially, thank you for pointing to the Python recording option in Macros Recording! It helps a lot!