Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
visible = False
else:
visible = True
else:
visible = True
# Drop channel from dims string
dims = (
dims.replace(Dimensions.Channel, "")
if Dimensions.Channel in dims
else dims
)
# Run napari
with napari.gui_qt():
napari.view_image(
data,
is_pyramid=False,
ndisplay=3 if Dimensions.SpatialZ in dims else 2,
channel_axis=c_axis,
axis_labels=dims,
title=title,
visible=visible,
**kwargs,
)
except ModuleNotFoundError:
raise ModuleNotFoundError(
f"'napari' has not been installed. To use this function install napari "
f"with either: pip install napari' or "
"""
Display one image using the add_image API.
"""
from skimage import data
import napari
with napari.gui_qt():
# create the viewer with an image
viewer = napari.view_image(data.astronaut(), rgb=True)
if isinstance(self.reader, ArrayLikeReader):
title = f"napari: {self.dask_data.shape}"
else:
title = f"napari: {self.reader._file.name}"
# Handle RGB entirely differently
if rgb:
# Swap channel to last dimension
new_dims = f"{dims.replace(Dimensions.Channel, '')}{Dimensions.Channel}"
data = transforms.transpose_to_dims(
data=data, given_dims=dims, return_dims=new_dims
)
# Run napari
with napari.gui_qt():
napari.view_image(
data,
is_pyramid=False,
ndisplay=3 if Dimensions.SpatialZ in dims else 2,
title=title,
axis_labels=dims.replace(Dimensions.Channel, ""),
rgb=rgb,
**kwargs,
)
# Handle all other images besides RGB not requested
else:
# Channel axis
c_axis = (
dims.index(Dimensions.Channel)
if Dimensions.Channel in dims
else None
"""
Display one points layer ontop of one image layer using the add_points and
add_image APIs
"""
import numpy as np
from skimage import data
import napari
print("click to add points; close the window when finished.")
with napari.gui_qt():
viewer = napari.view_image(data.astronaut(), rgb=True)
points = viewer.add_points(np.zeros((0, 2)))
points.mode = 'add'
print("you clicked on:")
print(points.data)
"""
Display a labels layer above of an image layer using the add_labels and
add_image APIs
"""
from skimage import data
from scipy import ndimage as ndi
import napari
with napari.gui_qt():
blobs = data.binary_blobs(length=128, volume_fraction=0.1, n_dim=3)
viewer = napari.view_image(blobs.astype(float), name='blobs')
labeled = ndi.label(blobs)[0]
viewer.add_labels(labeled, name='blob ID')
"""
Display a labels layer above of an image layer using the add_labels and
add_image APIs
"""
from skimage import data
from skimage.color import rgb2gray
from skimage.segmentation import slic
import napari
with napari.gui_qt():
astro = data.astronaut()
# initialise viewer with astro image
viewer = napari.view_image(rgb2gray(astro), name='astronaut', rgb=False)
# add the labels
# we add 1 because SLIC returns labels from 0, which we consider background
labels = slic(astro, multichannel=True, compactness=20) + 1
label_layer = viewer.add_labels(labels, name='segmentation')
# Set the labels layer mode to picker with a string
label_layer.mode = 'picker'
print(f'The color of label 5 is {label_layer.get_color(5)}')
from skimage.color import rgb2gray
from skimage.transform import pyramid_gaussian
import napari
import numpy as np
# create pyramid from astronaut image
base = np.tile(data.astronaut(), (8, 8, 1))
pyramid = list(
pyramid_gaussian(base, downscale=2, max_layer=4, multichannel=True)
)
print('pyramid level shapes: ', [p.shape[:2] for p in pyramid])
with napari.gui_qt():
# add image pyramid
napari.view_image(pyramid, is_pyramid=True)
"""
Display multiple image layers using the add_image API and then reorder them
using the layers swap method and remove one
"""
from skimage import data
from skimage.color import rgb2gray
import numpy as np
import napari
with napari.gui_qt():
# create the viewer with several image layers
viewer = napari.view_image(rgb2gray(data.astronaut()), name='astronaut')
viewer.add_image(data.camera(), name='photographer')
viewer.add_image(data.coins(), name='coins')
viewer.add_image(data.moon(), name='moon')
viewer.add_image(np.random.random((512, 512)), name='random')
viewer.add_image(data.binary_blobs(length=512, volume_fraction=0.2, n_dim=2), name='blobs')
viewer.grid_view()
# create pyramid from astronaut image
astronaut = data.astronaut()
base = np.tile(astronaut, (3, 3, 1))
pyramid = list(
pyramid_gaussian(base, downscale=2, max_layer=3, multichannel=True)
)
pyramid = [
np.array([p * (abs(3 - i) + 1) / 4 for i in range(6)]) for p in pyramid
]
print('pyramid level shapes: ', [p.shape for p in pyramid])
with napari.gui_qt():
# add image pyramid
napari.view_image(pyramid, is_pyramid=True)
import numpy as np
from skimage import data
import napari
with napari.gui_qt():
blobs = np.stack(
[
data.binary_blobs(
length=128, blob_size_fraction=0.05, n_dim=3, volume_fraction=f
)
for f in np.linspace(0.05, 0.5, 10)
],
axis=0,
)
viewer = napari.view_image(blobs.astype(float))
# add the points
points = np.array(
[
[0, 0, 100, 100],
[0, 0, 50, 120],
[1, 0, 100, 40],
[2, 10, 110, 100],
[9, 8, 80, 100],
]
)
viewer.add_points(
points, size=[0, 6, 10, 10], face_color='blue', n_dimensional=True
)