How to read in Zeiss CZI files into numpy and plot.

Download original file: 10_CZI_Zeiss_images.ipynb

View original file in nbviewer: 10_CZI_Zeiss_images.ipynb

Opening some image formats can be a pain

The Zeiss CZI image format can be opened up using code provided by Christoph Gohlke. There are other ways, but they rely on even more magic…

Our first goal is import code from his module. For this we need to append the code directory to the system path.

import sys
sys.path.insert(0, '../../code/')

from czifile import CziFile

../../code/czifile.py:123: UserWarning: failed to import the optional _czifile C extension module.
Decoding of JXR and JPG encoded images will be unavailable.
Czifile.pyx can be obtained at http://www.lfd.uci.edu/~gohlke/
  "failed to import the optional _czifile C extension module.\n"

The CziFile class allows us to read all of the images at once:

with CziFile('/home/david/Dropbox/images/test.czi') as czi:
    image_arrays = czi.asarray()

If we look at the underlying numpy array, we see that there are quite a few dimensions:

image_arrays.shape




(1, 3, 1, 18, 1024, 1024, 1)

It looks like three regions of interest with 18 z-stacked images in each region.

Now we can look at this array of 18 images for region 1:

%matplotlib inline
import matplotlib.pyplot as plt

# This indexing gives us full sized images in region 1
# Some playing was required to extract the correct data.
images = [image_arrays[0,0,0,index].T[0] for index in range(18)]

# To fit on the screen in a nice way, we can arange the 
# z-stack in a grid of 6x3 on a large figure.
N_rows = 6
N_cols = 3
fig, ax_grid = plt.subplots(N_rows, N_cols, figsize=(N_cols*10,N_rows*10))

for row in range(N_rows):
    for col in range(N_cols):
        image = images.pop()
        ax_grid[row][col].imshow(image)

png

Exercise:

Try to visualize only the cell in region x, y = [400:620, 400:600].