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.
The same encoder through
ImageFile._save()writesb'ABC'toBytesIO, 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.