Slightly simplify the getexif procedure

No need for a loop here either.
Note that you could also use a dictionary rather than an if-elif tree
for the value -> rotation mapping (where valid/supported).
This commit is contained in:
mara004 2024-08-25 17:52:38 +02:00
parent 819b366bf5
commit 4fa160259d

View file

@ -22,7 +22,7 @@ import sys
import os import os
import zlib import zlib
import argparse import argparse
from PIL import Image, TiffImagePlugin, GifImagePlugin, ImageCms from PIL import Image, TiffImagePlugin, GifImagePlugin, ImageCms, ExifTags
if hasattr(GifImagePlugin, "LoadingStrategy"): if hasattr(GifImagePlugin, "LoadingStrategy"):
# Pillow 9.0.0 started emitting all frames but the first as RGB instead of # Pillow 9.0.0 started emitting all frames but the first as RGB instead of
@ -1372,41 +1372,42 @@ def get_imgmetadata(
rotation = 0 rotation = 0
if rotreq in (None, Rotation.auto, Rotation.ifvalid): if rotreq in (None, Rotation.auto, Rotation.ifvalid):
if hasattr(imgdata, "_getexif") and imgdata._getexif() is not None: exif_dict = imgdata.getexif()
for tag, value in imgdata._getexif().items(): o_key = ExifTags.Base.Orientation.value # 274 rsp. 0x112
if TAGS.get(tag, tag) == "Orientation": if exif_dict and o_key in exif_dict:
# Detailed information on EXIF rotation tags: # Detailed information on EXIF rotation tags:
# http://impulseadventure.com/photo/exif-orientation.html # http://impulseadventure.com/photo/exif-orientation.html
if value == 1: value = exif_dict[o_key]
rotation = 0 if value == 1:
elif value == 6: rotation = 0
rotation = 90 elif value == 6:
elif value == 3: rotation = 90
rotation = 180 elif value == 3:
elif value == 8: rotation = 180
rotation = 270 elif value == 8:
elif value in (2, 4, 5, 7): rotation = 270
if rotreq == Rotation.ifvalid: elif value in (2, 4, 5, 7):
logger.warning( if rotreq == Rotation.ifvalid:
"Unsupported flipped rotation mode (%d): use " logger.warning(
"--rotation=ifvalid or " "Unsupported flipped rotation mode (%d): use "
"rotation=img2pdf.Rotation.ifvalid to ignore", "--rotation=ifvalid or "
value, "rotation=img2pdf.Rotation.ifvalid to ignore",
) value,
else: )
raise ExifOrientationError( else:
"Unsupported flipped rotation mode (%d): use " raise ExifOrientationError(
"--rotation=ifvalid or " "Unsupported flipped rotation mode (%d): use "
"rotation=img2pdf.Rotation.ifvalid to ignore" % value "--rotation=ifvalid or "
) "rotation=img2pdf.Rotation.ifvalid to ignore" % value
else: )
if rotreq == Rotation.ifvalid: else:
logger.warning("Invalid rotation (%d)", value) if rotreq == Rotation.ifvalid:
else: logger.warning("Invalid rotation (%d)", value)
raise ExifOrientationError( else:
"Invalid rotation (%d): use --rotation=ifvalid " raise ExifOrientationError(
"or rotation=img2pdf.Rotation.ifvalid to ignore" % value "Invalid rotation (%d): use --rotation=ifvalid "
) "or rotation=img2pdf.Rotation.ifvalid to ignore" % value
)
elif rotreq in (Rotation.none, Rotation["0"]): elif rotreq in (Rotation.none, Rotation["0"]):
rotation = 0 rotation = 0
elif rotreq == Rotation["90"]: elif rotreq == Rotation["90"]: