Image read error

I need to load an image as a numpy array, manipulate it and display the results. But it doesnā€™t work.

Displaying static image like below works

st.image('./data/gallery/AI_generated_meal.jpg')

However, when I try to have an image in a variable, and then display it, it raises an error ā€˜OSError: encoder error -2 when writing image fileā€™.

I tried reading with PIL, skimage, cv2, matplotlibā€¦
Streamlit version 1.2.0
Tried both already existed file and st.file_uploader()
Tried launching with sudo as well.

Some samples of non-working code:

img = cv2.imread('./data/gallery/AI_generated_meal.jpg')
st.image(img)

img = np.array(Image.open('./data/gallery/AI_generated_meal.jpg')).astype(np.float64)
st.image(img)

img = np.array(plt.imread('./data/gallery/AI_generated_meal.jpg'))
st.image(img)

Full Traceback:

OSError: encoder error -2 when writing image file
Traceback:

File "/home/sergey/Projects/streamlit-image-editor/venv/lib/python3.7/site-packages/streamlit/script_runner.py", line 354, in _run_script
    exec(code, module.__dict__)
File "/home/sergey/Projects/streamlit-image-editor/main.py", line 32, in <module>
    st.image(img)
File "/home/sergey/Projects/streamlit-image-editor/venv/lib/python3.7/site-packages/streamlit/elements/image.py", line 128, in image
    output_format,
File "/home/sergey/Projects/streamlit-image-editor/venv/lib/python3.7/site-packages/streamlit/elements/image.py", line 371, in marshall_images
    image, width, clamp, channels, output_format, image_id
File "/home/sergey/Projects/streamlit-image-editor/venv/lib/python3.7/site-packages/streamlit/elements/image.py", line 272, in image_to_url
    data = _np_array_to_bytes(data, output_format=output_format)
File "/home/sergey/Projects/streamlit-image-editor/venv/lib/python3.7/site-packages/streamlit/elements/image.py", line 181, in _np_array_to_bytes
    return _PIL_to_bytes(img, format)
File "/home/sergey/Projects/streamlit-image-editor/venv/lib/python3.7/site-packages/streamlit/elements/image.py", line 167, in _PIL_to_bytes
    image.save(tmp, format=format, quality=quality)
File "/home/sergey/Projects/streamlit-image-editor/venv/lib/python3.7/site-packages/PIL/Image.py", line 2240, in save
    save_handler(self, fp, filename)
File "/home/sergey/Projects/streamlit-image-editor/venv/lib/python3.7/site-packages/PIL/JpegImagePlugin.py", line 782, in _save
    ImageFile._save(im, fp, [("jpeg", (0, 0) + im.size, 0, rawmode)], bufsize)
File "/home/sergey/Projects/streamlit-image-editor/venv/lib/python3.7/site-packages/PIL/ImageFile.py", line 523, in _save
    raise OSError(f"encoder error {s} when writing image file") from exc

I also noticed that using

st.write(img)

I get

[[[0 0 0]
  [0 0 0]
  [0 0 0]
  ...
  [0 0 0]
  [0 0 0]
  [0 0 0]]

 [[0 0 0]
  [0 0 0]
  [0 0 0]
  ...

While the same code in jupyter notebook works properly

[[[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [218, 222, 223],
        [218, 222, 223],
        [218, 222, 223]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [218, 222, 223],
        [218, 222, 223],
        [218, 222, 223]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [218, 222, 223],
        [218, 222, 223],
        [218, 222, 223]],

       ...,

So, downgrading to version 1.0.0 works. I didnā€™t find other solution

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.