Simplified EXIF code (#12)

* Use break in switch

* Use walrus operator

* Do not add irot and imir flags if orientation is default

* Do not potentially call Exif tobytes() twice

* Simplified code by only setting info["exif"] once

---------

Co-authored-by: Andrew Murray <radarhere@users.noreply.github.com>
This commit is contained in:
Andrew Murray 2024-12-16 07:00:14 +11:00 committed by GitHub
parent ddc8e7e459
commit b585f9e560
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 37 deletions

View File

@ -96,20 +96,21 @@ class AvifImageFile(ImageFile.ImageFile):
if icc:
self.info["icc_profile"] = icc
if exif:
self.info["exif"] = exif
if xmp:
self.info["xmp"] = xmp
if exif_orientation != 1 or exif is not None:
if exif_orientation != 1 or exif:
exif_data = Image.Exif()
orig_orientation = 1
if exif is not None:
if exif:
exif_data.load(exif)
orig_orientation = exif_data.get(ExifTags.Base.Orientation, 1)
if exif_orientation != orig_orientation:
original_orientation = exif_data.get(ExifTags.Base.Orientation, 1)
else:
original_orientation = 1
if exif_orientation != original_orientation:
exif_data[ExifTags.Base.Orientation] = exif_orientation
self.info["exif"] = exif_data.tobytes()
exif = exif_data.tobytes()
if exif:
self.info["exif"] = exif
def seek(self, frame: int) -> None:
if not self._seek_check(frame):
@ -180,22 +181,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 = info.get("exif")
if exif:
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")

View File

@ -126,16 +126,6 @@ exif_orientation_to_irot_imir(avifImage *image, int orientation) {
// Orientation to irot and imir boxes as defined in HEIF ISO/IEC 28002-12:2021
// sections 6.5.10 and 6.5.12.
switch (orientation) {
case 1: // The 0th row is at the visual top of the image, and the 0th column is
// the visual left-hand side.
image->transformFlags = otherFlags;
image->irot.angle = 0; // ignored
#if AVIF_VERSION_MAJOR >= 1
image->imir.axis = 0; // ignored
#else
image->imir.mode = 0; // ignored
#endif
return;
case 2: // The 0th row is at the visual top of the image, and the 0th column is
// the visual right-hand side.
image->transformFlags = otherFlags | AVIF_TRANSFORM_IMIR;
@ -145,7 +135,7 @@ exif_orientation_to_irot_imir(avifImage *image, int orientation) {
#else
image->imir.mode = 1;
#endif
return;
break;
case 3: // The 0th row is at the visual bottom of the image, and the 0th column
// is the visual right-hand side.
image->transformFlags = otherFlags | AVIF_TRANSFORM_IROT;
@ -155,7 +145,7 @@ exif_orientation_to_irot_imir(avifImage *image, int orientation) {
#else
image->imir.mode = 0; // ignored
#endif
return;
break;
case 4: // The 0th row is at the visual bottom of the image, and the 0th column
// is the visual left-hand side.
image->transformFlags = otherFlags | AVIF_TRANSFORM_IMIR;
@ -165,7 +155,7 @@ exif_orientation_to_irot_imir(avifImage *image, int orientation) {
#else
image->imir.mode = 0;
#endif
return;
break;
case 5: // The 0th row is the visual left-hand side of the image, and the 0th
// column is the visual top.
image->transformFlags =
@ -177,7 +167,7 @@ exif_orientation_to_irot_imir(avifImage *image, int orientation) {
#else
image->imir.mode = 0;
#endif
return;
break;
case 6: // The 0th row is the visual right-hand side of the image, and the 0th
// column is the visual top.
image->transformFlags = otherFlags | AVIF_TRANSFORM_IROT;
@ -187,7 +177,7 @@ exif_orientation_to_irot_imir(avifImage *image, int orientation) {
#else
image->imir.mode = 0; // ignored
#endif
return;
break;
case 7: // The 0th row is the visual right-hand side of the image, and the 0th
// column is the visual bottom.
image->transformFlags =
@ -199,7 +189,7 @@ exif_orientation_to_irot_imir(avifImage *image, int orientation) {
#else
image->imir.mode = 0;
#endif
return;
break;
case 8: // The 0th row is the visual left-hand side of the image, and the 0th
// column is the visual bottom.
image->transformFlags = otherFlags | AVIF_TRANSFORM_IROT;
@ -209,7 +199,7 @@ exif_orientation_to_irot_imir(avifImage *image, int orientation) {
#else
image->imir.mode = 0; // ignored
#endif
return;
break;
}
}
@ -529,7 +519,7 @@ AvifEncoderNew(PyObject *self_, PyObject *args) {
return NULL;
}
}
if (exif_orientation > 0) {
if (exif_orientation > 1) {
exif_orientation_to_irot_imir(image, exif_orientation);
}