Changing the voxel size in Napari

Hi everyone,

I’m a very early Napri user and it looks very very powerfull. I’m playing first with different layers in order display/hide some channels change their brightness/contrast/gamma. Very easy with my tiff images.

I’m a totally Python newbie, I never use it since I try to use Napari. It was difficult for me to understand everything even just to instal it (the code to use, where to write it,etc.) But now, it work and it’s cool !

Now I’m playing with orthogonal views and 3D but my voxels aren’t isotropic and I’m looking for changing their ratio. And last, I’m playing with lightsheet 3D images and I’m asking if the images are shown in pyramidal view or not.
I’ve tried to see some post / tutorial about these different things but I don’t understand it and I don’t know where I have to write the line code to do it could someone help me ?

Thanks,
Nicolas

1 Like

Hi,

the trick here is to use the scalefactores. Have a look here:

Read_and_Display_Images_using_Widgets_and_Napari.ipynb

In principle it goes along those lines below. You just have to know what the “scalefactor” between XY and Z ist. For example is your xy scaling is 0.1 micron / pixel and z is 0.3 micron per pixel, that factor is 3.

In the example below I retrive taht factor always automatically from the metadata.

# configure napari viewer manually - check array shape and dimensions order carefully 
    
# get the scalefactors
scalefactors = imf.get_scalefactor(metadata)
print(scalefactors)

viewer = napari.Viewer()

# add every channel as a single layer
for ch in range(metadata['SizeC']):
    chname = metadata['Channels'][ch]
    channel = array[:, :, ch, :, :, :]
    clim = [channel.min(), np.round(channel.max() * 0.85)]
    # add channel as a layer
    viewer.add_image(channel,
                     name=chname,
                     scale=(1, 1, 1, scalefactors['zx'], 1, 1),
                     contrast_limits=clim,
                     blending='additive',
                     gamma=0.85)
1 Like

Ok @sebi06, thanks for your fast answer, I understand that scalefactor I’ve already practice it in Vaa3D :wink:
My problem is where did I write that code ? In the Napari windows ?

1 Like

Hi @NicoPIC, how are you launching napari and looking at your images right now? See our getting started tutorial for a full list of ways.

If you are using a jupyter notebook or a python script you could put the code from @sebi06 in that script.

If you’ve just launched napari from the command line so you see an empty viewer, you can then use the console in napari, accessible by clicking the console open button, to add images using code. In particular you’d type the viewer.add_image part of the code in there.

Finally if you’ve launched napari from the command line and then drag / drop your data (or use file open) to load your data you can then open the console and set the scale using viewer.layers['Image'].scale = [2, 1, 1] say where Image is the string name of the layer shown in the layers list of the left side of the viewer and [2, 1, 1] are the scale factors. We’ll be making our file loading dialog a bit nicer so you can set parameters like scale directly on loading. Hopefully we’ll have this supported in the next few weeks.

If you’re new to python you might want to look at some of the tutorials around jupyter notebooks and scikit-image as napari works well with both these tools, and I’d probably recommend learning how to launch napari from a jupyter notebook as it will give you a lot of flexibility in the future but isn’t overly complex.

2 Likes

Just a screenshot to illustrate one of @sofroniewn ’ suggestions:

1 Like

Finally if you’ve launched napari from the command line and then drag / drop your data (or use file open) to load your data you can then open the console and set the scale using viewer.layers['Image'].scale = [2, 1, 1] say where Image is the string name of the layer shown in the layers list of the left side of the viewer and [2, 1, 1] are the scale factors. We’ll be making our file loading dialog a bit nicer so you can set parameters like scale directly on loading. Hopefully we’ll have this supported in the next few weeks.

That work ! Thanks ! And I’ll have a look at the Jupiter notebook :wink: It’s in my next self improvements

I’m doing the tutorial for people who never use python I’ll try to post it soon :wink:

2 Likes