Skip to content

Commit

Permalink
added switch
Browse files Browse the repository at this point in the history
  • Loading branch information
haesleinhuepf committed Jan 29, 2023
1 parent 4c6c360 commit 45735a8
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 75 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,20 @@ stackview.side_by_side(image_stack[1:], image_stack[:-1], continuous_update=True
```
![](https://raw.githubusercontent.com/haesleinhuepf/stackview/main/docs/images/demo_side_by_side.gif)

### Switch

The `switch` function allows to switch between a list or dictionary of images.

```
stackview.switch([
slice_image,
binary,
labels
])
```

![](https://raw.githubusercontent.com/haesleinhuepf/stackview/main/docs/images/demo_switch.gif)

### Crop

You can crop images interactively:
Expand Down
196 changes: 122 additions & 74 deletions docs/demo.ipynb

Large diffs are not rendered by default.

Binary file added docs/images/demo_switch.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions stackview/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from ._side_by_side import side_by_side
from ._picker import picker
from ._assist import assist
from ._switch import switch



Expand Down
2 changes: 1 addition & 1 deletion stackview/_slice_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __init__(self,
# widgets.link((sliders1, 'value'), (slider2, 'value'))

# event handler when the user changed something:
def configuration_updated(event):
def configuration_updated(event=None):
if len(self.image.shape) == 3 and self.image.shape[-1] != 3:
self.slice_slider.layout.display = None
self.view.data = np.take(self.image, self.slice_slider.value, axis=axis)
Expand Down
80 changes: 80 additions & 0 deletions stackview/_switch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
def switch(images,
slice_number : int = None,
axis : int = 0,
display_width : int = None,
display_height : int = None,
continuous_update:bool=False,
slider_text:str="Slice",
zoom_factor:float = 1.0,
zoom_spline_order:int = 0
):
"""Allows switching between multiple images.
Parameters
----------
images : list(image), or dict(str:image)
Images to switch between. All images should have the same shape.
slice_number : int, optional
Slice-position in the stack
axis : int, optional
Axis in case we are slicing a stack
display_width : int, optional
This parameter is obsolete. Use zoom_factor instead
display_height : int, optional
This parameter is obsolete. Use zoom_factor instead
continuous_update : bool, optional
Update the image while dragging the mouse, default: False
zoom_factor: float, optional
Allows showing the image larger (> 1) or smaller (<1)
zoom_spline_order: int, optional
Spline order used for interpolation (default=0, nearest-neighbor)
Returns
-------
An ipywidget with an image display and a slider.
"""
from ._utilities import _no_resize
from ._slice_viewer import _SliceViewer
import ipywidgets

if isinstance(images, dict):
names = list(images.keys())
images = list(images.values())
layout = None
else:
names = [str(i) for i in range(len(images))]
layout = ipywidgets.Layout(min_width='10px', max_width='30px')

viewer = _SliceViewer(images[0],
slice_number,
axis,
display_width,
display_height,
continuous_update,
slider_text,
zoom_factor=zoom_factor,
zoom_spline_order=zoom_spline_order
)
view = viewer.view
slice_slider = viewer.slice_slider

buttons = []
for name, image in zip(names, images):
button = _make_button(name, image, layout, viewer)
buttons.append(button)

return ipywidgets.VBox([_no_resize(view), ipywidgets.HBox(buttons), slice_slider])

def _make_button(name, image, layout, viewer):
import ipywidgets
if layout is None:
button = ipywidgets.Button(description=name)
else:
button = ipywidgets.Button(description=name, layout=layout)

def act(event=None):
viewer.image = image
viewer.configuration_updated()

button.on_click(act)
return button

0 comments on commit 45735a8

Please sign in to comment.