我想使用按钮在我的 Streamlit Web 应用程序中显示图像。
因此,每当用户单击按钮时,图像都必须显示在 Streamlit 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')
Run Code Online (Sandbox Code Playgroud)
小智 5
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")
Run Code Online (Sandbox Code Playgroud)