We're Hiring!

Saving directly from ImagePlus

Historical discussions about the Bio-Formats library. Please look for and ask new questions at https://forum.image.sc/tags/bio-formats
Please note:
Historical discussions about the Bio-Formats library. Please look for and ask new questions at https://forum.image.sc/tags/bio-formats

If you are having trouble with image files, there is information about reporting bugs in the Bio-Formats documentation. Please send us the data and let us know what version of Bio-Formats you are using. For issues with your code, please provide a link to a public repository, ideally GitHub.

Saving directly from ImagePlus

Postby lando.wark » 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:

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)
lando.wark
 
Posts: 11
Joined: Fri May 20, 2016 3:23 am

Re: Saving directly from ImagePlus

Postby dgault » Thu Sep 06, 2018 9:22 am

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
User avatar
dgault
Team Member
 
Posts: 208
Joined: Fri Aug 14, 2015 2:56 pm


Return to User Discussion [Legacy]

Who is online

Users browsing this forum: No registered users and 1 guest

cron