1

我想使用按钮在我的流线型 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')
4

3 回答 3

4

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")
于 2020-08-16T06:28:41.070 回答
2

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

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

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
于 2021-02-09T18:37:58.217 回答
1

也许这可以像我的一个 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.")
于 2021-03-20T07:11:18.623 回答