簡體   English   中英

如何使用帶有 Streamlit 的按鈕顯示文件夾中的圖像?

[英]How to display image from a folder using button with Streamlit?

我想使用按鈕在我的流光 web 應用程序中顯示圖像。

因此,每當用戶單擊按鈕時,圖像必須顯示在流線型 web 應用程序上。

下面給出的代碼:

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

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

cv2.imshow在流光顯示圖像時不起作用,因為它會打開另一個單獨的 window。

除非您打算對圖像本身執行任何操作,

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

也許這可以像我的一個 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.")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM