Skip to content

PyEncoder.encode_to_file() writes buf[status:], where status is the number of encoded bytes. An encoder returning a complete buffer consequently writes nothing #10096

Description

@AGFACBNNR
import tempfile
from PIL import ImageFile

class Encoder(ImageFile.PyEncoder):
    def encode(self, bufsize):
        return 3, 1, b"ABC"

with tempfile.TemporaryFile() as fp:
    Encoder("RGB").encode_to_file(fp.fileno(), 65536)
    fp.seek(0)
    print(fp.read())  # b''; expected b'ABC'

The same encoder through ImageFile._save() writes b'ABC' to BytesIO, but an empty buffer to a real file.

Write the encoded prefix at ImageFile.py:935, rather than slicing it off. This affects non-push Python encoders; the bundled QOI/BLP encoders use a different path.

Reproduced on Pillow 13.0.0.dev0, Python 3.13.15, with the original Python sources and C extension built from this checkout.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions