Skip to content

Instantly share code, notes, and snippets.

@ctrueden
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ctrueden/26727b41db444b558e77 to your computer and use it in GitHub Desktop.
Save ctrueden/26727b41db444b558e77 to your computer and use it in GitHub Desktop.
Bio-Formats fun: messing with OME-XML, etc.
import ij.ImagePlus;
import java.io.IOException;
import loci.common.xml.XMLTools;
import loci.formats.FormatException;
import loci.formats.ome.OMEXMLMetadata;
import loci.plugins.in.ImagePlusReader;
import loci.plugins.in.ImportProcess;
import loci.plugins.in.ImporterOptions;
import loci.plugins.util.ImageProcessorReader;
import ome.xml.meta.MetadataStore;
import ome.xml.meta.OMEXMLMetadataRoot;
import org.w3c.dom.Element;
public class BioFormatsFun {
public static void main(final String... args) throws IOException,
FormatException
{
final String id = args[0];
// initialize Bio-Formats Importer
final ImporterOptions options = new ImporterOptions();
options.setId(id);
final ImportProcess process = new ImportProcess(options);
if (!process.execute()) throw new IllegalStateException("Process failed");
final ImagePlusReader reader = new ImagePlusReader(process);
final ImagePlus[] imps = reader.openImagePlus();
// extract 0th series sizeX, sizeY from Bio-Formats reader
final ImageProcessorReader r = process.getReader();
r.setSeries(0); // query the 0th series
final int sizeX = r.getSizeX();
final int sizeY = r.getSizeY();
System.out.println("From the reader: " + sizeX + " x " + sizeY);
// extract OME-XML
final MetadataStore store = process.getReader().getMetadataStore();
if (!(store instanceof OMEXMLMetadata)) {
throw new IllegalStateException("Not OME-XML");
}
final OMEXMLMetadata omexmlMeta = (OMEXMLMetadata) store;
final OMEXMLMetadataRoot root = (OMEXMLMetadataRoot) omexmlMeta.getRoot();
final int omeSizeX = root.getImage(0).getPixels().getSizeX().getValue();
final int omeSizeY = root.getImage(0).getPixels().getSizeY().getValue();
System.out.println("From the OME-XML: " + omeSizeX + " x " + omeSizeY);
// let's have the OME-XML as a DOM (untested!)
final Element rootElement = root.asXMLElement(XMLTools.createDocument());
System.out.println("DOM root = " + rootElement);
// let's have all the OME-XML as a string
final String xml = omexmlMeta.dumpXML();
System.out.println("Have some XML: " + xml);
// needed to close open files
if (!options.isVirtual()) process.getReader().close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment