How to save as tif file with channel splitted?

I open a slide in QuPath and want to save it as tif images with channel splitted.

I use the following code:

RegionRequest request = RegionRequest.createInstance(imgpath, downsample, xi, yi, wi, hi, z, t)
imp = IJTools.convertToImagePlus(server, request).getImage(), file.getAbsolutePath())

Then I get a single tif file that has all the channels.
Is there a way to use code to save each channel seperately?


I found the java code for ImageJ split channel.
Is there a way to call this method and get a list of imps,then each channel can be saved seperately?

   public static ImagePlus[] split(ImagePlus imp) {
        if (imp.getType()==ImagePlus.COLOR_RGB) {
            ImageStack[] stacks = splitRGB(imp.getStack(), true);
            ImagePlus[] images = new ImagePlus[3];
            images[0] = new ImagePlus("red", stacks[0]);
            images[1] = new ImagePlus("green", stacks[1]);
            images[2] = new ImagePlus("blue", stacks[2]);
            return images;
        int width = imp.getWidth();
        int height = imp.getHeight();
        int channels = imp.getNChannels();
        int slices = imp.getNSlices();
        int frames = imp.getNFrames();
        int bitDepth = imp.getBitDepth();
        int size = slices*frames;
        Vector images = new Vector();
        HyperStackReducer reducer = new HyperStackReducer(imp);
        for (int c=1; c<=channels; c++) {
            ImageStack stack2 = new ImageStack(width, height, size); // create empty stack
            stack2.setPixels(imp.getProcessor().getPixels(), 1); // can't create ImagePlus will null 1st image
            ImagePlus imp2 = new ImagePlus("C"+c+"-"+imp.getTitle(), stack2);
            stack2.setPixels(null, 1);
            imp.setPosition(c, 1, 1);
            imp2.setDimensions(1, slices, frames);
            if (imp.isComposite() && ((CompositeImage)imp).getMode()==IJ.GRAYSCALE)
      , "Grays", "");
            if (imp2.getNDimensions()>3)
        ImagePlus[] array = new ImagePlus[images.size()];
        return (ImagePlus[])images.toArray(array);

If looks like you have all the pieces you need - if you copy the method into your Groovy script you can call it. You don’t usually need to make (m)any changes to Java to make it work as Groovy.

I’d also suggest looking at

You can call methods from ImageJ1 through QuPath directly, but it helps to have IntelliJ (or another IDE) to write the code and get the import statements correct.

I’m not sure how familiar you are with scripting generally, but if you can specify exactly what you want to do and what you’ve tried then it may be possible to help some more.

Is the java method split(ImagePlus imp) is the build-in method in the ImageJ?
If it is, then there is no need to add this method?

I tried calling this method, but error appears:

ERROR: Error at line 542: No such property: split for class: ij.ImagePlus
Possible solutions: slice, lut

ERROR: Script error
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(
    at org.codehaus.groovy.runtime.callsite.GetEffectivePojoPropertySite.getProperty(
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(
    at qupath.lib.gui.scripting.DefaultScriptEditor.executeScript(
    at qupath.lib.gui.scripting.DefaultScriptEditor.executeScript(
    at qupath.lib.gui.scripting.DefaultScriptEditor.executeScript(
    at qupath.lib.gui.scripting.DefaultScriptEditor$
    at java.base/java.util.concurrent.Executors$
    at java.base/
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(
    at java.base/java.util.concurrent.ThreadPoolExecutor$
    at java.base/

It’s built-in to ImageJ, but not part of the ImagePlus class. Seems to be in

So I expect you’d need something like this:

def imps = ij.plugin.ChannelSplitter.split(imp)

Yeah,it worked!
Thank you vey much!