Changing metadata with python-bioformats (omexmlClass)

Hello everyone! (newbie here)

Environment

macOS Mojave 10.14
Fiji Version 2.0.0-rc-69/1.52p
Scikit image version 0.15.0 - April 2019

Context

Playing around with the python-bioformats library.

  1. I copy-pasted the code for the omexml Class from this github repository into a python file called “omexmlClass.py”, in my working directory called “apeer_ometiff_library_local

  2. The OME-TIFF file I work with, which I renamed from “nucleiTubolin.ome.tiff” to “XYZ_C3_T1.ome.tiff”, has the following OME-TIFF metadata:

<OME UUID="urn:uuid:d84e8214-3486-4b78-bfe9-550b102515c6"
	xmlns="http://www.openmicroscopy.org/Schemas/OME/2016-06"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openmicroscopy.org/Schemas/OME/2016-06 http://www.openmicroscopy.org/Schemas/OME/2016-06/ome.xsd">
	<Image ID="Image:0">
		<Pixels BigEndian="false" DimensionOrder="XYCZT" ID="Pixels:0" Interleaved="false" PhysicalSizeX="1.0" PhysicalSizeXUnit="µm" PhysicalSizeY="1.0" PhysicalSizeYUnit="µm" PhysicalSizeZ="1.0" PhysicalSizeZUnit="µm" SignificantBits="16" SizeC="3" SizeT="1" SizeX="1028" SizeY="870" SizeZ="1" TimeIncrement="0.0" TimeIncrementUnit="s" Type="uint8">
			<Channel ID="Channel:0:0" SamplesPerPixel="1">
				<LightPath/>
				<Channel ID="Channel:0:1" SamplesPerPixel="1">
					<LightPath/>
					<Channel ID="Channel:0:2" SamplesPerPixel="1">
						<LightPath/>
						<TiffData FirstC="0" FirstT="0" FirstZ="0" IFD="0" PlaneCount="1">
							<UUID FileName="nucleiTubolin.ome.tiff">urn:uuid:d84e8214-3486-4b78-bfe9-550b102515c6</UUID>

Problem

I tried changing the metadata, more specifically the Type="uint8" part from the <Pixels> tag, and then saving both the data and metadata into a new file called “test.ome.tiff” like so:

from skimage.external import tifffile
from apeer_ometiff_library_local import omexmlClass
import numpy as np

image = "database/nucleiTubolin.ome.tiff"
output = "output/test.ome.tiff"

with tifffile.TiffFile(image) as tif:
    array = tif.asarray()
    omexml_string = tif[0].image_description.decode("utf-8")

metadata = omexmlClass.OMEXML(omexml_string)
metadata.image(0).Pixels.PixelType = 'uint16'
omexml_string_new = metadata.to_xml(encoding="utf-8")

with tifffile.TiffWriter(output) as tif:
    tif.save(array, photometric='minisblack', description=omexml_string_new, metadata={'axes': 'TZCXY'})

When I check my results by opening both files with Fiji’s Bioformat importer with the “Display OME-XML metadata” option ticked, I still see Type=uint8 in both cases.

Questions

Why is the metadata not showing Type=uint16 for the second file?
Is there any way of changing the metadata when saving to another file ?

My reasoning

I tried changing the dtype of the original file by adding a simple line just before calling the save() function like so:

array = array.astype('uint16')

This time, when opening the new file’s metadata, it did show Type=uint16.

So, is the dtype somehow automatically detected when calling the save() function and the metadata overwritten accordingly ?

Thank you very much in advance for your help !

When Bio-Formats reads in the OME-TIFF it will check the PixelType in the XML metadata and confirm that it matches the PixelType of the TIFF data. If there is a mismatch (which is probably what is happening in your first scenario) then you should a warning printing out as below:
PixelType mismatch: OME=UINT16, TIFF=UINT8

If there is a mismatch it will use the PixelType of the TIFF data and override the XML value.

1 Like