Pillow

Форк
0
/
test_file_fli.py 
188 строк · 4.7 Кб
1
from __future__ import annotations
2

3
import warnings
4

5
import pytest
6

7
from PIL import FliImagePlugin, Image, ImageFile
8

9
from .helper import assert_image_equal, assert_image_equal_tofile, is_pypy
10

11
# created as an export of a palette image from Gimp2.6
12
# save as...-> hopper.fli, default options.
13
static_test_file = "Tests/images/hopper.fli"
14

15
# From https://samples.ffmpeg.org/fli-flc/
16
animated_test_file = "Tests/images/a.fli"
17

18
# From https://samples.ffmpeg.org/fli-flc/
19
animated_test_file_with_prefix_chunk = "Tests/images/2422.flc"
20

21

22
def test_sanity() -> None:
23
    with Image.open(static_test_file) as im:
24
        im.load()
25
        assert im.mode == "P"
26
        assert im.size == (128, 128)
27
        assert im.format == "FLI"
28
        assert not im.is_animated
29

30
    with Image.open(animated_test_file) as im:
31
        assert im.mode == "P"
32
        assert im.size == (320, 200)
33
        assert im.format == "FLI"
34
        assert im.info["duration"] == 71
35
        assert im.is_animated
36

37

38
def test_prefix_chunk() -> None:
39
    ImageFile.LOAD_TRUNCATED_IMAGES = True
40
    try:
41
        with Image.open(animated_test_file_with_prefix_chunk) as im:
42
            assert im.mode == "P"
43
            assert im.size == (320, 200)
44
            assert im.format == "FLI"
45
            assert im.info["duration"] == 171
46
            assert im.is_animated
47

48
            palette = im.getpalette()
49
            assert palette[3:6] == [255, 255, 255]
50
            assert palette[381:384] == [204, 204, 12]
51
            assert palette[765:] == [252, 0, 0]
52
    finally:
53
        ImageFile.LOAD_TRUNCATED_IMAGES = False
54

55

56
@pytest.mark.skipif(is_pypy(), reason="Requires CPython")
57
def test_unclosed_file() -> None:
58
    def open() -> None:
59
        im = Image.open(static_test_file)
60
        im.load()
61

62
    with pytest.warns(ResourceWarning):
63
        open()
64

65

66
def test_closed_file() -> None:
67
    with warnings.catch_warnings():
68
        im = Image.open(static_test_file)
69
        im.load()
70
        im.close()
71

72

73
def test_seek_after_close() -> None:
74
    im = Image.open(animated_test_file)
75
    im.seek(1)
76
    im.close()
77

78
    with pytest.raises(ValueError):
79
        im.seek(0)
80

81

82
def test_context_manager() -> None:
83
    with warnings.catch_warnings():
84
        with Image.open(static_test_file) as im:
85
            im.load()
86

87

88
def test_tell() -> None:
89
    # Arrange
90
    with Image.open(static_test_file) as im:
91
        # Act
92
        frame = im.tell()
93

94
        # Assert
95
        assert frame == 0
96

97

98
def test_invalid_file() -> None:
99
    invalid_file = "Tests/images/flower.jpg"
100

101
    with pytest.raises(SyntaxError):
102
        FliImagePlugin.FliImageFile(invalid_file)
103

104

105
def test_palette_chunk_second() -> None:
106
    with Image.open("Tests/images/hopper_palette_chunk_second.fli") as im:
107
        with Image.open(static_test_file) as expected:
108
            assert_image_equal(im.convert("RGB"), expected.convert("RGB"))
109

110

111
def test_n_frames() -> None:
112
    with Image.open(static_test_file) as im:
113
        assert im.n_frames == 1
114
        assert not im.is_animated
115

116
    with Image.open(animated_test_file) as im:
117
        assert im.n_frames == 384
118
        assert im.is_animated
119

120

121
def test_eoferror() -> None:
122
    with Image.open(animated_test_file) as im:
123
        n_frames = im.n_frames
124

125
        # Test seeking past the last frame
126
        with pytest.raises(EOFError):
127
            im.seek(n_frames)
128
        assert im.tell() < n_frames
129

130
        # Test that seeking to the last frame does not raise an error
131
        im.seek(n_frames - 1)
132

133

134
def test_seek_tell() -> None:
135
    with Image.open(animated_test_file) as im:
136
        layer_number = im.tell()
137
        assert layer_number == 0
138

139
        im.seek(0)
140
        layer_number = im.tell()
141
        assert layer_number == 0
142

143
        im.seek(1)
144
        layer_number = im.tell()
145
        assert layer_number == 1
146

147
        im.seek(2)
148
        layer_number = im.tell()
149
        assert layer_number == 2
150

151
        im.seek(1)
152
        layer_number = im.tell()
153
        assert layer_number == 1
154

155

156
def test_seek() -> None:
157
    with Image.open(animated_test_file) as im:
158
        im.seek(50)
159

160
        assert_image_equal_tofile(im, "Tests/images/a_fli.png")
161

162

163
@pytest.mark.parametrize(
164
    "test_file",
165
    [
166
        "Tests/images/timeout-9139147ce93e20eb14088fe238e541443ffd64b3.fli",
167
        "Tests/images/timeout-bff0a9dc7243a8e6ede2408d2ffa6a9964698b87.fli",
168
    ],
169
)
170
@pytest.mark.timeout(timeout=3)
171
def test_timeouts(test_file: str) -> None:
172
    with open(test_file, "rb") as f:
173
        with Image.open(f) as im:
174
            with pytest.raises(OSError):
175
                im.load()
176

177

178
@pytest.mark.parametrize(
179
    "test_file",
180
    [
181
        "Tests/images/crash-5762152299364352.fli",
182
    ],
183
)
184
def test_crash(test_file: str) -> None:
185
    with open(test_file, "rb") as f:
186
        with Image.open(f) as im:
187
            with pytest.raises(OSError):
188
                im.load()
189

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

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

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

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