How to access OME Metadata from plugin

I’m trying to read the OME Metadata from a Nikon .nd2 image. Specifically I want to access the channel name, the metadata is in the format below

I’ve tried the code below but I get a null value

ImageProcessorReader r = new ImageProcessorReader();
String field = “Channel”;
Object test = r.getMetadataValue(field);

I’d be grateful for any advice on how to access this data, as I’ve had a look on the forum and the only related post was for Matlab and ImageJ macros which is where I got the idea for the above code.


Hi David, theres a few ways to accomplish this depending on the type of macro you need to use. If you are using the r.getMetadataValue(field) value instead then you will want to use the same String that appears in the Show Info window with the metadata key value pairs. Probably the easiest way to access all of the metadata values from a macro would likely be using the below jython example:

reader = ImageProcessorReader()
omeMeta = MetadataTools.createOMEXMLMetadata()
omeMeta.getChannelName(seriesIndex, channelIndex)

Hello @dgault

Thanks for responding. I’m writing this plugin in Eclipse using Java which is having trouble with the MetadataTools in your code. I can find an import loci.formats.MetadataTools; but it gives an error (unable to resolve). I’m using the latest version of loci and all the other loci imports work so I’m wondering if there is a problem with this particular import which is stopping me from trying your code. I’m totally unfamiliar with jython but it looks very similar to the normal java. Do you think this will work if MetadataTools can be fixed.



The University of Edinburgh is a charitable body, registered in Scotland, with registration number SC005336.

Yeah, the same code should work fine with Java, the Bio-Formats API is also Java so it actually makes things easier. The MetadataTools would indeed be loci.formats.MetadataTools, how exactly are you including the dependencies, are you using Maven or Gradle for example or downloading the jars directly?

You can find some Java examples at, for your particular case this one might be most relevant:

The full documentation is at

I’m downloading the jars directly and putting them into the plugins directory of my ImageJ install. I guess loci.jar must be working on some level as the other loci imports are OK its just MetadataTools. My code is below.



import java.awt.Frame;
import java.awt.Window;
import java.util.ArrayList;
import java.util.Arrays;

import ij.IJ;
import ij.WindowManager;
import ij.gui.GenericDialog;
import ij.plugin.PlugIn;
import ij.text.TextWindow;
import loci.formats.FormatException;
import loci.formats.MetadataTools;
import loci.formats.ReaderWrapper;
import loci.formats.meta.MetadataStore;
import loci.plugins.util.ImageProcessorReader;

public class Nikon_Z_projection_ implements PlugIn{
String imageFilename;
String directoryPath;

System.setProperty("plugins.dir", "C:/Workspace/IJ/plugins");

public void run(String arg) {



public void processDirectory() {
String fts;
String dirstr = IJ.getDirectory("Choose the directory");
File f = new File(dirstr);
ArrayList<String> names = new ArrayList<String>(Arrays.asList(f.list()));
int arraysize = names.size();
//Cycle through the files in the directory
for (int a=0; a<arraysize;a++){
fts = names.get(a); //Get specified item from arraylist
Boolean foundit = fts.contains(".nd2");
String filepath = dirstr + fts;
//Construct string to open file using Bio-Formats
if(foundit == true){
String OpenCommand =(dirstr + fts);"Bio-Formats Importer", "open=" + OpenCommand + " color_mode=Default open_files display_ome-xml rois_import=[ROI manager] split_channels view=Hyperstack stack_order=XYCZT");



public void openDialog(){
String title="Example";
int width=512,height=512;

GenericDialog gd = new GenericDialog("Z Projector");
gd.addStringField("Title: ", imageFilename);
gd.addNumericField("Width: ", width, 0);
gd.addNumericField("Height: ", height, 0);
if (gd.wasCanceled()) return;
title = gd.getNextString();
width = (int)gd.getNextNumber();
height = (int)gd.getNextNumber();
IJ.newImage(title, "8-bit", width, height, 1);


public void getDetails(String fts, String filepath) {
String textWindowName = "OME Metadata - " + fts;
Window frame = null;
frame = WindowManager.getWindow(textWindowName);

ReaderWrapper reader = new ImageProcessorReader();
MetadataStore omeMeta = MetadataTools.createOMEXMLMetadata();
int seriesIndex;
int channelIndex;
omeMeta.getChannelName(seriesIndex, channelIndex);


The University of Edinburgh is a charitable body, registered in Scotland, with registration number SC005336.

All of the Bio-Formats jars will be available in a default Fiji install in the jars/bioformats folder, the MetadataTools in particular will be in the formats-api.jar. You can download the individual components from;O=D if necessary, formats-api, formats-bsd and formats-gpl are likely the 3 you will need.

If you wanted to use Maven for your dependencies there is an example pom in You can see for further details on other methods of including the jars in your Java project.

Thanks, that worked, I put formats-api.jar into my projects libraries in Eclipse. The code I used to extract the channel names is below in case it helps anyone else:

public ArrayList getDetails() throws FormatException, IOException {
int seriesIndex = 0;

	ImageProcessorReader reader = new ImageProcessorReader();
	IMetadata omeMeta = MetadataTools.createOMEXMLMetadata();
	int numChannels = omeMeta.getChannelCount(seriesIndex);
	ArrayList<String> ChannelName = new ArrayList<String>();

	for (int z = 0; z<numChannels;z++){
		String nextName = omeMeta.getChannelName(seriesIndex, z);
	return (ChannelName);

I would like to use Maven for dependencies but that would requite using ImageJ2. Unfortunately I can’t find any web pages giving advice/help on using bioformats to open images as yet within ImageJ2, which currently prevents me using ImageJ2.
Thanks for the help, much appreciated.