Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interactive cropping #43

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/crop.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"id": "deec8f67-28d2-4823-9f0e-f98feb8c8f3c",
"metadata": {},
"source": [
"The widget contains 2 or 3 sliders, for 2D and 3D images respectively. It also supports `stackview`'s typical parameters such as `continuous_update`, `zoom_factor`, etc."
"The widget contains 2 or 3 sliders, for 2D and 3D images respectively. It also supports `stackview`'s typical parameters such as `continuous_update`, `zoom_factor`, etc.\n",
"The crop can either be selected via the sliders or with two mouse clicks (left click for top left corner, right lick for bottom right corner of the cropped rectangle)."
]
},
{
Expand Down
41 changes: 41 additions & 0 deletions stackview/_crop.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,47 @@ def __init__(self,
if len(image.shape) > 2:
widgets.append(slice_slider)

from ipyevents import Event

event_handler_top_left = Event(source=view, watched_events=["click"])
event_handler_bottom_right = Event(source=view, watched_events=["auxclick"])

def update_rec_top_left(event=None):
relative_position_x = event["relativeX"] / zoom_factor
relative_position_y = event["relativeY"] / zoom_factor
positions = [int(relative_position_y), int(relative_position_x)]

for i in [-1, -2]:
cmin, cmax = self._range_sliders[i].value
cvalue = positions[i] + cmin # Adjust position for current crop
self._range_sliders[i].value = (cvalue, cmax)

if len(self._image.shape) > 2:
cvalue = slice_slider.value
cmin, cmax = self._range_sliders[0].value
self._range_sliders[0].value = (cvalue, cmax)
self.update()

def update_rec_bottom_right(event=None):
relative_position_x = event["relativeX"] / zoom_factor
relative_position_y = event["relativeY"] / zoom_factor
positions = [int(relative_position_y), int(relative_position_x)]

for i in [-1, -2]:
cmin, cmax = self._range_sliders[i].value
cvalue = positions[i] + cmin # Adjust position for current crop
self._range_sliders[i].value = (cmin, cvalue)

if len(self._image.shape) > 2:
cvalue = slice_slider.value
cmin, cmax = self._range_sliders[0].value

self._range_sliders[0].value = (cmin, cvalue)
self.update()

event_handler_top_left.on_dom_event(update_rec_top_left)
event_handler_bottom_right.on_dom_event(update_rec_bottom_right)

super(_Cropper, self).__init__(widgets)

def update(self, event=None):
Expand Down