Search code examples
pythonjythonimagej

IJ.close() - Scripting python in ImageJ/FIJI


I'm exceptionally new to python/scripting and I'm having a problem. I'm writing the following in Fiji (shortened version of the script is below...)

from ij import IJ, ImagePlus
from java.lang import Runtime, Runnable

import os

filepaths = []

for folder, subs, files in os.walk('location/of/files/'):
    for filename in files:
        #the next part stops it appending DS files
        if not filename.startswith('.'):
            filepaths.append(os.path.abspath(os.path.join(folder, filename,)))   

for i in filepaths:
    IJ.open(i);
    IJ.close();

Basically I want to open an image, do stuff, and then close the processed image using IJ.close(). However it gives the following error:

AttributeError: type object 'ij.IJ' has no attribute 'close'

Any idea how to get around this?

Thanks!


Solution

  • The IJ class does not have a close() method. You probably want to call the close() method of ImagePlus, which is the class for the image objects themselves.

    Try something like:

    IJ.open(i)
    imp = IJ.getImage()
    imp.getProcessor().setf(100, 100, 3.14159) # or whatever
    IJ.save(imp, "/path/to/myShinyModifiedImage.tif")
    imp.close()
    

    If you need to operate over multiple slices of a multi-plane image, see also the "Loop over slices" template (Templates > Python menu of the Script Editor).

    Note also that Jython does not have trailing semicolons on statements.