Skip to content

Commit

Permalink
Simplified EXIF code (#12)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
radarhere and radarhere authored Dec 15, 2024
1 parent ddc8e7e commit b585f9e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 37 deletions.
35 changes: 16 additions & 19 deletions src/PIL/AvifImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,21 @@ def _open(self) -> None:

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):
Expand Down Expand Up @@ -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")

Expand Down
26 changes: 8 additions & 18 deletions src/_avif.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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 =
Expand All @@ -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;
Expand All @@ -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 =
Expand All @@ -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;
Expand All @@ -209,7 +199,7 @@ exif_orientation_to_irot_imir(avifImage *image, int orientation) {
#else
image->imir.mode = 0; // ignored
#endif
return;
break;
}
}

Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit b585f9e

Please sign in to comment.