🤠 Image Selector
Submitted by Arnaud Miribel
Summary
Allows users to select an area within an image, using a lasso or a bounding box.
Functions
image_selector
Show the image, and enable the user to select an area in the image using the provided selection type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
Image | str | ndarray
|
Original image. Can be a PIL object, or path to local file, or URL, or NumPy array |
required |
selection_type |
Literal[["lasso", "box"]
|
Selection type |
'box'
|
key |
str
|
Key for the st.plotly_chart component. This needs to be unique
for each instance of |
'image-selector'
|
width |
int
|
Width of the image container. Defaults to 300. |
300
|
height |
int
|
Height of the image container. Defaults to 300. |
300
|
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
Selection coordinates |
Source code in src/streamlit_extras/image_selector/__init__.py
Import:
- You should add this to the top of your .py file
show_selection
Shows the image selection
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
Image | str | ndarray
|
Original image. Can be a PIL object, or path to local file, or URL, or NumPy array |
required |
selection |
dict
|
Selection coordinates, output of |
required |
Source code in src/streamlit_extras/image_selector/__init__.py
Import:
- You should add this to the top of your .py file
Examples
example
def example():
response = requests.get(
"https://images.pexels.com/photos/45201/kitty-cat-kitten-pet-45201.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500"
)
image = Image.open(BytesIO(response.content))
selection_type = st.radio(
"Selection type", ["lasso", "box"], index=0, horizontal=True
)
selection = image_selector(image=image, selection_type=selection_type)
if selection:
st.json(selection, expanded=False)
show_selection(image, selection)