Access Image Metadata Using A Macro - a simple question

Hi everyone,

In writing a macro, do I necessarily need to open the image to read the metadata, or can I just load the metadata and read its contents?

I know I can programmatically call Bio-Formats and use define the “view” mode as [Metadata only]. But how I access the Metadata window that is then created and access the info inside it is not clear to me.

Specifically, when loading the metadata (window?) for an image, how do I access the information to retrieve the value of a particular key? For example, in my images for the key “BitsPerPixel” the value is “16”. The getMetadata (“info”) is not helpful as this loads all metadata in the log.

Any suggestions?
Hope you guys can help!

Very best,
R.

Hi @rhjpires,

You don’t necessarily have to open the image planes but the file will have to be parsed in order to retrieve the metadata values.

For some basic Bio-Formats macros you can see https://docs.openmicroscopy.org/bio-formats/5.8.1/users/imagej/index.html#macros-and-plugins

For the full list of macro extensions you can find them in FIJI via:
Plugins > Bio-Formats > Bio-Formats Macro Extensions

Here you find how to open metadata only, but that you already know.

I’ve written a few functions to retrieve data from the text window that results.
The macro has some tests using a .lif file, you might want to change e.g. the FluoCubeName for your own key(s). Import the Bio-Formats metadata, then apply this macro:

run("Bio-Formats", "open=<filename>.lif color_mode=Default display_metadata rois_import=[ROI manager] view=[Metadata only] stack_order=Default");
theTitle="Original Metadata";
tws=getList("window.titles");
for(i=tws.length;i>0;i--){
	thisTitle=tws[i-1];
	foundPos=indexOf(thisTitle,theTitle);
	if(foundPos!=-1){
		originalMetadataWindowName=thisTitle;
	}
}

selectWindow(originalMetadataWindowName);
theText= getInfo("window.contents");

metaDataLines=split(theText,'\n');
  setOption("ExpandableArrays", true);

theSearchArray = newArray(" Series ");//spaces are important!
atArrayIndexes= findIndexes(metaDataLines,theSearchArray);
nSeries=atArrayIndexes.length;

seriesNames=newArray();
for(series = 0;series < nSeries; series++){
	monkey=returnValueByLine(metaDataLines,atArrayIndexes[series]);
	seriesNames[series]=monkey;
}

wavelengths = newArray();
index=0;
for(series = 0; series < nSeries; series++){
	theSearchArray = newArray("FluoCubeName"," Image",seriesNames[series]);//spaces are important!
	atArrayIndexes= findIndexes(metaDataLines,theSearchArray);
	for(j=0;j<atArrayIndexes.length;j++){
		wavelengths[index++]=atArrayIndexes[j];
	}
}
debug
for(i=0;i<wavelengths.length;i++){
	print(returnValueByLine(metaDataLines,wavelengths[i]));
}

function findIndexes(theArray,theKeyArray){
	//return key (integer 0..theArray.length) of theArray that contains (all elements of) theKeyArray
	theIndex = -1;line=0;found=0;nFound=0;
	theIndexArray=newArray();
	while(line  < theArray.length){
		found = 1;
		keyIndex = 0;
		while(found == 1 && keyIndex < theKeyArray.length ){
			haystack= theArray[line];
			needle = theKeyArray[keyIndex];
			if(indexOf(haystack,needle) == -1){
				found=0;
			}
			keyIndex++;			
		}
		if(found){
			theIndexArray[nFound] = line;
			nFound++;
		}
		line++;
	}
	return theIndexArray;//array of line numbers containing all search keys in theKeyArray
}
function returnValueByLine(theArray,lineNo){
	//return the value
	//(array is "filename<space>key(s)<tab>Vaule") or (in main index)
	//(array is "<space>series<space>X<space>name<<tab>Vaule")
	theLine=theArray[lineNo];
	theParts = split(theLine,'\t');
	theValue = theParts[1];
	return theValue;
}

Hi guys,

Many thanks for helping out.
So I’ve dug a bit deeper into the direction of the Bio-Formats Macro Extensions, and I guess I found what I was looking for.

The extension Ext.setId(path) locks on to the file to be processed and Ext.getMetadataValue(key,value) helps to retrieve the string “value” from whatever “key” string present in the Metadata.

I think I can continue n working for the time being.

Thanks a lot.

R.

2 Likes