diff --git a/Tests/test_image.py b/Tests/test_image.py index 2cd858df16f..df546518afa 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -666,6 +666,19 @@ def act(fp): assert not fp.closed + def test_empty_exif(self): + with Image.open("Tests/images/exif.png") as im: + exif = im.getexif() + assert dict(exif) != {} + + # Test that exif data is cleared after another load + exif.load(None) + assert dict(exif) == {} + + # Test loading just the EXIF header + exif.load(b"Exif\x00\x00") + assert dict(exif) == {} + @mark_if_feature_version( pytest.mark.valgrind_known_error, "libjpeg_turbo", "2.0", reason="Known Failing" ) diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 0ff754af8e5..54cce848504 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -3485,12 +3485,12 @@ def load(self, data): self._loaded_exif = data self._data.clear() self._ifds.clear() + if data and data.startswith(b"Exif\x00\x00"): + data = data[6:] if not data: self._info = None return - if data.startswith(b"Exif\x00\x00"): - data = data[6:] self.fp = io.BytesIO(data) self.head = self.fp.read(8) # process dictionary