Saving directly from ImagePlus
Posted: Wed Sep 05, 2018 1:32 pm
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:
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)
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)