Pillow

Форк
0
/
test_file_webp_metadata.py 
167 строк · 5.0 Кб
1
from __future__ import annotations
2

3
from io import BytesIO
4
from pathlib import Path
5
from types import ModuleType
6

7
import pytest
8

9
from PIL import Image
10

11
from .helper import mark_if_feature_version, skip_unless_feature
12

13
pytestmark = skip_unless_feature("webp")
14

15
ElementTree: ModuleType | None
16
try:
17
    from defusedxml import ElementTree
18
except ImportError:
19
    ElementTree = None
20

21

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)
27
        assert exif_data
28

29
        exif = image._getexif()
30

31
        # Camera make
32
        assert exif[271] == "Canon"
33

34
        with Image.open("Tests/images/flower.jpg") as jpeg_image:
35
            expected_exif = jpeg_image.info["exif"]
36

37
            assert exif_data == expected_exif
38

39

40
def test_read_exif_metadata_without_prefix() -> None:
41
    with Image.open("Tests/images/flower2.webp") as im:
42
        # Assert prefix is not present
43
        assert im.info["exif"][:6] != b"Exif\x00\x00"
44

45
        exif = im.getexif()
46
        assert exif[305] == "Adobe Photoshop CS6 (Macintosh)"
47

48

49
@mark_if_feature_version(
50
    pytest.mark.valgrind_known_error, "libjpeg_turbo", "2.0", reason="Known Failing"
51
)
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"]
57

58
        image.save(test_buffer, "webp", exif=expected_exif)
59

60
    test_buffer.seek(0)
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"
64

65

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)
71

72
        icc = image.info["icc_profile"]
73

74
        with Image.open("Tests/images/flower2.jpg") as jpeg_image:
75
            expected_icc = jpeg_image.info["icc_profile"]
76

77
            assert icc == expected_icc
78

79

80
@mark_if_feature_version(
81
    pytest.mark.valgrind_known_error, "libjpeg_turbo", "2.0", reason="Known Failing"
82
)
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"]
88

89
        image.save(test_buffer, "webp", icc_profile=expected_icc_profile)
90

91
    test_buffer.seek(0)
92
    with Image.open(test_buffer) as webp_image:
93
        webp_icc_profile = webp_image.info.get("icc_profile", None)
94

95
    assert webp_icc_profile
96
    if webp_icc_profile:
97
        assert webp_icc_profile == expected_icc_profile, "Webp ICC didn't match"
98

99

100
@mark_if_feature_version(
101
    pytest.mark.valgrind_known_error, "libjpeg_turbo", "2.0", reason="Known Failing"
102
)
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
108

109
        image.save(test_buffer, "webp")
110

111
    test_buffer.seek(0)
112
    with Image.open(test_buffer) as webp_image:
113
        assert not webp_image._getexif()
114

115

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:
120
            with pytest.warns(
121
                UserWarning,
122
                match="XMP data cannot be read without defusedxml dependency",
123
            ):
124
                xmp = im.getxmp()
125
        else:
126
            xmp = im.getxmp()
127
        assert xmp == {}
128

129
    with Image.open("Tests/images/flower2.webp") as im:
130
        if ElementTree is None:
131
            with pytest.warns(
132
                UserWarning,
133
                match="XMP data cannot be read without defusedxml dependency",
134
            ):
135
                assert im.getxmp() == {}
136
        else:
137
            assert "xmp" in im.info
138
            assert (
139
                im.getxmp()["xmpmeta"]["xmptk"]
140
                == "Adobe XMP Core 5.3-c011 66.145661, 2012/02/06-14:56:27        "
141
            )
142

143

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>"
148

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:
152
            frame1.save(
153
                temp_file,
154
                save_all=True,
155
                append_images=[frame2, frame1, frame2],
156
                icc_profile=iccp_data,
157
                exif=exif_data,
158
                xmp=xmp_data,
159
            )
160

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)
168

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.