🔀 Image Compare Slider
Submitted by Lukas Masuch
Summary
Compare two images with an interactive slider overlay.
Functions
image_compare_slider
Display an interactive image comparison slider.
Overlays two images with a draggable divider that reveals portions of each. Useful for before/after comparisons, quality comparisons, and visual diffs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image1
|
ImageLike
|
The first (left or top) image. Supports URLs (str), file paths (str or Path), PIL images, numpy arrays, or raw bytes/BytesIO. |
required |
image2
|
ImageLike
|
The second (right or bottom) image. Same formats as image1. |
required |
label1
|
str | None
|
Optional label displayed over the first image. |
None
|
label2
|
str | None
|
Optional label displayed over the second image. |
None
|
position
|
float
|
Initial slider position as a value between 0 and 1. Default is 0.5. |
0.5
|
portrait
|
bool
|
If True, slider moves vertically (top/bottom comparison). Default is False (horizontal left/right comparison). |
False
|
height
|
Literal['content'] | int
|
Component height. "content" (default) auto-sizes based on image aspect ratio. Integer sets fixed pixel height. |
'content'
|
width
|
Literal['stretch', 'content'] | int
|
Component width. "stretch" (default) fills container width. "content" sizes to fit content. Integer sets fixed pixel width. |
'stretch'
|
on_change
|
Literal['ignore', 'rerun'] | Callable[[], None]
|
Controls behavior when the slider position changes. "ignore" (default): No rerun, returns None. "rerun": Triggers a script rerun, returns the current position. Callable: Calls the provided callback function, returns the current position. |
'ignore'
|
key
|
str | None
|
Unique key for this component instance. |
None
|
Returns:
| Type | Description |
|---|---|
float | None
|
Current slider position as a value between 0 and 1 when on_change is |
float | None
|
"rerun" or a callable. Returns None when on_change is "ignore". |
Raises:
| Type | Description |
|---|---|
StreamlitAPIException
|
If position is not between 0 and 1. |
Example
Basic comparison:
With labels:
Vertical comparison:
Source code in src/streamlit_extras/image_compare_slider/__init__.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | |
Import:
- You should add this to the top of your .py file
Examples
example_basic
def example_basic() -> None:
"""Basic before/after comparison."""
st.write("### Basic Comparison")
st.write("Drag the slider to compare the two images.")
image_compare_slider(
"https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=800",
"https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=800&sat=-100",
label1="Color",
label2="Grayscale",
key="basic_compare",
)
st.write("By default, the slider doesn't trigger reruns.")
example_portrait
def example_portrait() -> None:
"""Vertical (portrait) comparison mode."""
st.write("### Portrait Mode")
st.write("Vertical slider for top/bottom comparison.")
image_compare_slider(
"https://images.unsplash.com/photo-1682687220742-aba13b6e50ba?w=600",
"https://images.unsplash.com/photo-1682687220742-aba13b6e50ba?w=600&blur=10",
label1="Sharp",
label2="Blurred",
portrait=True,
key="portrait_compare",
)
example_custom_position
def example_custom_position() -> None:
"""Custom starting position."""
st.write("### Custom Start Position")
start_pos = st.slider("Starting position", 0.0, 1.0, 0.25, key="start_pos_slider")
image_compare_slider(
"https://images.unsplash.com/photo-1469474968028-56623f02e42e?w=800",
"https://images.unsplash.com/photo-1469474968028-56623f02e42e?w=800&con=2",
label1="Normal",
label2="High Contrast",
position=start_pos,
key="custom_position",
)
example_track_position
def example_track_position() -> None:
"""Track slider position with on_change."""
st.write("### Track Position")
st.write("Use `on_change='rerun'` to get the current slider position.")
position = image_compare_slider(
"https://images.unsplash.com/photo-1682687220742-aba13b6e50ba?w=600",
"https://images.unsplash.com/photo-1682687220742-aba13b6e50ba?w=600&blur=10",
label1="Sharp",
label2="Blurred",
on_change="rerun",
key="track_position",
)
assert position is not None
st.write(f"Slider position: **{position:.2f}**")