streamlit-cropper
streamlit-cropper copied to clipboard
Unexpected behavior with default_coords in st_cropper
Hello streamlit-cropper team,
I noticed some unexpected behavior, when I was trying to make the selection box persistant.
I used the return_Type="both"
option in the st_cropper
function to get the coordinates, than stored them is an st_session_state
variable, with the goal to use them in the default_coords
option.
I also had to do some conversions to match the output coordinates with the input coordinates in convert_coordinates_dict_to_tuple
.
When I do this the selection box seems to be stuck every second time I move the box. I expected the cropper function to take the stored coordinates as the coordinates to place the box. So I'm kind of stuck here and have no idea, why it works every second time.
Background: I do some manipulations on the image thats meant to be cropped. When the image changes, the st_cropper
functions gets called again and resets the coordinates.
Also I'm kind if new to programming, so it might be that I just overlooked something here. Also this is my first Issue on GitHub. So just let me know if I could improve.
Thank you for your support.
Here is the code example to reproduce the behavior:
import streamlit as st
from streamlit_cropper import st_cropper
from PIL import Image
def convert_coordinates_dict_to_tuple(data):
left = data['left'] + 2
top = data['top'] + 2
width = data['width']
height = data['height']
right = left + width + 4
bottom = top + height + 4
return (left, right, top, bottom)
# Upload an image and set some options for demo purposes
st.header("Cropper Demo")
image = st.file_uploader("Upload Your Image", type=['jpg', 'png', 'jpeg'], accept_multiple_files=False)
if image:
img_file = Image.open(image)
# check if coordinates already available, if not use cropper box algorythm
if 'coordinates' in st.session_state:
print("coordinates already available")
print("before moving",st.session_state.coordinates)
cropped_img, st.session_state.coordinates= st_cropper(img_file, realtime_update=True, box_color='#0000FF', return_type="both", default_coords=st.session_state.coordinates)
st.session_state.coordinates = convert_coordinates_dict_to_tuple(st.session_state.coordinates)
print("after moving ", st.session_state.coordinates)
else:
print("no coordinates")
cropped_img, st.session_state.coordinates = st_cropper(img_file, realtime_update=True, box_color='#0000FF', return_type="both")
st.session_state.coordinates = convert_coordinates_dict_to_tuple(st.session_state.coordinates)
print("new coordinates", st.session_state.coordinates)
st.image(cropped_img)