-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from ian-fox/click_drag
feat: add click_and_drag option
- Loading branch information
Showing
8 changed files
with
97 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import streamlit as st | ||
from PIL import Image, ImageDraw | ||
from streamlit_image_coordinates import streamlit_image_coordinates | ||
|
||
st.set_page_config( | ||
page_title="Streamlit Image Coordinates: Rectangle Select", | ||
layout="wide", | ||
page_icon="🪟", | ||
) | ||
|
||
"# 🪟 Streamlit Image Coordinates: Rectangle Select" | ||
|
||
"## Click and drag on the image" | ||
|
||
|
||
if "coordinates" not in st.session_state: | ||
st.session_state["coordinates"] = None | ||
|
||
|
||
def get_rectangle_coords( | ||
points: tuple[tuple[int, int], tuple[int, int]], | ||
) -> tuple[int, int, int, int]: | ||
point1, point2 = points | ||
return ( | ||
point1[0], | ||
point1[1], | ||
point2[0], | ||
point2[1], | ||
) | ||
|
||
|
||
with st.echo("below"), Image.open("kitty.jpeg") as img: | ||
draw = ImageDraw.Draw(img) | ||
|
||
if st.session_state["coordinates"]: | ||
coords = get_rectangle_coords(st.session_state["coordinates"]) | ||
draw.rectangle(coords, fill=None, outline="red", width=2) | ||
|
||
cols = st.columns([1, 1, 4]) | ||
with cols[0]: | ||
value = streamlit_image_coordinates(img, key="rectangle", click_and_drag=True) | ||
|
||
if value is not None: | ||
point1 = value["x1"], value["y1"] | ||
point2 = value["x2"], value["y2"] | ||
|
||
if point1 != point2 and st.session_state["coordinates"] != (point1, point2): | ||
st.session_state["coordinates"] = (point1, point2) | ||
st.rerun() | ||
|
||
# Enlarge the rectangle selected between point1 and point2 | ||
if st.session_state["coordinates"]: | ||
coords = get_rectangle_coords(st.session_state["coordinates"]) | ||
new_image = img.crop(coords) | ||
new_image = new_image.resize( | ||
(int(new_image.width * 1.5), int(new_image.height * 1.5)) | ||
) | ||
with cols[1]: | ||
st.image(new_image, use_column_width=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
|
||
setuptools.setup( | ||
name="streamlit-image-coordinates", | ||
version="0.1.7", | ||
version="0.1.8", | ||
author="Zachary Blackwood", | ||
author_email="[email protected]", | ||
description=( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,7 @@ | |
width: 100%; | ||
height: auto; | ||
} | ||
|
||
img { | ||
user-select: none; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters