From 938f413d1dd7ff549ee2c152c53cfc3e71f96ef8 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 14 Dec 2024 20:48:26 +1100 Subject: [PATCH] Do not potentially call Exif tobytes() twice --- src/PIL/AvifImagePlugin.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/PIL/AvifImagePlugin.py b/src/PIL/AvifImagePlugin.py index 2ebe38ff2fa..8746666bf1d 100644 --- a/src/PIL/AvifImagePlugin.py +++ b/src/PIL/AvifImagePlugin.py @@ -180,21 +180,18 @@ def _save( autotiling = bool(info.get("autotiling", tile_rows_log2 == tile_cols_log2 == 0)) icc_profile = info.get("icc_profile", im.info.get("icc_profile")) + exif_orientation = 1 if exif := info.get("exif"): if isinstance(exif, Image.Exif): exif_data = exif - exif = exif.tobytes() else: exif_data = Image.Exif() exif_data.load(exif) - exif_orientation = exif_data.pop(ExifTags.Base.Orientation, 0) - if exif_orientation != 0: - if len(exif_data): - exif = exif_data.tobytes() - else: - exif = None - else: - exif_orientation = 0 + if ExifTags.Base.Orientation in exif_data: + exif_orientation = exif_data.pop(ExifTags.Base.Orientation) + exif = exif_data.tobytes() if exif_data else b"" + elif isinstance(exif, Image.Exif): + exif = exif_data.tobytes() xmp = info.get("xmp")