Skip to content

Conversation

@radarhere
Copy link
Member

@radarhere radarhere commented May 27, 2025

Resolves #8977

With each new frame that is saved to an MPO image, an offset is added

offsets.append(fp.tell())
else:
im_frame.save(fp, "JPEG")
offsets.append(fp.tell() - offsets[-1])

leading to a longer mpentries
mpentries += struct.pack("<LLLHH", mptype, size, data_offset, 0, 0)

meaning the IFD becomes longer
ifd[0xB002] = mpentries

We rewind and replace the placeholder data with the IFD

fp.seek(mpf_offset)
fp.write(b"II\x2a\x00" + o32le(8) + ifd.tobytes(8))
fp.seek(0, os.SEEK_END)

But the placeholder data only ever has the same number of bytes, regardless of the number of frames.

b"\xff\xe2" + struct.pack(">H", 6 + 82) + b"MPF\0" + b" " * 82

This PR increases the length of the placeholder data as the number of frames increase.

@radarhere radarhere changed the title Fixed saving MPO with more than one appended image Fix saving MPO with more than one appended image Jun 14, 2025
radarhere and others added 2 commits June 28, 2025 01:20
Co-authored-by: Hugo van Kemenade <[email protected]>
@radarhere radarhere mentioned this pull request Jun 28, 2025
@hugovk hugovk merged commit d263e3b into python-pillow:main Jun 28, 2025
57 checks passed
@hugovk
Copy link
Member

hugovk commented Jun 28, 2025

Thanks!

@radarhere radarhere deleted the mpo branch June 28, 2025 11:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MPO encoding then decoding does not work

2 participants