andfanilo / streamlit-drawable-canvas

Do you like Quick, Draw? Well what if you could train/predict doodles drawn inside Streamlit? Also draws lines, circles and boxes over background images for annotation.
https://drawable-canvas.streamlit.app/
MIT License
544 stars 83 forks source link

Canvas resize - No re-rendering #73

Closed Yishaiaz closed 1 year ago

Yishaiaz commented 2 years ago

Hi there. First thanks for a great component! I'm having trouble resizing the canvas during runtime: My canvas background is a PIL image with scaled height and width. Given an integer as a parameter (configured by a user using Streamlit slider component), the canvas's height and width should alter and the canvas re-rendered. I'm using the height & width arguments of the st_canvas constructor to configure the canvas dimensions.

My relevant code:

` image_scaling = st.sidebar.slider('Down scaling image', 1, 10, 5, 1, help='Processing is performed on original') initial_img = Image.fromarray(img_clr) canvas_result = st_canvas( fill_color="rgba(255, 165, 0, 0.3)",
stroke_width=3, stroke_color="#ff1100", background_image=initial_img, update_streamlit=True, height=initial_img.size[1] // image_scaling, width=initial_img.size[0] // image_scaling, drawing_mode=drawing_mode, key="canvas", )

The rest is more code to parse and process the drawn path on the canvas (I don't believe it has anything to do with the issue).

` There are no errors or exceptions generated when updating the 'image_scaling' slider value and it seems that only the canvas buttons (redo/delete/etc.) are re-rendered and their location updated and the image itself does not (see attached image), this causes the buttons to appear in the middle of the image (if the scaling value is increased).

image

Any help? Thanks in advance.

andfanilo commented 2 years ago

Hi @Yishaiaz

Not tested, but I believe the key argument in drawable canvas prevents your canvas from rerendering. You may have to change it whenever you want your canvas to reload or change size (you can put the hash of your image with dimensions as key argument for example so it changes for every image). Can you try this out?

Hope this helps, Fanilo