Image file formats#

When working with microscopy image data, many file formats are circulating. Most microscope vendors bring proprietary image file formats, image analysis software vendors offer custom and partially open file formats. Traditional file formats exist as well which are supported by common python libraries. For general-purpose image storage, only very few image file formats such as TIF can be recommended. Furthermore, formats such as JPEG should be avoided to conserve image data integrity.

See also

In this notebook, we will open a proprietary file format (.czi) with a library desgined for that. Thus, we will use the czifile library, check links for that here and here.

If your file format is different, like .ims, .lif, among others, try checking if there is a library available to open it by searching in conda-forge or pipy. Another possibility is to open these files, including .czi, directly in napari by having the napari-aicsimageio plugin installed.

import czifile
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Input In [1], in <cell line: 1>()
----> 1 import czifile

ModuleNotFoundError: No module named 'czifile'

Here you may get an error from the cell above if you don’t have that package installed in your environment. Below we show you how to install this package.

Installing a new package#

This is a general recipe to install packages in a conda environment. Start by openning a second command line (we assume you have one already open, but that one is busy running jupyter lab).

First thing, very important, activate your environment with (replace environment_name by your environment name, replace conda by mamba or micromamba in case you use mamba):

conda activate environment_name

Now we use conda install (or mamba, see above) to install the package. Many packages are available from a channel called conda-forge, which is the case now, therefore, type the following command now to install czifile library from channel (-c) conda-forge:

conda install -c conda-forge czifile

After the installation finishes, you can close the second command line and continue here.

Opening a proprietary file#

Now, let’s try to import the library again.

import czifile

If it installed correctly, you should have no errors this time.

We can open an image usually by providing a path to it to an imread function (this may vary a bit from library to library, check their documentation):

from pathlib import Path
file_path = Path(r"../../../data/PupalWing.czi")
image = czifile.imread(file_path)

We usually have access to the image shape like this:

image.shape
(1, 1, 1, 80, 520, 692, 1)

These formats usually have extra dimentions to accomodate many different data shapes. Some possibilities for microscopy data are (more examples in this thread):

  • ‘X’: ‘Width’,

  • ‘Y’: ‘Height’,

  • ‘C’: ‘Channel’,

  • ‘Z’: ‘Slice’, # depth

  • ‘T’: ‘Time’,

  • ‘R’: ‘Rotation’,

  • ‘S’: ‘Scene’, # contiguous regions of interest in a mosaic image

We know we have a 3D image. One trick to get rid of the unecessary extra dimentions is the np.squeeze function.

import numpy as np
image_squeezed = np.squeeze(image)
image_squeezed.shape
(80, 520, 692)

Also, now we have a numpy array (check also np.asarray).

type(image_squeezed)
numpy.ndarray

We can now visualize it in napari. We will explain more about using napari from a jupyter notebook soon.

import napari
from napari.utils import nbscreenshot
viewer = napari.Viewer()
viewer.add_image(image_squeezed)
nbscreenshot(viewer)
WARNING: QWindowsWindow::setGeometry: Unable to set geometry 5436x2109+963+312 (frame: 5468x2197+947+240) on QWidgetWindow/"_QtMainWindowClassWindow" on "\\.\DISPLAY1". Resulting geometry: 3844x2108+963+312 (frame: 3876x2196+947+240) margins: 16, 72, 16, 16 minimum size: 385x493 MINMAXINFO maxSize=0,0 maxpos=0,0 mintrack=1187,1567 maxtrack=0,0)