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

Do not ignore SyntaxError when saving EXIF data #8

Merged
merged 3 commits into from
Dec 7, 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
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
Loading