Displaying uint16 images

I’m trying to display uint16 tifffiles in napari, but unless I do the normalisation myself (ie

def read_tiffile(filepath):
    image = io.imread(filepath)
    image = (image - image.min()) / (image.max() - image.min())
    return image

The image comes out as all white-- is there a setting I need to change? (at the moment I am just using napari.view_image(image)

Hi @wwymak, that’s certainly unusual. uint16 should work fine. Are you able to share the file you’re using?

yup-- here’s an example RGB-PanSharpen_AOI_3_Paris_img62.tif (2.4 MB)

thanks for the file, looks like this is the same as issue #924, it’s a uint16bit, but it’s also RGB, and we still have some issues dealing with 48bit RGB images. See that thread for more background, but until we fix this on our end, you have two options:

# use the channel_axis argument to tell napari your image is multichannel
napari.view_image(img, channel_axis=2, colormap=['red', 'green', 'blue'])

# keep it as an RGB image (where the channels are a single layer)
# but normalize the data:
napari.view_image(img/img.max())
1 Like

okay, cool. Thanks!
(I think I’ll stick with the normalisation because for some reason the first method froze the viewer…)

Ha! That’s a good reason :slight_smile:

Is it?? :joy: I feel like it needs to be investigated, but I can’t reproduce. @wwymak could you maybe submit a bug report at https://github.com/napari/napari/issues?

Yeah, doesn’t it go, “if at first you don’t succeed, it likely wasn’t meant to be”? :wink:

actually, now it didn’t freeze with the first method but it gave

Traceback (most recent call last):
  File "/home/wwymak/udacity-ml-eng/project_satellite_segmentation/data_preprocessing/dataviewer.py", line 68, in <module>
    viewer.add_image(stack_masks, channel_axis=2, colormap=['red', 'green', 'blue'])
  File "/home/wwymak/anaconda3/envs/solaris/lib/python3.7/site-packages/napari/components/add_layers_mixin.py", line 269, in add_image
    layer = self.add_layer(layers.Image(image, **i_kwargs))
  File "/home/wwymak/anaconda3/envs/solaris/lib/python3.7/site-packages/napari/layers/image/image.py", line 246, in __init__
    self._update_dims()
  File "/home/wwymak/anaconda3/envs/solaris/lib/python3.7/site-packages/napari/layers/base/base.py", line 403, in _update_dims
    self.refresh()
  File "/home/wwymak/anaconda3/envs/solaris/lib/python3.7/site-packages/napari/layers/base/base.py", line 633, in refresh
    self.set_view_slice()
  File "/home/wwymak/anaconda3/envs/solaris/lib/python3.7/site-packages/napari/layers/base/base.py", line 586, in set_view_slice
    self._set_view_slice()
  File "/home/wwymak/anaconda3/envs/solaris/lib/python3.7/site-packages/napari/layers/image/image.py", line 545, in _set_view_slice
    image = np.asarray(self.data[self.dims.indices]).transpose(order)
  File "/home/wwymak/anaconda3/envs/solaris/lib/python3.7/site-packages/numpy/core/_asarray.py", line 85, in asarray
    return array(a, dtype, copy=False, order=order)
  File "/home/wwymak/anaconda3/envs/solaris/lib/python3.7/site-packages/dask/array/core.py", line 1340, in __array__
    x = self.compute()
  File "/home/wwymak/anaconda3/envs/solaris/lib/python3.7/site-packages/dask/base.py", line 166, in compute
    (result,) = compute(self, traverse=False, **kwargs)
  File "/home/wwymak/anaconda3/envs/solaris/lib/python3.7/site-packages/dask/base.py", line 445, in compute
    return repack([f(r, *a) for r, (f, a) in zip(results, postcomputes)])
  File "/home/wwymak/anaconda3/envs/solaris/lib/python3.7/site-packages/dask/base.py", line 445, in <listcomp>
    return repack([f(r, *a) for r, (f, a) in zip(results, postcomputes)])
  File "/home/wwymak/anaconda3/envs/solaris/lib/python3.7/site-packages/dask/array/core.py", line 986, in finalize
    return concatenate3(results)
  File "/home/wwymak/anaconda3/envs/solaris/lib/python3.7/site-packages/dask/array/core.py", line 4424, in concatenate3
    result[idx] = arr
ValueError: could not broadcast input array from shape (1,900) into shape (1,650)

I am using the dask array stacks to have a slider to view all the images in my folder like so:

    io.use_plugin('tifffile')
    sample_img = io.imread(image_filepaths[90])
    sample_mask = np.array(Image.open(mask_filepaths[90]))
    lazy_imread = delayed(read_tiffile)  # lazy reader
    lazy_arrays_img = [delayed(read_tiffile)(fn) for fn in image_filepaths]
    lazy_arrays_masks = [delayed(read_mask)(fn) for fn in mask_filepaths]
    dask_arrays_img = [
        da.from_delayed(delayed_reader, shape=sample_img.shape, dtype=sample_img.dtype)
        for delayed_reader in lazy_arrays_img
    ]

    dask_arrays_masks = [
        da.from_delayed(delayed_reader, shape=sample_mask.shape, dtype=sample_mask.dtype)
        for delayed_reader in lazy_arrays_masks
    ]
    # Stack into one large dask.array
    stack_img = da.stack(dask_arrays_img, axis=0)
    stack_masks = da.stack(dask_arrays_masks)

    with napari.gui_qt():

        viewer = napari.view_image(stack_img)
        viewer.add_image(stack_masks, channel_axis=2, colormap=['red', 'green', 'blue'])

Maybe I am not passing in the correct args to the stacks?