Convert .czi to .tiff with python

Hi,

I need to convert .czi to .tiff files with python.

I found several modules but couldn’t succeed at different steps of the process.

  1. With the aicsimageio module:
    I can’t open the 'OmeTiffWriter" output file (“file.ome.tiff”). It seems it has not the right format to be opened.
    2 . With the pylibczi module:
    the function “export_tiff” has the argument (reduce=>function mean>) which is completely understandable in the doc. I don’t know what it is.
  2. There is also a third module “czifile”. It opens czi files but I couldn’t find any function able to convert those files in tiff files.

Thanks for your help !

Francois

@francois

there a various options to achieve what you want. the czifile library has a built-in czi2tiff. Did you try this one?

Or I use the following code as well

from aicsimageio.writers import ome_tiff_writer

# write the array as an OME-TIFF incl. the metadata
try:
    with ome_tiff_writer.OmeTiffWriter(savepath, overwrite_file=overwrite) as writer:
        writer.save(imgarray,
                    channel_names=channel_names,
                    ome_xml=None,
                    image_name=os.path.basename((savepath)),
                    pixels_physical_size=pixels_physical_size,
                    channel_colors=None,
                    dimension_order=new_dimorder)
        writer.close()

except Exception as error:
    print(error.__class__.__name__ + ": " + error.msg)
    print('Could not write OME-TIFF')
    savepath = None
1 Like

Thanks @sebi06.

My image is “img2.czi”

“czi2tif img2.czi” returns “SyntaxError: invalid syntax”

Concerning “aicsimagebio”, I don’t see in the code where I must enter the name of the image file as an argument.

Hi @francois,

the code above is just a snippet which has to be adapted.

  • Use aicsimageio to read the CZI in the first place --> imgarray
  • image_name --> use whatever you need here
  • pixels_physical_size --> scaling
  • dimension order --> typically something like ‘TZCYX’

Can you post the img2.czi here. Maybe I can have a look?

czi2tif is a console script that is installed with pip install czifile tifffile imagecodecs. You can also run it using python -m czifile.czi2tif img2.czi .

czi2tif is also the name of a Python function that can be imported with from czifile import czi2tif and then called with czi2tif('img2.czi').

The script works with single scene CZI.

1 Like

@sebi06
I can’t upload my czi image here (Sorry, the file you are trying to upload is not authorized (authorized extensions: jpg, jpeg, png, gif, zip, rar, pdf, krwf, tif, tiff, svg, knwf, cp, mat, csv, cpproj, cppipe, m, bmp, webp, properties, xml, txt, jar, yml, yaml, ijm, protocol, ndpi).) Do you know how to do it in another way ?

@cgohlke
Thanks Christoph, I could get a tiff image. Unfoturnately, it’s in black&white and I can barely see my dapi colored neurons (the other staining in red (neurites)) is not visible. Do you know why and how could I get a RGB image as bright as the .CZI original?

I tried this code “ome_tiff_writer” with teh following syntax:

from aicsimageio.writers import OmeTiffWriter

img = AICSImage(“img2.czi”)

first_channel_data = img.get_image_data(“ZYX”, C=1, S=0, T=0)

with OmeTiffWriter(“file.ome.tiff”) as writer:
writer.save(
first_channel_data,
pixels_physical_size=img.get_physical_pixel_size(),
dimension_order=“ZYX”,
)

I could get an output file (“file.ome.tiff”), but I couldn’t open it. error message : “It seems that this file has not the right format to be opened.”

The image data are probably 16 bit grayscale. If you want to visualize/convert the image data in RGB colorspace, enhance the brightness and contrast if needed and apply colormaps to the channel dimension using numpy or other image processing libraries.

the original CZI image has two colors (dapi in blue and neurites staining in red). I don’t know how to perform all the operations you mentionned but I suppose it’s easy to find the python commands. However, do those operations need to be done on the CZI files or after the conversion, on the TIFF files ?

@francois: put your image in a .zip archive :wink:

1 Like

If I am reading this thread correctly, you have an RGB CZI file that you would like to convert to an OME TIFF. I believe the issue with aicsimageio would be that in the current release series (3.x) we have poor support for RGB anything. So your error message of “we dont support this file” while it’s a poor error message and should contain more info, is accurate in that we really don’t support that file (because of RGB).

We have already solved this in our upcoming 4.x series of releases but I can’t give you a timeline for release on that unfortunately.

If I have made an incorrect assumption about your CZI file being RGB and instead it is a just greyscale multi-channel image, can you please make a bug report on our repo and follow the bug report template: https://github.com/AllenCellModeling/aicsimageio/issues/new/choose

Will make the debugging process go much faster.

2 Likes

@cgohlke : did you mean that it’s really impossible to export a 2 scenes CZI in a single TIFF merging both scenes using czi2tif from czifile ?

Not impossible, but the scenes are merged in one array, which can result in a terrabyte of uncompressed TIFF file for some applications. czi2tif is meant to copy simple laser scanning microscopy data from CZI to standard (Big)TIFF such that the data can be accessed or memory-mapped by non-CZI aware software. In our lab, ZEN would take several hours to convert such files. For proper multi-scene or export to OME-TIFF it is probably better to use a CZI library that is still actively supported such as aicsimageio.

@cgohlke : does that mean that, for example, czi2tif would allow to put data in memory, and then that this data could be taken by libvips (pyvips in python) to work on it ?

EDIT : i did succeed using part of the script of @SergioLopez in this thread, but indeed it works like a charm for on scene CZI (~6 to 8 GB in RAM) but as soon as there is some scenes, it start to be heavy (32GB in memory for a 2 scene CZI).

Question : is there a way to load one scene at a time, and getting their position from the metadata using czi.metadata() ?

To get data into memory you could use czifile, aicspylibczi, or pylibczi directly. No need to go through TIFF.

For high-level access to scenes, better use aicspylibczi or pylibczi (unless you can’t depend on GPL libraries).

@cgohlke @sebi06
img3.zip (3.3 MB)

@JacksonMaxfield
Yes, you’re right, my image is a double channel red and blue CZI file. Thanks for your help. I’m looking forward to use your next release.

@sebi06
img3.zip (3.3 MB)

To get individual scenes and metadata from an CZI you should use aicspylibczi. It allows to read individual scene, subimages and pyramid levels.
On top you can also access all the metadata and XYZ positions from individual tiles if required.