Hi,
I’d like to replace the shapes of my Shapes layer with some new shapes. Something like this:
from skimage import data
import napari
import numpy as np
def enable_gui_qt():
from IPython import get_ipython
ipython = get_ipython()
ipython.magic('gui qt')
enable_gui_qt()
viewer = napari.view_image(data.astronaut(), rgb=True)
old_shapes = [np.array([[217.18092301, 14.32894795],
[531.98354928, 14.32894795],
[531.98354928, 277.02631195],
[217.18092301, 277.02631195]]),
np.array([[81.49013583, 199.95394483],
[287.74013235, 199.95394483],
[287.74013235, 396.43420467],
[81.49013583, 396.43420467]])]
shapes_layer = viewer.add_shapes(data=old_shapes,
face_color='transparent',
edge_color='red',
edge_width=4,
name='some_name')
new_shapes = [np.array([[266.0296064, 300.61183407],
[490.77960153, 300.61183407],
[490.77960153, 510.63156727],
[266.0296064, 510.63156727]])]
shapes_layer.data = new_shapes
This works as expected. However, this approach fails when having properties:
from skimage import data
import napari
import numpy as np
def enable_gui_qt():
from IPython import get_ipython
ipython = get_ipython()
ipython.magic('gui qt')
enable_gui_qt()
viewer = napari.view_image(data.astronaut(), rgb=True)
old_shapes = [np.array([[217.18092301, 14.32894795],
[531.98354928, 14.32894795],
[531.98354928, 277.02631195],
[217.18092301, 277.02631195]]),
np.array([[81.49013583, 199.95394483],
[287.74013235, 199.95394483],
[287.74013235, 396.43420467],
[81.49013583, 396.43420467]])]
old_scores = list(np.random.rand(len(old_shapes)))
old_labels = [f'old_label_{i}' for i in range(len(old_shapes))]
properties = {
'labels': old_labels,
'scores': old_scores
}
text_parameters = {
'text': '{labels} {scores:.2f}',
'size': 12,
'color': 'white',
'anchor': 'upper_left',
'translation': [-1, 0],
}
shapes_layer = viewer.add_shapes(data=old_shapes,
face_color='transparent',
edge_color='red',
edge_width=4,
name='some_name',
properties=properties,
text=text_parameters)
new_shapes = [np.array([[220.18092301, 14.32894795],
[531.98354928, 14.32894795],
[531.98354928, 277.02631195],
[220.18092301, 277.02631195]]),
np.array([[90.49013583, 199.95394483],
[287.74013235, 199.95394483],
[287.74013235, 396.43420467],
[90.49013583, 396.43420467]]),
np.array([[266.0296064, 300.61183407],
[490.77960153, 300.61183407],
[490.77960153, 510.63156727],
[266.0296064, 510.63156727]])]
shapes_layer.data = new_shapes
How do I update the properties correctly without destroying the layer? Can anyone point me in the right direction?