Skip to content

Instantly share code, notes, and snippets.

@raacampbell
Created May 9, 2022 13:58
Show Gist options
  • Save raacampbell/acbc6da468c6340a42d8b699e10f238a to your computer and use it in GitHub Desktop.
Save raacampbell/acbc6da468c6340a42d8b699e10f238a to your computer and use it in GitHub Desktop.
Loading an image, adding layers, manipulating layers
import napari
import numpy as np
from skimage import data
from skimage.color import rgb2gray
# Convert the astronaut image to a grayscale
astro_im = rgb2gray(data.astronaut())
# Display the astronaut image and also display its negative in a new layer
viewer = napari.view_image(astro_im, name='astronaut')
viewer.add_image(astro_im*-1, name='negative')
# Put the two images next to each other as a grid
viewer.grid.enabled = True
# The two layers are in a list:
viewer.layers
# What is in this?
viewer.layers[0].__dir__()
# The name of the layers
[x.name for x in viewer.layers]
# Disable both layers then enabel the first one (the original image)
viewer.layers[0].visible = False
viewer.layers[1].visible = False
viewer.layers[0].visible = True
# The data the comprise the first layer are here:
viewer.layers[0].data
# We can invert it to make it a negative image
viewer.layers[0].data = viewer.layers[0].data * -1
# Image looks blank because the contrast range is now wrong.
# Therefore we reset the contrast limits:
viewer.layers[0].reset_contrast_limits()
# Flip the raw data up/down
viewer.layers[0].data = np.flipud(viewer.layers[0].data)
import napari
import numpy as np
from skimage import data
from skimage.color import rgb2gray
# Convert the astronaut image to a grayscale
astro_im = rgb2gray(data.astronaut())
# Display the astronaut image and also display its negative in a new layer
viewer = napari.view_image(astro_im, name='astronaut')
viewer.add_image(astro_im*-1, name='negative')
# Put the two images next to each other as a grid
viewer.grid.enabled = True
# The two layers are in a list:
viewer.layers
# What is in this?
viewer.layers[0].__dir__()
# The name of the layers
[x.name for x in viewer.layers]
# Disable both layers then enabel the first one (the original image)
viewer.layers[0].visible = False
viewer.layers[1].visible = False
viewer.layers[0].visible = True
# The data the comprise the first layer are here:
viewer.layers[0].data
# We can invert it to make it a negative image
viewer.layers[0].data = viewer.layers[0].data * -1
# Image looks blank because the contrast range is now wrong.
# Therefore we reset the contrast limits:
viewer.layers[0].reset_contrast_limits()
# Flip the raw data up/down
viewer.layers[0].data = np.flipud(viewer.layers[0].data)
# Invert again and change the colormap
viewer.layers[0].data = viewer.layers[0].data * -1
viewer.layers[0].reset_contrast_limits()
viewer.layers[0].colormap = "inferno"
# Manually alter contrast
viewer.layers[0].contrast_limits = [0, 0.75]
# Enable the upper, negative image
viewer.layers[1].visible = True
# Invert again and change the colormap
viewer.layers[0].data = viewer.layers[0].data * -1
viewer.layers[0].reset_contrast_limits()
viewer.layers[0].colormap = "inferno"
# Manually alter contrast
viewer.layers[0].contrast_limits = [0, 0.75]
# Enable the upper, negative image
viewer.layers[1].visible = True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment