Pulling channel color from CZI metadata

Hi everyone,

I’ve been working on porting over some code from matlab to python for an in-lab image processing software I’ve been working on.

I’ve been using aicsimageio and aicspylibczi for czi import, but I can’t seem to find a way to either save the entirety of the metadata and index it for hex color code used for a channel, or to just pull the channel color directly.

To clarify, I cannot just use the get_channel_names function and assume color is always the same on each channel, since our images often use different colors on different channels depending on the type of probe being used.

Does anyone know a way to get the hex color code or string color associated with a channel in the CZI?

Thanks very much in advance.

Hey @ncalvert994

This sounds like a pretty deep metadata question that I don’t have the best answers for but can point you in directions you may want to look:

from aicsimageio import AICSImage

img = AICSImage("some_file.czi")
img.metadata  # This is an lxml element

Using that lxml element you can either save it out to an XML file to explore or dig around with xpath and look for the values you want that way.

I personally don’t know the CZI metadata spec too well but we do have, (and I don’t know if it’s the latest but pretty up-to-date), metadata XSL documents here to try and figure out what information you may be able to pull.

Maybe @sebi06 has better ideas though.

Hopefully some of this helps.

Thanks for your reply! I took a look through the czi-to-ome-xslt documents and the channel color hex is definitely in there.

I will try out these suggestions, but I think this will work. Once I get it working I’ll reply with the code in case this is useful for someone else in the future.

@ncalvert994, I use the follwing code inside my python package to read the channel colors from CZI using the czifile python package. A simplifies minimal example would be:

import czifile as zis
import numpy as np

filename_czi = r'E:\tmp\myimage.czi'

# get CZI object and read array
czi = zis.CziFile(filename_czi)

# parse the XML into a dictionary
channels_colors = []
metadatadict_czi = czi.metadata(raw=False)
sizeC = np.int(metadatadict_czi['ImageDocument']['Metadata']['Information']['Image']['SizeC'])

if sizeC == 1:
    channels_colors.append(metadatadict_czi['ImageDocument']['Metadata']['DisplaySetting']
                                  ['Channels']['Channel']['Color'])

if sizeC > 1:
    for ch in range(sizeC):
        channels_colors.append(metadatadict_czi['ImageDocument']['Metadata']['DisplaySetting']
                                  ['Channels']['Channel'][ch]['Color'])

czi.close()
print(channels_colors)
1 Like

Works perfectly. Thank you so much!

In addition to czifile, I can also recommend aicspylibczi

https://pypi.org/project/aicspylibczi/

it has additional functionality for reading multiscene czi files and it uses the libczi from Zeiss (I believe czifile has its own parsing of the czi file format). Czifile appears to be faster in most cases, but I don’t think it can read multiscene files currently.

1 Like

Good point. I also use aicspylibczi for reading multi-scene images which are to big for the RAM. And I am happy to see that our libCZI was picked up by the community and is useful.