Skip to content

save to bytes and got this error: io.UnsupportedOperation: fileno #8902

Description

@CoinCheung

What did you do?

save an jpeg image to bytes string.

What did you expect to happen?

Obtain the string successfully.

What actually happened?

Script crashes

What are your OS, Python and Pillow versions?

  • OS: ubuntu 22.04
  • Python: 3.10
  • Pillow: 11.0.0
--------------------------------------------------------------------
Pillow 11.0.0
Python 3.10.15 (main, Oct  3 2024, 07:27:34) [GCC 11.2.0]
--------------------------------------------------------------------
Python executable is /opt/miniconda3/envs/py310/bin/python3
System Python files loaded from /opt/miniconda3/envs/py310
--------------------------------------------------------------------
Python Pillow modules loaded from /opt/miniconda3/envs/py310/lib/python3.10/site-packages/PIL
Binary Pillow modules loaded from /opt/miniconda3/envs/py310/lib/python3.10/site-packages/PIL
--------------------------------------------------------------------
--- PIL CORE support ok, compiled for 11.0.0
--- TKINTER support ok, loaded 8.6
--- FREETYPE2 support ok, loaded 2.12.1
--- LITTLECMS2 support ok, loaded 2.12
--- WEBP support ok, loaded 1.3.2
--- JPEG support ok, compiled for 9.0
--- OPENJPEG (JPEG2000) support ok, loaded 2.5.2
--- ZLIB (PNG/ZIP) support ok, loaded 1.2.13
--- LIBTIFF support ok, loaded 4.5.1
*** RAQM (Bidirectional Text) support not installed
*** LIBIMAGEQUANT (Quantization method) support not installed
*** XCB (X protocol) support not installed
--------------------------------------------------------------------```
import PIL
from PIL import Image
import io
import time

impth = 'n12762896_263.JPEG'
img = Image.open(impth)

buf = io.BytesIO()
img.save(buf, format='jpeg', quality=90, subsampling=0, optimize=True)

Error message is:

  File "/opt/miniconda3/envs/py310/lib/python3.10/site-packages/PIL/Image.py", line 2605, in save
    save_handler(self, fp, filename)
  File "/opt/miniconda3/envs/py310/lib/python3.10/site-packages/PIL/JpegImagePlugin.py", line 843, in _save
    ImageFile._save(
  File "/opt/miniconda3/envs/py310/lib/python3.10/site-packages/PIL/ImageFile.py", line 558, in _save
    _encode_tile(im, fp, tile, bufsize, None, exc)
  File "/opt/miniconda3/envs/py310/lib/python3.10/site-packages/PIL/ImageFile.py", line 593, in _encode_tile
    raise _get_oserror(errcode, encoder=True) from exc
OSError: broken data stream when writing image file

This only happens with certain images, here is an image that can cause this error:

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions