From 003f0a35574872e6731b808e933ef3291d20e38c Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 7 Dec 2024 14:07:28 +1100 Subject: [PATCH] Do not ignore SyntaxError when saving EXIF data --- Tests/test_file_avif.py | 2 +- src/PIL/AvifImagePlugin.py | 18 ++++++------------ 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/Tests/test_file_avif.py b/Tests/test_file_avif.py index 9a3bb6b73c6..003d292beab 100644 --- a/Tests/test_file_avif.py +++ b/Tests/test_file_avif.py @@ -353,7 +353,7 @@ def test_exif_save_argument(self, tmp_path: Path, bytes: bool) -> None: def test_exif_invalid(self, tmp_path: Path) -> None: with Image.open(TEST_AVIF_FILE) as im: test_file = str(tmp_path / "temp.avif") - with pytest.raises(ValueError): + with pytest.raises(SyntaxError): im.save(test_file, exif=b"invalid") def test_xmp(self) -> None: diff --git a/src/PIL/AvifImagePlugin.py b/src/PIL/AvifImagePlugin.py index c92e2534ef9..d9d2850037e 100644 --- a/src/PIL/AvifImagePlugin.py +++ b/src/PIL/AvifImagePlugin.py @@ -169,21 +169,15 @@ def _save( icc_profile = info.get("icc_profile", im.info.get("icc_profile")) exif = info.get("exif", im.info.get("exif")) - if isinstance(exif, Image.Exif): - exif = exif.tobytes() - exif_orientation = 0 if exif: - exif_data = Image.Exif() - try: - exif_data.load(exif) - except SyntaxError: - pass + if isinstance(exif, Image.Exif): + exif_data = exif + exif = exif.tobytes() else: - orientation_tag = next( - k for k, v in ExifTags.TAGS.items() if v == "Orientation" - ) - exif_orientation = exif_data.get(orientation_tag) or 0 + exif_data = Image.Exif() + exif_data.load(exif) + exif_orientation = exif_data.get(ExifTags.Base.Orientation, 0) xmp = info.get("xmp", im.info.get("xmp") or im.info.get("XML:com.adobe.xmp"))