question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
kantunicommented, 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!

5reactions
robmarkcolecommented, 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,
    )

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't read an image uploaded with st.file_uploader() - Streamlit
I tested it to upload an image and read it with OpenCV (cv.VideoCapture) but unfortunately, I encounter an error. The type of the...
Read more >
getting an error from streamlit file_uploader - Stack Overflow
i can't figure in which file type the streamlit is uploading the file. Please help me with how I can upload a custom...
Read more >
Working with File Uploads in Streamlit Python - JCharisTech
To work with the file uploads you will have to use the st.file_uploader() function. Let us see how the st.file_uploader() functions works.
Read more >
Python Streamlit: Uploading and Using Files - Level Up Coding
Streamlit provides a file_uploader , which is accessible with st.file_uploader and looks like this: Streamlit file picker. Code for the file picker, ...
Read more >
Working with File Uploads In Streamlit Python - YouTube
In this indepth tutorial we will be working with the file uploads feature of streamlit - how to process Images,PDF,Docx,Txt,CSV etc.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found