Search code examples
pythonnumpytiffscikit-imageimagej

Create a multi-stack (e.g., 12 color) tiff image in python for ImageJ


I have an array with dimension (5000, 5000, 12) representing X, Y and color. It is already dtype=uint16.

I would like to create a tiff stack from this array so I can load it in ImageJ. My approach so far:

skimage.io.imsave(
   'Top4cores.tif', full_small_image,
    bigtiff=True, imagej=True, resolution=(1, 1),
    metadata={'spacing': 1, 'unit': 'um', 'axes': 'XYC'},photometric='minisblack')

Unfortunately, this creates an image with:

SizeC = 5000
SizeT = 1
SizeX = 18
SizeY = 5000
SizeZ = 1

How can I make sure my image has the correct coordinates? Do I have to export individual images and stack them afterwards?


Solution

  • skimage.io.imsave uses tifffile under the hood. Recent versions raise builtins.ValueError: ImageJ hyperstack axes must be in TZCYXS order. To fix this error, update tifffile, reverse the dimensions of your array, and use axes='CYX':

    tifffile.imwrite(
        'Top4cores.tif',
        full_small_image.transpose(),
        imagej=True,
        resolution=(1, 1),
        photometric='minisblack',
        metadata={'spacing': 1, 'unit': 'um', 'axes': 'CYX'}
    )