Pillow icon indicating copy to clipboard operation
Pillow copied to clipboard

MPO encoding then decoding does not work

Open misaka-10032 opened this issue 7 months ago • 1 comments

What did you do?

I encoded a list of image into MPO and decoded them. Decoding failed to return multiple images.

What did you expect to happen?

The MPO plugin should be able to decode the MPO encoded by its own.

What actually happened?

Decoder complains that the file is truncated.

What are your OS, Python and Pillow versions?

  • OS: Linux
  • Python: 3.10.12
  • Pillow: 11.2.1 (I also tried 9.5.0, 10.4.0. Same error)
import io
import PIL.Image

frames = [PIL.Image.new("RGB", (512, 512), c) for c in ["red", "green", "blue"]]
with io.BytesIO() as f:
  frames[0].save(
      f, format="MPO", save_all=True, append_images=frames[1:]
  )
  mpo_bytes = f.getvalue()
with io.BytesIO(mpo_bytes) as f:
  mpo_image = PIL.Image.open(f)
  print(mpo_image.n_frames)

Logs:

> /usr/local/lib/python3.10/dist-packages/PIL/TiffImagePlugin.py:950: UserWarning: Truncated File Read
>  warnings.warn(str(msg))
> /usr/local/lib/python3.10/dist-packages/PIL/JpegImagePlugin.py:890: UserWarning: Image appears to be a malformed MPO file, it will be interpreted as a base JPEG file
>  warnings.warn(
>
> [/usr/local/lib/python3.10/dist-packages/PIL/JpegImagePlugin.py](https://localhost:8080/#) in __getattr__(self, name)
>    398             deprecate(name, 12)
>    399             return getattr(self, "_" + name)
> --> 400         raise AttributeError(name)
>    401 
>    402     def __getstate__(self) -> list[Any]:
>
> AttributeError: n_frames

The same works for GIF, but fails for MPO.

misaka-10032 avatar May 25 '25 22:05 misaka-10032

I've created #8979 to resolve this.

radarhere avatar May 27 '25 11:05 radarhere