[英]Streamlit download button not working when trying to download files as zip
[英]Streamlit image download button
st.write("Preview")
#openCV image result cropped_image which is an np array
st.image(cropped_image)
#cropped_image converted to PIL image color
result = Image.fromarray(cropped_image.astype('uint8'), 'RGB')
img = Image.open(result)
btn = st.download_button(
label="Download image",
data=img,
file_name="imagename.png",
mime="image/png")
我想使用 st.download_button 下载图像结果。 我知道我不能使用cropped_image 结果,因为它是一个np 数组。 我将图像数组转换为 PIL 图像,但我不知道如何从此处获取结果图像文件名。 你能给我一些想法如何解决这个问题吗?
Streamlit download_button
期望数据类型为特定类型。 在此处阅读更多信息: https://github.com/streamlit/streamlit/blob/ba46ad297e8816f0d6a3aa910ce67556239f3e6d/lib/streamlit/elements/button.py#L353
您可以使用以下代码段将您的PIL
图像转换为bytes
:
from io import BytesIO
buf = BytesIO()
img.save(buf, format="JPEG")
byte_im = buf.getvalue()
现在您可以使用st.download_button
btn = col.download_button(
label="Download Image",
data=byte_im,
file_name="imagename.png",
mime="image/jpeg",
)
如果您的目录中有图像文件:
st.download_button(label='Download Image',
data= open('yourimage.png', 'rb').read(),
file_name='imagename.png',
mime='image/png')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.