diff --git a/Tests/test_file_tiff_metadata.py b/Tests/test_file_tiff_metadata.py index 4339c946c..0adbaf016 100644 --- a/Tests/test_file_tiff_metadata.py +++ b/Tests/test_file_tiff_metadata.py @@ -390,3 +390,16 @@ def test_tag_group_data(): assert interop_ifd.tagtype[2] == 7 assert base_ifd.tagtype[2] != interop_ifd.tagtype[256] + + +def test_empty_subifd(tmp_path): + out = str(tmp_path / "temp.jpg") + + im = hopper() + exif = im.getexif() + exif[TiffImagePlugin.EXIFIFD] = {} + im.save(out, exif=exif) + + with Image.open(out) as reloaded: + exif = reloaded.getexif() + assert exif.get_ifd(TiffImagePlugin.EXIFIFD) == {} diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py index 699eb33e0..46798871e 100644 --- a/src/PIL/TiffImagePlugin.py +++ b/src/PIL/TiffImagePlugin.py @@ -830,7 +830,8 @@ class ImageFileDirectory_v2(MutableMapping): else: ifh = b"MM\x00\x2A\x00\x00\x00\x08" ifd = ImageFileDirectory_v2(ifh, group=tag) - for ifd_tag, ifd_value in self._tags_v2[tag].items(): + values = self._tags_v2[tag] + for ifd_tag, ifd_value in values.items(): ifd[ifd_tag] = ifd_value data = ifd.tobytes(offset) else: