✂️ Image Crop
Submitted by Lukas Masuch
Summary
Interactive image cropping with adjustable bounds.
Functions
image_crop
Display an interactive image cropping component.
Allows users to select a rectangular region on an image by clicking and dragging. Returns the crop bounds as normalized values (0-1).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
ImageLike
|
The image to crop. Supports URLs (str), file paths (str or Path), PIL images, numpy arrays, or raw bytes/BytesIO. |
required |
aspect
|
float | None
|
Fixed aspect ratio for the crop selection (e.g., 1 for square, 16/9 for landscape). None allows free-form selection. |
None
|
min_width
|
int
|
Minimum crop width in pixels. Default is 10. |
10
|
min_height
|
int
|
Minimum crop height in pixels. Default is 10. |
10
|
initial_crop
|
CropBounds | None
|
Initial crop selection as a CropBounds dict with x, y, width, and height as normalized values (0-1). None shows no initial selection. |
None
|
circular
|
bool
|
If True, displays a circular mask over the selection. The returned bounds are still rectangular. Default is False. |
False
|
rule_of_thirds
|
bool
|
If True, displays composition guideline grid. Default is False. |
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 crop region changes. "rerun" (default): Triggers a script rerun, returns the crop bounds. "ignore": No rerun, always returns None. Callable: Calls the provided callback, returns the crop bounds. |
'rerun'
|
key
|
str | None
|
Unique key for this component instance. |
None
|
Returns:
| Type | Description |
|---|---|
CropBounds | None
|
CropBounds dict with x, y, width, and height as normalized values (0-1) |
CropBounds | None
|
when a selection exists and on_change is not "ignore". Returns None |
CropBounds | None
|
when no selection is active or on_change is "ignore". |
Raises:
| Type | Description |
|---|---|
StreamlitAPIException
|
If parameters are invalid. |
Example
Basic cropping:
Square avatar selection:
Source code in src/streamlit_extras/image_crop/__init__.py
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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | |
Import:
- You should add this to the top of your .py file
Examples
example_basic
def example_basic() -> None:
"""Basic image cropping."""
st.write("### Basic Cropping")
st.write("Click and drag on the image to select a crop region.")
crop = image_crop(
"https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=800",
key="basic_crop",
)
if crop:
st.write(
f"**Selected region:** x={crop['x']:.2f}, y={crop['y']:.2f}, "
f"width={crop['width']:.2f}, height={crop['height']:.2f}"
)
else:
st.write("No region selected yet.")
example_square
def example_square() -> None:
"""Square aspect ratio cropping."""
st.write("### Square Crop")
st.write("Fixed 1:1 aspect ratio for avatar-style selection.")
crop = image_crop(
"https://images.unsplash.com/photo-1682687220742-aba13b6e50ba?w=600",
aspect=1,
initial_crop={"x": 0.25, "y": 0.1, "width": 0.5, "height": 0.5},
key="square_crop",
)
if crop:
st.write(f"**Crop bounds:** {crop}")
example_circular
def example_circular() -> None:
"""Circular crop mask."""
st.write("### Circular Mask")
st.write("Circular display for profile picture selection.")
crop = image_crop(
"https://images.unsplash.com/photo-1469474968028-56623f02e42e?w=800",
aspect=1,
circular=True,
key="circular_crop",
)
if crop:
st.write(f"**Crop bounds:** {crop}")
example_composition
def example_composition() -> None:
"""Composition helper with rule of thirds."""
st.write("### Composition Helper")
st.write("Rule of thirds grid for photo composition.")
crop = image_crop(
"https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=800",
aspect=16 / 9,
rule_of_thirds=True,
key="composition_crop",
)
if crop:
st.write(f"**16:9 crop:** {crop}")