Using ImageJ in a task type

Core.Host.ImageJ automatically installs ImageJ. This allows integrating ImageJ functionality in XamFlow workflows to improve automation and allow combining it with other tools conveniently.

For example Example.ImageJ.Segmentation uses the ImageJ function Make Binary:

The main script.py contains the boilerplate XamFlow input and output handling, and uses imagejutil.run_imagej_script() to execute an ImageJ script:

import omni
import imagejutil
import xfworkerutil


worker = xfworkerutil.XFWorkerJob()

inputs = worker.job['inputs']
method = inputs['method']

# Download input files
image = worker.get_omni_image(inputs['image'])

seg_omni_path = worker.create_related_path(image.metadata_path, '_seg.omni')
seg_raw_path = worker.create_related_path(image.metadata_path, '_seg.raw')

if image.pixels_compression != omni.Image.PIXELS_COMPRESSION_UNCOMPRESSED:
    worker.error(f"Unsupported image compression: {image.pixels_compression}")

# Run ImageJ script
imagejutil.run_imagej_script("macro.py",
    raw_image_path=image.pixels_path,
    raw_seg_path=seg_raw_path,
    pixel_type=imagejutil.IMAGEJ_PIXEL_TYPES[image.pixel_type],
    sizex=image.size_3d[0],
    sizey=image.size_3d[1],
    sizez=image.size_3d[2],
    offset=image.pixels_file_data_offset,
    method=method)

seg = image.save_derived_segments(seg_omni_path, seg_raw_path)

# Upload output files
seg_output = worker.upload_omni_output(seg)

# Save outputs
outputs = {
    "segments": seg_output,
}
worker.finish(outputs)

The macro.py script contains the ImageJ commands:

#@String raw_image_path
#@String raw_seg_path
#@String pixel_type
#@String sizex
#@String sizey
#@String sizez
#@String offset
#@String method

from ij import IJ

IJ.run("Raw...",
    "open='" + raw_image_path + "'" +
    " image=[" + pixel_type + "]" +
    " width=" + sizex +
    " height=" + sizey +
    " number=" + sizez +
    " offset=" + offset +
    " little-endian");
imp = IJ.getImage()
IJ.run(imp, "Make Binary",
    "method=" + method +
    " background=Default"
    " calculate");
IJ.saveAs(imp, "Raw Data", raw_seg_path);