Page 1 of 1

Saving directly from ImagePlus

PostPosted: Wed Sep 05, 2018 1:32 pm
by lando.wark
I'm writing an ImageJ plugin to take a stack of tiffs from two different fluorescent channels, create the ome metadata and save as an 3D ome.tif. My current version constructs an ImagePlus object first, saves that as an intermediate file in the user's temp directory and uses IFormatReader to read that temporary file when writing the ome.tif. This works well, but it's pretty inelegant.

I'm hoping someone has some code they use to save 3D ImagePlus objects directly as ome.tifs that they'd be willing to share. I've tried the following to write to a byte array and map that to a string:

Code: Select all
BufferedImage rawImage = imp.getBufferedImage();
ByteArrayOutputStream baos=new ByteArrayOutputStream();
try {
   ImageIO.write(rawImage, "tif", baos);
} catch (IOException e) {
   e.printStackTrace();
}
byte[] imageInByte=baos.toByteArray();
String inId = "inbytes.tif";
Location.mapFile(inId, new ByteArrayHandle(imageInByte));


But it gives me the following error in the ImageJ console:

loci.formats.FormatException: Invalid TIFF file: inbytes.tif
at loci.formats.in.MinimalTiffReader.initFile(MinimalTiffReader.java:444)
at loci.formats.in.BaseTiffReader.initFile(BaseTiffReader.java:583)
at loci.formats.FormatReader.setId(FormatReader.java:1397)
at loci.formats.DelegateReader.setId(DelegateReader.java:291)
at loci.formats.ImageReader.setId(ImageReader.java:842)
at com.mycompany.imagej.Scanner_Ome.run(Scanner_Ome.java:184)
at ij.IJ.runUserPlugIn(IJ.java:228)
at ij.IJ.runPlugIn(IJ.java:192)
at ij.Executer.runCommand(Executer.java:137)
at ij.Executer.run(Executer.java:66)
at java.lang.Thread.run(Thread.java:745)
Checking for JAI
Reading movie dimensions
loci.formats.FormatException: Unsupported handle typeloci.common.ByteArrayHandle
at loci.formats.in.TiffJAIReader.initFile(TiffJAIReader.java:127)
at loci.formats.FormatReader.setId(FormatReader.java:1397)
at loci.formats.DelegateReader.setId(DelegateReader.java:300)
at loci.formats.ImageReader.setId(ImageReader.java:842)
at com.mycompany.imagej.Scanner_Ome.run(Scanner_Ome.java:184)
at ij.IJ.runUserPlugIn(IJ.java:228)
at ij.IJ.runPlugIn(IJ.java:192)
at ij.Executer.runCommand(Executer.java:137)
at ij.Executer.run(Executer.java:66)
at java.lang.Thread.run(Thread.java:745)

And the following as a seperate exception:

(Fiji Is Just) ImageJ 2.0.0-rc-68/1.52e; Java 1.8.0_66 [64-bit]; Windows 10 10.0; 196MB of 6044MB (3%)

java.lang.IllegalStateException: FormatReader.getImageCount: Current file should not be null; call setId(String) first
at loci.formats.FormatTools.assertId(FormatTools.java:983)
at loci.formats.FormatReader.getImageCount(FormatReader.java:649)
at loci.formats.ImageReader.getImageCount(ImageReader.java:280)
at com.mycompany.imagej.Scanner_Ome.run(Scanner_Ome.java:200)
at ij.IJ.runUserPlugIn(IJ.java:228)
at ij.IJ.runPlugIn(IJ.java:192)
at ij.Executer.runCommand(Executer.java:137)
at ij.Executer.run(Executer.java:66)
at java.lang.Thread.run(Thread.java:745)

Re: Saving directly from ImagePlus

PostPosted: Thu Sep 06, 2018 9:22 am
by dgault
The best place to check out code for saving ImagePlus as ome.tiff is probably the Bio-Formats exporter. You can view the export code at https://github.com/openmicroscopy/biofo ... .java#L323. There is code prior to this section which is more to do with opening dialog boxes and providing options for splitting planes etc.

The majority of the code in the exporter revolves around the metadata but for the pixel data https://github.com/openmicroscopy/biofo ... .java#L597 gets the processor and then based on its type retrieves the plane data at https://github.com/openmicroscopy/biofo ... .java#L709. The actual saveBytes call is right at the bottom of the method at https://github.com/openmicroscopy/biofo ... .java#L821