Skip to content

Instantly share code, notes, and snippets.

@joshmoore
Created January 21, 2020 09:02
Show Gist options
  • Save joshmoore/03e5836199fab91b1a875a677719c3e5 to your computer and use it in GitHub Desktop.
Save joshmoore/03e5836199fab91b1a875a677719c3e5 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""
<Image ID="Image:0" Name="image_0000247585 (R0)">
<AcquisitionDate>2020-01-15T06:29:11.537</AcquisitionDate>
<Description>Collection ImageCollection__0000042467</Description>
<InstrumentRef ID="Instrument:0"/>
<ObjectiveSettings ID="Objective:0:0"/>
<Pixels BigEndian="false" DimensionOrder="XYCZT" ID="Pixels:0" Interleaved="false" PhysicalSizeX="16.438446163366336" PhysicalSizeXUnit="µm" PhysicalSizeY="16.438446015424162" PhysicalSizeYUnit="µm" SignificantBits="8" SizeC="3" SizeT="1" SizeX="1616" SizeY="4668" SizeZ="1" Type="uint8">
<Channel ID="Channel:0:0" IlluminationType="Transmitted" SamplesPerPixel="3">
<LightPath/>
</Channel>
<MetadataOnly/>
<Plane PositionX="0" PositionXUnit="reference frame" PositionY="0" PositionYUnit="reference frame" TheC="0" TheT="0" TheZ="0"/>
</Pixels>
</Image>
"""
import xml.etree.ElementTree as ET
NS = "{http://www.openmicroscopy.org/Schemas/OME/2016-06}"
tree = ET.parse("test.ome.xml")
root = tree.getroot()
images = {}
for img in root.findall(NS + "Image"):
name = img.attrib["Name"]
base, res = name.split(" ")
if base not in images:
images[base] = img
else:
previous = images[base].attrib["Name"].split(" ")[1]
if res < previous:
images[base] = img
for base, img in images.items():
name = img.attrib["Name"]
pixels = img.find(NS + "Pixels")
sizeX = int(pixels.attrib["SizeX"])
sizeY = int(pixels.attrib["SizeY"])
phyX = float(pixels.attrib["PhysicalSizeX"])
phyY = float(pixels.attrib["PhysicalSizeY"])
unitX = pixels.attrib["PhysicalSizeXUnit"]
unitY = pixels.attrib["PhysicalSizeYUnit"]
plane = pixels.find(NS + "Plane")
posX = int(plane.attrib.get("PositionX", ""))//1000000
posY = int(plane.attrib.get("PositionY", ""))//1000000
print(f'{name:20}\t{sizeX:5d}\t{sizeY:5d}\t{phyX:>4.2f}{unitX}\t{phyY:>4.2f}{unitY}\t{posX:10d}\t{posY:10d}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment