Napari loading images from OMERO

Hi,

I just heard about your nice napari viewer and since we are working on python3 support for OMERO (Call Omero API from python3?) the timing is great.

Here’s some sample code to view a Z-stack multi-channel image from OMERO in napari.

import napari
from vispy.color import Colormap
import numpy
from omero.gateway import BlitzGateway
conn = BlitzGateway('username', 'password', port=4064, host="omero.server")
conn.connect()

IMAGE_ID = 4418
image = conn.getObject("Image", IMAGE_ID)

def get_z_stack(img, c=0, t=0):
    zct_list = [(z, c, t) for z in range(img.getSizeZ())]
    pixels = image.getPrimaryPixels()
    return numpy.array(list(pixels.getPlanes(zct_list)))

with napari.gui_qt():
    viewer = napari.Viewer()

    for c, channel in enumerate(image.getChannels()):
        print('loading channel %s' % c)
        data = get_z_stack(image, c=c)
        # use current rendering settings from OMERO
        color = channel.getColor().getRGB()
        color = [r/256 for r in color]
        cmap = Colormap([[0, 0, 0], color])
        # Z-scale for 3D viewing
        size_x = image.getPixelSizeX()
        size_z = image.getPixelSizeZ()
        z_scale = size_z / size_x
        viewer.add_image(data, blending='additive',
                         colormap=('from_omero', cmap),
                         scale=[1, z_scale, 1, 1],
                         name=channel.getLabel())

    print('closing conn...')
    conn.close()

Thanks,

Will.

6 Likes

Here’s the current screenshot:

5 Likes

This is awesome. Exactly what I have been looking for!
Might be a good solution for this as well
(5D volume viewing in Python)

1 Like

This is really cool!

EDIT: I updated the code sample above to include channel label, color and Z-scale metadata from OMERO.

1 Like