Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplified EXIF code #12

Merged
merged 5 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -124,16 +124,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 @@ -143,7 +133,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 @@ -153,7 +143,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 @@ -163,7 +153,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 @@ -175,7 +165,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 @@ -185,7 +175,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 @@ -197,7 +187,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 @@ -207,7 +197,7 @@ exif_orientation_to_irot_imir(avifImage *image, int orientation) {
#else
image->imir.mode = 0; // ignored
#endif
return;
break;
}
}

Expand Down Expand Up @@ -523,7 +513,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
Loading