首页 > 解决方案 > 如何使用带有 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')

标签: pythonstreamlit

解决方案


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

推荐阅读