Skip to content

Commit

Permalink
Merge pull request #6817 from radarhere/mpo_exif
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Dec 23, 2022
2 parents 0934c25 + 9898613 commit e0eca1f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
5 changes: 4 additions & 1 deletion Tests/test_file_mpo.py
Expand Up @@ -80,7 +80,10 @@ def test_app(test_file):

@pytest.mark.parametrize("test_file", test_files)
def test_exif(test_file):
with Image.open(test_file) as im:
with Image.open(test_file) as im_original:
im_reloaded = roundtrip(im_original, save_all=True, exif=im_original.getexif())

for im in (im_original, im_reloaded):
info = im._getexif()
assert info[272] == "Nintendo 3DS"
assert info[296] == 2
Expand Down
14 changes: 11 additions & 3 deletions src/PIL/MpoImagePlugin.py
Expand Up @@ -52,14 +52,22 @@ def _save_all(im, fp, filename):
_save(im, fp, filename)
return

mpf_offset = 28
offsets = []
for imSequence in itertools.chain([im], append_images):
for im_frame in ImageSequence.Iterator(imSequence):
if not offsets:
# APP2 marker
im.encoderinfo["extra"] = (
im_frame.encoderinfo["extra"] = (
b"\xFF\xE2" + struct.pack(">H", 6 + 82) + b"MPF\0" + b" " * 82
)
exif = im_frame.encoderinfo.get("exif")
if isinstance(exif, Image.Exif):
exif = exif.tobytes()
im_frame.encoderinfo["exif"] = exif
if exif:
mpf_offset += 4 + len(exif)

JpegImagePlugin._save(im_frame, fp, filename)
offsets.append(fp.tell())
else:
Expand All @@ -79,11 +87,11 @@ def _save_all(im, fp, filename):
mptype = 0x000000 # Undefined
mpentries += struct.pack("<LLLHH", mptype, size, data_offset, 0, 0)
if i == 0:
data_offset -= 28
data_offset -= mpf_offset
data_offset += size
ifd[0xB002] = mpentries

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

Expand Down

0 comments on commit e0eca1f

Please sign in to comment.