1
from __future__ import annotations
4
from pathlib import Path
5
from types import ModuleType
11
from .helper import mark_if_feature_version, skip_unless_feature
13
pytestmark = skip_unless_feature("webp")
15
ElementTree: ModuleType | None
17
from defusedxml import ElementTree
22
def test_read_exif_metadata() -> None:
23
file_path = "Tests/images/flower.webp"
24
with Image.open(file_path) as image:
25
assert image.format == "WEBP"
26
exif_data = image.info.get("exif", None)
29
exif = image._getexif()
32
assert exif[271] == "Canon"
34
with Image.open("Tests/images/flower.jpg") as jpeg_image:
35
expected_exif = jpeg_image.info["exif"]
37
assert exif_data == expected_exif
40
def test_read_exif_metadata_without_prefix() -> None:
41
with Image.open("Tests/images/flower2.webp") as im:
43
assert im.info["exif"][:6] != b"Exif\x00\x00"
46
assert exif[305] == "Adobe Photoshop CS6 (Macintosh)"
49
@mark_if_feature_version(
50
pytest.mark.valgrind_known_error, "libjpeg_turbo", "2.0", reason="Known Failing"
52
def test_write_exif_metadata() -> None:
53
file_path = "Tests/images/flower.jpg"
54
test_buffer = BytesIO()
55
with Image.open(file_path) as image:
56
expected_exif = image.info["exif"]
58
image.save(test_buffer, "webp", exif=expected_exif)
61
with Image.open(test_buffer) as webp_image:
62
webp_exif = webp_image.info.get("exif", None)
63
assert webp_exif == expected_exif[6:], "WebP EXIF didn't match"
66
def test_read_icc_profile() -> None:
67
file_path = "Tests/images/flower2.webp"
68
with Image.open(file_path) as image:
69
assert image.format == "WEBP"
70
assert image.info.get("icc_profile", None)
72
icc = image.info["icc_profile"]
74
with Image.open("Tests/images/flower2.jpg") as jpeg_image:
75
expected_icc = jpeg_image.info["icc_profile"]
77
assert icc == expected_icc
80
@mark_if_feature_version(
81
pytest.mark.valgrind_known_error, "libjpeg_turbo", "2.0", reason="Known Failing"
83
def test_write_icc_metadata() -> None:
84
file_path = "Tests/images/flower2.jpg"
85
test_buffer = BytesIO()
86
with Image.open(file_path) as image:
87
expected_icc_profile = image.info["icc_profile"]
89
image.save(test_buffer, "webp", icc_profile=expected_icc_profile)
92
with Image.open(test_buffer) as webp_image:
93
webp_icc_profile = webp_image.info.get("icc_profile", None)
95
assert webp_icc_profile
97
assert webp_icc_profile == expected_icc_profile, "Webp ICC didn't match"
100
@mark_if_feature_version(
101
pytest.mark.valgrind_known_error, "libjpeg_turbo", "2.0", reason="Known Failing"
103
def test_read_no_exif() -> None:
104
file_path = "Tests/images/flower.jpg"
105
test_buffer = BytesIO()
106
with Image.open(file_path) as image:
107
assert "exif" in image.info
109
image.save(test_buffer, "webp")
112
with Image.open(test_buffer) as webp_image:
113
assert not webp_image._getexif()
116
def test_getxmp() -> None:
117
with Image.open("Tests/images/flower.webp") as im:
118
assert "xmp" not in im.info
119
if ElementTree is None:
122
match="XMP data cannot be read without defusedxml dependency",
129
with Image.open("Tests/images/flower2.webp") as im:
130
if ElementTree is None:
133
match="XMP data cannot be read without defusedxml dependency",
135
assert im.getxmp() == {}
137
assert "xmp" in im.info
139
im.getxmp()["xmpmeta"]["xmptk"]
140
== "Adobe XMP Core 5.3-c011 66.145661, 2012/02/06-14:56:27 "
144
def test_write_animated_metadata(tmp_path: Path) -> None:
145
iccp_data = b"<iccp_data>"
146
exif_data = b"<exif_data>"
147
xmp_data = b"<xmp_data>"
149
temp_file = str(tmp_path / "temp.webp")
150
with Image.open("Tests/images/anim_frame1.webp") as frame1:
151
with Image.open("Tests/images/anim_frame2.webp") as frame2:
155
append_images=[frame2, frame1, frame2],
156
icc_profile=iccp_data,
161
with Image.open(temp_file) as image:
162
assert "icc_profile" in image.info
163
assert "exif" in image.info
164
assert "xmp" in image.info
165
assert iccp_data == image.info.get("icc_profile", None)
166
assert exif_data == image.info.get("exif", None)
167
assert xmp_data == image.info.get("xmp", None)