# Image upload problem

**URL:** <https://discuss.streamlit.io/t/image-upload-problem/4810>\
**Category:** Using Streamlit\
**Tags:** file-upload\
**Created:** [August 9, 2020, 1:24am UTC](https://discuss.streamlit.io/t/image-upload-problem/4810 "2020-08-09T01:24:06Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![BassamAbdeltwab](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/bassamabdeltwab/32/2762_2.png) [@BassamAbdeltwab](https://discuss.streamlit.io/u/BassamAbdeltwab)\
**Post date:** [August 9, 2020, 1:24am UTC](https://discuss.streamlit.io/t/image-upload-problem/4810/1 "2020-08-09T01:24:06Z")

</div>

Hello community team.  
I have a problem with file uploader.  
I use file upload to upload images, show that image on the app.  
then I use that image as input to my Keras model to make a prediction.  
the code was working fine, but now it stopped and get an error message as below

```auto
" TypeError: expected str, bytes, or os.PathLike object, not _io.BytesIO " 

```

my code in details is tw oclasses as below :  
1- classify.py

```auto
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.applications.vgg16 import preprocess_input
from keras.applications.vgg16 import decode_predictions
from keras.applications.vgg16 import VGG16

def predict(image1): 
    model = VGG16()
    image = load_img(image1, target_size=(224, 224))
    # convert the image pixels to a numpy array
    image = img_to_array(image)
    # reshape data for the model
    image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
    # prepare the image for the VGG model
    image = preprocess_input(image)
    # predict the probability across all output classes
    yhat = model.predict(image)
    # convert the probabilities to class labels
    label = decode_predictions(yhat)
    # retrieve the most likely result, e.g. highest probability
    label = label[0][0]
    return label 

```

2- upload.py

```auto
import streamlit as st 
from PIL import Image
from classify import predict

st.title("Upload + Classification Example")

uploaded_file = st.file_uploader("Choose an image...", type="jpg")
if uploaded_file is not None:
    image = Image.open(uploaded_file)
    st.image(image, caption='Uploaded Image.', use_column_width=True)
    st.write("")
    st.write("Classifying...")
    label = predict(uploaded_file)
    st.write('%s (%.2f%%)' % (label[1], label[2]*100))

```

the error in details :

```auto
TypeError: expected str, bytes or os.PathLike object, not _io.BytesIO
Traceback:

```

```auto
File "d:\users\bassam\anaconda3\lib\site-packages\streamlit\ScriptRunner.py", line 319, in _run_script
    exec(code, module. __dict__ )
File "C:\Users\Bassam\upload.py", line 38, in <module>
    label = predict(uploaded_file)
File "C:\Users\Bassam\classify1.py", line 9, in predict
    image = load_img(image1, target_size=(224, 224))
File "d:\users\bassam\anaconda3\lib\site-packages\keras_preprocessing\image\utils.py", line 113, in load_img with open(path, 'rb') as f:

```

---

<div class="post-metadata">

**Author:** ![randyzwitch](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/randyzwitch/32/58772_2.png) [@randyzwitch](https://discuss.streamlit.io/u/randyzwitch)\
**Post date:** [August 10, 2020, 1:11pm UTC](https://discuss.streamlit.io/t/image-upload-problem/4810/2 "2020-08-10T13:11:42Z")

</div>

Hi @BassamAbdeltwab, welcome to the Streamlit community. Please see this thread, which details working with the file\_uploader:

> [@How to process .apk file uploaded using fileuploader?](https://discuss.streamlit.io/t/how-to-process-apk-file-uploaded-using-fileuploader/4604/2):
>
> Hi @allenpbiju, welcome to the Streamlit community! st.file\_uploader returns a BytesIO object, which is a buffer, which isn’t one of the types that function accepts (as the error message indicates). To get the bytes from a BytesIO object, you need to [read the buffer](https://docs.python.org/3/library/io.html#io.RawIOBase.read): a = APK(uploaded\_apk.read())

---

<div class="post-metadata">

**Author:** ![doubianimehdi](https://avatars.discourse-cdn.com/v4/letter/d/f1d935/32.png) [@doubianimehdi](https://discuss.streamlit.io/u/doubianimehdi)\
**Post date:** [August 30, 2020, 5:00pm UTC](https://discuss.streamlit.io/t/image-upload-problem/4810/3 "2020-08-30T17:00:53Z")

</div>

Hi, I have the same problem and it was working fine until recently ! something changed with fileuploader but I can’t understand what …  
TypeError: expected str, bytes or os.PathLike object, not \_io.BytesIO

Traceback:

```auto
File "c:\programdata\anaconda3\envs\virtenv\lib\site-packages\streamlit\script_runner.py", line 324, in _run_script
    exec(code, module. __dict__ )
File "C:\OpenClassRooms\projet 6\upload.py", line 17, in <module>
    label, probability = predict(uploaded_file)
File "C:\OpenClassRooms\projet 6\classify_120_streamlit.py", line 22, in predict
    image = load_img(image1, target_size=(299, 299))
File "c:\programdata\anaconda3\envs\virtenv\lib\site-packages\tensorflow\python\keras\preprocessing\image.py", line 301, in load_img
    target_size=target_size, interpolation=interpolation)File "c:\programdata\anaconda3\envs\virtenv\lib\site-packages\keras_preprocessing\image\utils.py", line 113, in load_img
    with open(path, 'rb') as f:

```

---

<div class="post-metadata">

**Author:** ![randyzwitch](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/randyzwitch/32/58772_2.png) [@randyzwitch](https://discuss.streamlit.io/u/randyzwitch)\
**Post date:** [August 31, 2020, 12:52pm UTC](https://discuss.streamlit.io/t/image-upload-problem/4810/4 "2020-08-31T12:52:48Z")

</div>

`file_uploader` returns a BytesIO buffer, so you need to read the bytes before passing it to another function.

```python
upload = file_uploader(...)

data = upload.read()

```

---

<div class="post-metadata">

**Author:** ![sainivedh](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/sainivedh/32/3343_2.png) [@sainivedh](https://discuss.streamlit.io/u/sainivedh)\
**Post date:** [October 2, 2020, 7:50pm UTC](https://discuss.streamlit.io/t/image-upload-problem/4810/5 "2020-10-02T19:50:37Z")

</div>

This isn’t working, getting the same error while reading images

 ![Screenshot (82)](https://global.discourse-cdn.com/streamlit/original/2X/8/8dd06a0ec56b530872390e27675a7ec0b4007dd6.jpeg)

---

<div class="post-metadata">

**Author:** ![ash2shukla](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/ash2shukla/32/2521_2.png) [@ash2shukla](https://discuss.streamlit.io/u/ash2shukla)\
**Post date:** [October 2, 2020, 9:58pm UTC](https://discuss.streamlit.io/t/image-upload-problem/4810/6 "2020-10-02T21:58:43Z")

</div>

Hi @sainivedh, As @randyzwitch has already pointed out the file\_uploader returns a BytesIO object so you need to wrap it in a TemporaryFile and then feed it to the load\_img function.

I have tried this on my system and it works.

```python
from keras.preprocessing.image import load_img

import streamlit as st

from tempfile import NamedTemporaryFile

st.set_option('deprecation.showfileUploaderEncoding', False)

buffer = st.file_uploader("Image here pl0x")
temp_file = NamedTemporaryFile(delete=False)
if buffer:
    temp_file.write(buffer.getvalue())
    st.write(load_img(temp_file.name))

```

However, I would suggest using PIL directly, check this issue on keras, [https://github.com/keras-team/keras/issues/11684](https://github.com/keras-team/keras/issues/11684)  
And this comment.  
[https://github.com/keras-team/keras/issues/11684#issuecomment-440175840](https://github.com/keras-team/keras/issues/11684#issuecomment-440175840)

Hope it helps. 🙂

---

<div class="post-metadata">

**Author:** ![Snehit\_Vaddi](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/snehit_vaddi/32/2556_2.png) [@Snehit\_Vaddi](https://discuss.streamlit.io/u/Snehit_Vaddi)\
**Post date:** [March 16, 2021, 6:19am UTC](https://discuss.streamlit.io/t/image-upload-problem/4810/7 "2021-03-16T06:19:23Z")

</div>

@ash2shukla Have you resolved the problem?  
If yes, could you please share the code you have used. I am trying to do image classification, but getting errors in the prediction of uploaded files using `st.file_uploader()`

---

<div class="post-metadata">

**Author:** ![Raj1411](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/raj1411/32/10280_2.png) [@Raj1411](https://discuss.streamlit.io/u/Raj1411)\
**Post date:** [September 2, 2021, 5:49pm UTC](https://discuss.streamlit.io/t/image-upload-problem/4810/8 "2021-09-02T17:49:18Z")

</div>

just do np.array(file)

---

<div class="post-metadata">

**Author:** ![PNH](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/pnh/32/11505_2.png) [@PNH](https://discuss.streamlit.io/u/PNH)\
**Post date:** [November 3, 2021, 4:02pm UTC](https://discuss.streamlit.io/t/image-upload-problem/4810/9 "2021-11-03T16:02:23Z")

</div>

Thank you very much. Now the error is resolved.

---

<div class="post-metadata">

**Author:** ![Srikanth\_Kansrika](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/srikanth_kansrika/32/13913_2.png) [@Srikanth\_Kansrika](https://discuss.streamlit.io/u/Srikanth_Kansrika)\
**Post date:** [March 5, 2022, 8:19am UTC](https://discuss.streamlit.io/t/image-upload-problem/4810/10 "2022-03-05T08:19:31Z")

</div>

I have used the PIL directly to upload the image for prediction . But unfortunately getting an error \anaconda3\lib\site-packages\PIL\Image.py", line 546, in **getattr**  
raise AttributeError(name)  
AttributeError: read

---

<div class="post-metadata">

**Author:** ![Asuy](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@Asuy](https://discuss.streamlit.io/u/Asuy)\
**Post date:** [April 2, 2025, 10:15am UTC](https://discuss.streamlit.io/t/image-upload-problem/4810/11 "2025-04-02T10:15:45Z")

</div>

Thank you so much, now its clear.
