ImageJ incorrectly opens tif stack saved with deflate compression

I am having an issue using ImageJ to open compressed tifs - specifically, tif stacks that have been saved using “deflate”. From the UserGuide, it is my understanding that ImageJ supports opening this format.

When I attempt to open a stack of this format, ImageJ will open a stack of the correct dimensions, but will have the first slice duplicated into every other slice of the stack.

Here are the tif tags that imageJ reads from the file when opening:

Tif Tags
test.tif: opening
    254, "NewSubfileType", value=0
    256, "ImageWidth", value=256
    257, "ImageLength", value=256
    258, "BitsPerSample", value=16
    259, "Compression", value=8
    262, "PhotoInterp", value=1
    270, "ImageDescription", value=182, count=65
    273, "StripOffsets", value=312, count=2
    277, "SamplesPerPixel", value=1
    278, "RowsPerStrip", value=128
    279, "StripByteCount", value=320, count=2
    305, "Software", value=328, count=12
    306, "DateTime", value=340, count=20
    339, "SampleFormat", value=1
  nextIFD=131492
number of IFDs: 1
offset to first image: 368
gap between images: 0
little-endian byte order: true```

This does not happen when I use another program to open the tif or use Bio-Formats to import the tif into ImageJ (which has been a suitable work around for now).

Is this the expected behavior and ImageJ does not support opening tif stacks that have been saved with this compression?

I have attached an example test.tif (256.6 KB) or one can be created with the following python snippet (requires the tifffile and numpy packages):

#!/bin/python
import tifffile
import numpy as np
test_shape = (2, 256, 256)
dtype = np.uint16
test_stack = np.random.randint(
    low=0, high=(2**16)-1, size=test_shape, dtype=dtype)
tifffile.imsave(
    data=test_stack, file='test.tif', 
    imagej=True, compress=1)

Remove “imagej=True” and the Python snippet should create a file that will open correctly. Or upgrade to the latest ImageJ daily build (1.52p15), which works around this problem.

Remove “imagej=True” and the Python snippet should create a file that will open correctly.

This is true, but I think I will lose the ability to save/open hyperstacks. I had stripped this out of my example for simplicity.

Is it possible to save a compressed tif hyperstack in a way that ImageJ can open it with the correct dimensions?

Upgrade to the latest ImageJ daily build (1.52p15), which works around this problem. It opens the example compressed tiff as a hyperstack.

1 Like

This case is mentioned in the docstring of the tifffile.TiffWriter._init__ function:

    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.
3 Likes

I’m not sure how I missed this in your docstring, but thank you for pointing it out.