1
+ def blend (
2
+ image1 ,
3
+ image2 ,
4
+ slice_number : int = None ,
5
+ axis : int = 0 ,
6
+ continuous_update : bool = True ,
7
+ blend_factor : float = 0.5 ,
8
+ zoom_factor : float = 1.0 ,
9
+ zoom_spline_order : int = 0 ,
10
+ colormap1 : str = None ,
11
+ display_min1 : float = None ,
12
+ display_max1 : float = None ,
13
+ colormap2 : str = None ,
14
+ display_min2 : float = None ,
15
+ display_max2 : float = None
16
+ ):
17
+ """Show two images blended together with a slider to control the blend factor.
18
+
19
+ Parameters
20
+ ----------
21
+ image1 : image
22
+ First image to blend
23
+ image2 : image
24
+ Second image to blend
25
+ slice_number : int, optional
26
+ Slice-position in case we are looking at an image stack
27
+ axis : int, optional
28
+ This parameter is obsolete. If you want to show any other axis than the first, you need to transpose the image before, e.g. using np.swapaxes().
29
+ continuous_update : bool, optional
30
+ Update the image while dragging the mouse, default: True
31
+ blend_factor: float, optional
32
+ Controls the blend between images (0 = only image1, 1 = only image2)
33
+ zoom_factor: float, optional
34
+ Allows showing the image larger (> 1) or smaller (<1)
35
+ zoom_spline_order: int, optional
36
+ Spline order used for interpolation (default=0, nearest-neighbor)
37
+ colormap1: str, optional
38
+ Matplotlib colormap name or "pure_green", "pure_magenta", ... for first image
39
+ display_min1: float, optional
40
+ Lower bound of properly shown intensities for first image
41
+ display_max1: float, optional
42
+ Upper bound of properly shown intensities for first image
43
+ colormap2: str, optional
44
+ Matplotlib colormap name or "pure_green", "pure_magenta", ... for second image
45
+ display_min2: float, optional
46
+ Lower bound of properly shown intensities for second image
47
+ display_max2: float, optional
48
+ Upper bound of properly shown intensities for second image
49
+
50
+ Returns
51
+ -------
52
+ An ipywidget with an image display and a slider.
53
+ """
54
+ import ipywidgets
55
+ from ._image_widget import ImageWidget
56
+ from ._slice_viewer import _SliceViewer
57
+ import numpy as np
58
+ from ._utilities import _no_resize
59
+ from ._uint_field import intSlider
60
+
61
+ if 'cupy.ndarray' in str (type (image1 )):
62
+ image1 = image1 .get ()
63
+
64
+ if 'cupy.ndarray' in str (type (image2 )):
65
+ image2 = image2 .get ()
66
+
67
+ # setup user interface for changing the blend factor
68
+ blend_slider = intSlider (
69
+ value = blend_factor ,
70
+ min = 0 ,
71
+ max = 100 ,
72
+ continuous_update = continuous_update ,
73
+ description = "Blend"
74
+ )
75
+
76
+ viewer = None
77
+ from ._image_widget import _img_to_rgb
78
+
79
+ def transform_image ():
80
+ image_slice1 = _img_to_rgb (viewer .get_view_slice (), colormap = colormap1 , display_min = display_min1 , display_max = display_max1 ).copy ()
81
+ image_slice2 = _img_to_rgb (viewer .get_view_slice (image2 ), colormap = colormap2 , display_min = display_min2 , display_max = display_max2 )
82
+ blend_value = blend_slider .value / 100
83
+ blended_image = (1 - blend_value ) * image_slice1 + blend_value * image_slice2
84
+ return blended_image
85
+
86
+ viewer = _SliceViewer (image1 , continuous_update = continuous_update , zoom_factor = zoom_factor ,
87
+ zoom_spline_order = zoom_spline_order , colormap = colormap1 , display_min = display_min1 ,
88
+ display_max = display_max1 )
89
+
90
+ view = viewer .view
91
+ sliders = viewer .slice_slider
92
+
93
+ # event handler when the user changed something:
94
+ def configuration_updated (event = None ):
95
+ view .data = transform_image ()
96
+
97
+ configuration_updated (None )
98
+
99
+ # connect user interface with event
100
+ blend_slider .observe (configuration_updated )
101
+
102
+ # connect user interface with event
103
+ viewer .observe (configuration_updated )
104
+ result = _no_resize (ipywidgets .VBox ([_no_resize (view ), sliders , blend_slider ]))
105
+ result .update = configuration_updated
106
+ result .viewer = viewer
107
+ return result
0 commit comments