【问题标题】:How to display image from a folder using button with Streamlit?如何使用带有 Streamlit 的按钮显示文件夹中的图像?
【发布时间】:2020-11-10 15:25:46
【问题描述】:

我想使用按钮在我的流光网络应用中显示图像。

因此,每当用户单击按钮时,图像都必须显示在流光网络应用上。

下面给出的代码:

feature_choice2 = st.sidebar.multiselect("Plot Size", task2)
if st.button('Find Blueprint'):
    if feature_choice2 == '3-marla':
        imagee = cv2.imread('Floor_plans/3-marla.png')
        cv2.imshow('Image', imagee)
        st.image(imagee, caption='3 marla plot')

【问题讨论】:

    标签: python streamlit


    【解决方案1】:

    Streamlit 不会以自然形式上传图像,因此必须将其转换为数组然后使用它。希望这会有所帮助:

    import cv2
    import numpy as np
    import streamlit as st
    
    uploaded_file = st.file_uploader("Choose a image file", type="jpg")
    
    if uploaded_file is not None:
        # Convert the file to an opencv image.
        file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
        opencv_image = cv2.imdecode(file_bytes, 1)
    
        # Now do something with the image! For example, let's display it:
        st.image(opencv_image, channels="BGR")
    

    【讨论】:

      【解决方案2】:

      cv2.imshow 在流光显示图像时不起作用,因为它会打开另一个单独的窗口。

      除非您打算对图像本身执行任何操作,否则

      from PIL import Image
      import streamlit as st
      
      feature_choice2 = st.sidebar.multiselect("Plot Size", task2)
      if st.button('Find Blueprint'):
          if feature_choice2 == '3-marla':
              image = Image.open('./Floor_plans/3-marla.png')
              st.image(image, caption='3 marla plot',use_column_width=True)
      

      注意: 为此,您的目录结构应该是

      |- app.py # Your Streamlit Script
      |- Floor_plans
         |- 3-marla.png
      

      【讨论】:

        【解决方案3】:

        也许这可以像我的一个 instafilter open-cv 应用程序那样更有效地工作。

        from PIL import Image
        import numpy as np 
        import streamlit as st 
        
        # Function to Read and Manupilate Images
        def load_image(img):
            im = Image.open(img)
            image = np.array(im)
            return image
        
        # Uploading the File to the Page
        uploadFile = st.file_uploader(label="Upload image", type=['jpg', 'png'])
        
        # Checking the Format of the page
        if uploadFile is not None:
            # Perform your Manupilations (In my Case applying Filters)
            img = load_image(uploadFile)
            st.image(img)
            st.write("Image Uploaded Successfully")
        else:
            st.write("Make sure you image is in JPG/PNG Format.")
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-11-28
          • 2023-03-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多