Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can't read an image uploaded with st.file_uploader() #884

Closed
amineHY opened this issue Dec 20, 2019 · 5 comments
Closed

can't read an image uploaded with st.file_uploader() #884

amineHY opened this issue Dec 20, 2019 · 5 comments
Labels
type:bug Something isn't working

Comments

@amineHY
Copy link

amineHY commented Dec 20, 2019

I get my hand on the new feature that we all have been waiting for.

I tested it to upload an image and read it with OpenCV (cv.VideoCapture) but unfortunately, I encounter an error. The type of the uploaded image is <_io.BytesIO object at 0x7f4c7adc7db0>

Error message

TypeError: an integer is required (got type _io.BytesIO)
Traceback:
  File "/usr/local/lib/python3.6/site-packages/streamlit/ScriptRunner.py", line 324, in _run_script
    exec(code, module.__dict__)
  File "/workspaces/inveesion/main.py", line 767, in <module>
    cap = Config().input_output_data(guiParam)
  File "/workspaces/inveesion/main.py", line 508, in input_output_data
    cap = cv.VideoCapture(file_path)
@amineHY amineHY added the type:bug Something isn't working label Dec 20, 2019
@robmarkcole
Copy link

robmarkcole commented Dec 21, 2019

The return type of file_uploader is BytesIO, I think all that is required is some docs on how to load this with openCV/PIL. I have tried a few approaches with PIL but it appears you need the image shape...?

@robmarkcole
Copy link

robmarkcole commented Dec 21, 2019

Edit - just noticed this thread with solution https://discuss.streamlit.io/t/png-bytes-io-numpy-conversion-using-file-uploader/1409/2

Suggested example for the docs:

import streamlit as st
from PIL import Image
import numpy as np

img_file_buffer = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])

image = Image.open(img_file_buffer)
img_array = np.array(image)

if image is not None:
    st.image(
        image,
        caption=f"You amazing image has shape {img_array.shape[0:2]}",
        use_column_width=True,
    )

@kantuni
Copy link
Collaborator

kantuni commented Jan 16, 2020

Dear @robmarkcole,

You need to check that img_file_buffer is not None before trying to Image.open() it. Otherwise, you will get an AttributeError.

import streamlit as st
import numpy as np
from PIL import Image

img_file_buffer = st.file_uploader("Upload an image")
if img_file_buffer is not None:
    image = Image.open(img_file_buffer)
    img_array = np.array(image) # if you want to pass it to OpenCV
    st.image(image, caption="The caption", use_column_width=True)

P.S. Thanks for the suggested example!

@kantuni kantuni closed this as completed Jan 16, 2020
@kantuni kantuni reopened this Jan 16, 2020
@kantuni kantuni closed this as completed Jan 17, 2020
@emacarayan
Copy link

emacarayan commented Jan 27, 2020

Hi, this is very helpful. Unfortunately, I have an audio file to upload instead of an image and I need streamlit to open it and display as an audio file. Ive written this code but it keeps on returning an error:

with st.file_uploader(label="Upload an audio file", type=["ogg", "wav"]) as input:
    if input == None:
        st.warning('No file selected')
    else:
        audio_file=open(input)
        audio_bytes= audio_file.read(input)
        st.audio(audio_bytes, format='audio/ogg')

Error: TypeError: expected str, bytes or os.PathLike object, not _io.BytesIO

Is there a better way for me to go around the type error issue in streamlit so I can play the uploaded audio file by the user?

Thank you so much!

@nthmost
Copy link
Contributor

nthmost commented Jan 27, 2020

Hi @emacarayan ,

I'm working on an improvement to the audio/video/image processors that will allow you to do exactly what you're trying to do. It should be out with the next release.

For now, try doing this to read the bytes out of the file into the audio processor:

st.audio(audio_file.getvalue(), format="audio/ogg")

Thanks for playing with this new feature. (And also, thanks for using OGG! 😸 )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants