【问题标题】:Is there a way to get the streamlit canvas image data to show up in the terminal有没有办法让流光画布图像数据显示在终端中
【发布时间】:2021-12-25 16:13:10
【问题描述】:

这是我的第一个 python 项目,其中一部分涉及电子签名。我一直在尝试一段时间来找到一种方法来将绘制的任何图像保存为文件。问题是我似乎无法获得有用的信息,甚至无法开始在 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)

无论我在画布上画什么,我总是在终端返回None。也许canvas_result.image_data 并没有按我的预期工作。但是,如果是这样的话,我什至不知道从这里去哪里。

【问题讨论】:

    标签: python streamlit


    【解决方案1】:

    首先 canvas_result.image_data 返回画布中的图像,您可以使用以下简单代码进行测试:

    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()
    

    现在为了保存图片可以使用OpenCV,代码:

    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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-23
      • 1970-01-01
      • 2019-11-26
      • 2010-09-07
      • 1970-01-01
      • 2012-07-18
      • 2014-07-23
      • 2013-10-22
      相关资源
      最近更新 更多