Skip to content

Commit

Permalink
Save interop IFD when converting Exif to bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Jan 4, 2021
1 parent 6f8dab0 commit 8723cb3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Tests/test_image.py
Expand Up @@ -746,9 +746,14 @@ def test_exif_interop(self):
4098: 1704,
}

reloaded_exif = Image.Exif()
reloaded_exif.load(exif.tobytes())
assert reloaded_exif.get_ifd(0xA005) == exif.get_ifd(0xA005)

def test_exif_ifd(self):
im = Image.open("Tests/images/flower.jpg")
exif = im.getexif()
del exif.get_ifd(0x8769)[0xA005]

reloaded_exif = Image.Exif()
reloaded_exif.load(exif.tobytes())
Expand Down
7 changes: 7 additions & 0 deletions src/PIL/Image.py
Expand Up @@ -3368,6 +3368,13 @@ def tobytes(self, offset=8):
for tag, value in self.items():
if tag in [0x8769, 0x8225] and not isinstance(value, dict):
value = self.get_ifd(tag)
if (
tag == 0x8769
and 0xA005 in value
and not isinstance(value[0xA005], dict)
):
value = value.copy()
value[0xA005] = self.get_ifd(0xA005)
ifd[tag] = value
return b"Exif\x00\x00" + head + ifd.tobytes(offset)

Expand Down
1 change: 1 addition & 0 deletions src/PIL/TiffTags.py
Expand Up @@ -184,6 +184,7 @@ def lookup(tag):
34665: ("ExifIFD", LONG, 1),
34675: ("ICCProfile", UNDEFINED, 1),
34853: ("GPSInfoIFD", LONG, 1),
40965: ("InteroperabilityIFD", LONG, 1),
# MPInfo
45056: ("MPFVersion", UNDEFINED, 1),
45057: ("NumberOfImages", LONG, 1),
Expand Down

0 comments on commit 8723cb3

Please sign in to comment.