Skip to content

Instantly share code, notes, and snippets.

@hiraksarkar
Created October 14, 2025 15:07
Show Gist options
  • Select an option

  • Save hiraksarkar/ff178d72a3a01688b9f06f854832b9de to your computer and use it in GitHub Desktop.

Select an option

Save hiraksarkar/ff178d72a3a01688b9f06f854832b9de to your computer and use it in GitHub Desktop.
Parsing ome.tiff for getting channels
import tifffile
file_name = 'laulab_tma1_file.ome.tiff'
with tifffile.TiffFile(file_name) as tif:
img = tif.asarray()
print(f"Image shape: {img.shape}")
print(f"Image dtype: {img.dtype}")
print(f"Number of pages: {len(tif.pages)}")
# Check if OME metadata exists
if tif.ome_metadata:
print("\nOME metadata found")
print(tif.ome_metadata[:500])
# Parse OME metadata to get channel names
if tif.ome_metadata:
import xml.etree.ElementTree as ET
try:
root = ET.fromstring(tif.ome_metadata)
ns = {'ome': 'http://www.openmicroscopy.org/Schemas/OME/2016-06'}
# Find all channels
channels = root.findall('.//ome:Channel', ns)
print(f"\n=== FOUND {len(channels)} CHANNELS/PROTEINS ===")
for i, ch in enumerate(channels):
name = ch.get('Name', f'Channel_{i}')
ch_id = ch.get('ID', 'N/A')
print(f" {i}: {name} (ID: {ch_id})")
# Also check image dimensions
pixels = root.find('.//ome:Pixels', ns)
if pixels is not None:
print(f"\nImage dimensions from OME:")
print(f" SizeC (channels): {pixels.get('SizeC')}")
print(f" SizeX: {pixels.get('SizeX')}")
print(f" SizeY: {pixels.get('SizeY')}")
print(f" SizeZ: {pixels.get('SizeZ', '1')}")
except Exception as e:
print(f"Could not parse OME XML: {e}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment