Skip to content

Commit

Permalink
Do not ignore SyntaxError when saving EXIF data (#8)
Browse files Browse the repository at this point in the history
* Do not ignore SyntaxError when saving EXIF data

* Do not save orientation in EXIF data

* Do not save XMP and EXIF data from info dictionary

---------

Co-authored-by: Andrew Murray <radarhere@users.noreply.github.com>
  • Loading branch information
radarhere and radarhere authored Dec 7, 2024
1 parent c40bcbf commit d76ae2f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 59 deletions.
31 changes: 2 additions & 29 deletions Tests/test_file_avif.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,17 +329,8 @@ def test_exif(self) -> None:
exif = im.getexif()
assert exif[274] == 3

def test_exif_save_default(self, tmp_path: Path) -> None:
with Image.open("Tests/images/avif/exif.avif") as im:
test_file = str(tmp_path / "temp.avif")
im.save(test_file)

with Image.open(test_file) as reloaded:
exif = reloaded.getexif()
assert exif[274] == 1

@pytest.mark.parametrize("bytes", [True, False])
def test_exif_save_argument(self, tmp_path: Path, bytes: bool) -> None:
def test_exif_save(self, tmp_path: Path, bytes: bool) -> None:
exif = Image.Exif()
exif[274] = 1
exif_data = exif.tobytes()
Expand All @@ -353,7 +344,7 @@ def test_exif_save_argument(self, tmp_path: Path, bytes: bool) -> None:
def test_exif_invalid(self, tmp_path: Path) -> None:
with Image.open(TEST_AVIF_FILE) as im:
test_file = str(tmp_path / "temp.avif")
with pytest.raises(ValueError):
with pytest.raises(SyntaxError):
im.save(test_file, exif=b"invalid")

def test_xmp(self) -> None:
Expand All @@ -362,24 +353,6 @@ def test_xmp(self) -> None:
assert_xmp_orientation(xmp, 3)

def test_xmp_save(self, tmp_path: Path) -> None:
with Image.open("Tests/images/avif/xmp_tags_orientation.avif") as im:
test_file = str(tmp_path / "temp.avif")
im.save(test_file)

with Image.open(test_file) as reloaded:
xmp = reloaded.info["xmp"]
assert_xmp_orientation(xmp, 3)

def test_xmp_save_from_png(self, tmp_path: Path) -> None:
with Image.open("Tests/images/xmp_tags_orientation.png") as im:
test_file = str(tmp_path / "temp.avif")
im.save(test_file)

with Image.open(test_file) as reloaded:
xmp = reloaded.info["xmp"]
assert_xmp_orientation(xmp, 3)

def test_xmp_save_argument(self, tmp_path: Path) -> None:
xmp_arg = "\n".join(
[
'<?xpacket begin="" id="W5M0MpCehiHzreSzNTczkc9d"?>',
Expand Down
25 changes: 10 additions & 15 deletions src/PIL/AvifImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,24 +168,19 @@ 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", im.info.get("exif"))
if isinstance(exif, Image.Exif):
exif = exif.tobytes()

exif_orientation = 0
exif = info.get("exif")
if exif:
exif_data = Image.Exif()
try:
exif_data.load(exif)
except SyntaxError:
pass
if isinstance(exif, Image.Exif):
exif_data = exif
exif = exif.tobytes()
else:
orientation_tag = next(
k for k, v in ExifTags.TAGS.items() if v == "Orientation"
)
exif_orientation = exif_data.get(orientation_tag) or 0
exif_data = Image.Exif()
exif_data.load(exif)
exif_orientation = exif_data.pop(ExifTags.Base.Orientation, 1)
else:
exif_orientation = 1

xmp = info.get("xmp", im.info.get("xmp") or im.info.get("XML:com.adobe.xmp"))
xmp = info.get("xmp")

if isinstance(xmp, str):
xmp = xmp.encode("utf-8")
Expand Down
15 changes: 0 additions & 15 deletions src/_avif.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,22 +170,7 @@ exif_orientation_to_irot_imir(avifImage *image, int orientation) {
image->imir.mode = 0; // ignored
#endif
return;
default: // reserved
break;
}

// The orientation tag is not mandatory (only recommended) according to JEITA
// CP-3451C section 4.6.8.A. The default value is 1 if the orientation tag is
// missing, meaning:
// 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
}

static int
Expand Down

0 comments on commit d76ae2f

Please sign in to comment.