Create a Z-stack with numpy

Hello everyone, newbie here!

Environment

macOS Mojave 10.14
Fiji Version 2.0.0-rc-69/1.52p
Scikit image version 0.15.0 - April 2019

Context

I tried creating a simple/random 5D image like so:

data_0 = np.random.randint(0, 255, (2, 5, 3, 300, 300), 'uint8')

with tifffile.TiffWriter('path/to/temp.ome.tiff') as tif:
    tif.save(data_0, metadata={'axes':'TZCYX'})

Problem

When I try opening it with Fiji, a have a 300X300 colored image but the SizeZ is 1 and SizeT is 10.

Questions

How do I create a Z-stack using scikit-image and save it as an .ome.tiff file ?
Why does it seem that the specified axes are disregarded by the save() function and the first two integers of the tuple multiplied ?

Thank you very much in advance for your help !

you might get what you want by using imagej=True in the TiffWriter constructor:

with tifffile.TiffWriter('path/to/temp.ome.tiff', imagej=True) as tif:
    tif.save(data_0)

#or directly
tifffile.imwrite('path/to/temp.ome.tiff', data_0, imagej=True)

note however, the bit about dimension ordering from the tifffile docstring:

imagej : bool
      If True, write an ImageJ hyperstack compatible file.
      This format can handle data types uint8, uint16, or float32 and
      data shapes up to 6 dimensions in TZCYXS order.
      RGB images (S=3 or S=4) must be uint8.
      ImageJ's default byte order is big-endian but this implementation
      uses the system's native byte order by default.
      ImageJ hyperstacks do not support BigTIFF or compression.
      The ImageJ file format is undocumented.
      When using compression, use ImageJ's Bio-Formats import function.
2 Likes

Thank you for the fast answer!
However this creates another problem: I am trying to save several of these kind of arrays non-contiguously, which is not supported by ImageJ…

I might need some more detail on that one… can you elaborate with an example?

I am trying to add a file to my answer but it might be too big (250 MB). Is there any other way to communicate with you ? Otherwise i’ll just upload it somewhere and add the link.

Please keep your conversation open and transparent by continuing this forum topic instead of any private conversation :slight_smile:

Yes, if your issue can’t be illustrated with a smaller example file, please use any file sharing service of your choice and post the link here. Thanks!

1 Like

Context

I am working with the tifffile python package for the APEER platform (www.apeer.com), more precisely the library is used in many of the Modules on the platform. It works fine for 5D but I am struggling with including series to write 6D.

To better understand my problem, let’s look at the following file:

When I open this file with Fiji, I get the following:

As you can see it has several Series, and when I choose one (the first one), I get this:

I can inspect both the channels and the z-stack.

Problem

Now, in order to get familiar with this package, I tried to reproduce this type of file by doing the following:

data_0 = np.random.randint(0, 255, (5, 3000, 3000, 3), 'uint16')
data_1 = np.random.randint(0, 255, (5, 300, 300, 3), 'uint8')

with tifffile.TiffWriter('path/to/temp.ome.tiff') as tif:
    tif.save(data_0)
    tif.save(data_1, contiguous=False)

Now I thought I was going to get a file with 2 Series with each a SizeZ (or SizeT) = 5. And this is the result I got:

This one has 10 Series, each with 1 plane and 3 channels. I initially thought I wasn’t properly generating the Z-stacks, which is why I wrote my initial question. However when I change my code the way Talley suggested it, I get the following error: “ValueError: ImageJ does not support non-contiguous data”

Question

How do I properly create a 6D image from scratch ?

Thank you all again for your time !

This will write a TIFF file containing two series, but the JSON metadata that describe the series are not understood by ImageJ or Bio-Format.

However when I change my code the way Talley suggested it, I get the following error: “ValueError: ImageJ does not support non-contiguous data”

AFAIK ImageJ hyperstack files cannot contain multiple series/hyperstacks in one file.

You could try to create an OME-TIFF compatible file to store multiple series in one TIFF file and be able to read them with Bio-Formats. Tifffile does not support writing OME-TIFF directly. There are several packages that you may try:

2 Likes

Thank you for the leads ! I will try some things out.