Groggyboi
Groggyboi

Reputation: 31

Is there a way to get the streamlit canvas image data to show up in the terminal

This is my first python project and a piece of it involves electronic signatures. I've been trying for a while now to find a way to save whatever image is drawn as a file. The problem is it doesn't seem like I can get usable information to even start manipulating in Visual Studio Code.

def signaturefunk ():
    
    st.write("Signature")

    canvas_result = st_canvas(
            fill_color="#eee",
            stroke_width=5,
            stroke_color="black",
            background_color="",
            update_streamlit=False,
            height=200,
            width=700,
            drawing_mode="freedraw",
        )
     
    im = canvas_result.image_data
    print(im)

I always get back None in the terminal no matter what I draw on the canvas. Maybe canvas_result.image_data just doesn't work how I expect it to. However, if that's the case I don't even know where to go from here.

Upvotes: 2

Views: 1680

Answers (1)

RoseGod
RoseGod

Reputation: 1234

First of all canvas_result.image_data returns the image in the canvas, you can test this with this simple code:

import streamlit as st
from streamlit_drawable_canvas import st_canvas

st.title("Signature")

def signaturefunk():
    
    st.write("Canvas")

    canvas_result = st_canvas(
            fill_color="#eee",
            stroke_width=5,
            stroke_color="black",
            background_color="white",
            update_streamlit=False,
            height=200,
            width=700,
            drawing_mode="freedraw",
        )
    
    st.write("Image of the canvs")
    if canvas_result.image_data is not None:
        st.image(canvas_result.image_data)

signaturefunk()

Now in order to save the image you can use OpenCV, the code:

import streamlit as st
from streamlit_drawable_canvas import st_canvas
import cv2

st.title("Signature")

def signaturefunk():
    canvas_result = st_canvas(
            fill_color="#eee",
            stroke_width=5,
            stroke_color="black",
            background_color="white",
            update_streamlit=False,
            height=200,
            width=700,
            drawing_mode="freedraw",
        )
    
    # save image
    if canvas_result.image_data is not None:
        cv2.imwrite(f"img.jpg",  canvas_result.image_data)
    else:
        st.write("no image to save")

signaturefunk()

Upvotes: 1

Related Questions