1
from __future__ import annotations
7
from PIL import FliImagePlugin, Image, ImageFile
9
from .helper import assert_image_equal, assert_image_equal_tofile, is_pypy
13
static_test_file = "Tests/images/hopper.fli"
16
animated_test_file = "Tests/images/a.fli"
19
animated_test_file_with_prefix_chunk = "Tests/images/2422.flc"
22
def test_sanity() -> None:
23
with Image.open(static_test_file) as im:
26
assert im.size == (128, 128)
27
assert im.format == "FLI"
28
assert not im.is_animated
30
with Image.open(animated_test_file) as im:
32
assert im.size == (320, 200)
33
assert im.format == "FLI"
34
assert im.info["duration"] == 71
38
def test_prefix_chunk() -> None:
39
ImageFile.LOAD_TRUNCATED_IMAGES = True
41
with Image.open(animated_test_file_with_prefix_chunk) as im:
43
assert im.size == (320, 200)
44
assert im.format == "FLI"
45
assert im.info["duration"] == 171
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]
53
ImageFile.LOAD_TRUNCATED_IMAGES = False
56
@pytest.mark.skipif(is_pypy(), reason="Requires CPython")
57
def test_unclosed_file() -> None:
59
im = Image.open(static_test_file)
62
with pytest.warns(ResourceWarning):
66
def test_closed_file() -> None:
67
with warnings.catch_warnings():
68
im = Image.open(static_test_file)
73
def test_seek_after_close() -> None:
74
im = Image.open(animated_test_file)
78
with pytest.raises(ValueError):
82
def test_context_manager() -> None:
83
with warnings.catch_warnings():
84
with Image.open(static_test_file) as im:
88
def test_tell() -> None:
90
with Image.open(static_test_file) as im:
98
def test_invalid_file() -> None:
99
invalid_file = "Tests/images/flower.jpg"
101
with pytest.raises(SyntaxError):
102
FliImagePlugin.FliImageFile(invalid_file)
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"))
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
116
with Image.open(animated_test_file) as im:
117
assert im.n_frames == 384
118
assert im.is_animated
121
def test_eoferror() -> None:
122
with Image.open(animated_test_file) as im:
123
n_frames = im.n_frames
126
with pytest.raises(EOFError):
128
assert im.tell() < n_frames
131
im.seek(n_frames - 1)
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
140
layer_number = im.tell()
141
assert layer_number == 0
144
layer_number = im.tell()
145
assert layer_number == 1
148
layer_number = im.tell()
149
assert layer_number == 2
152
layer_number = im.tell()
153
assert layer_number == 1
156
def test_seek() -> None:
157
with Image.open(animated_test_file) as im:
160
assert_image_equal_tofile(im, "Tests/images/a_fli.png")
163
@pytest.mark.parametrize(
166
"Tests/images/timeout-9139147ce93e20eb14088fe238e541443ffd64b3.fli",
167
"Tests/images/timeout-bff0a9dc7243a8e6ede2408d2ffa6a9964698b87.fli",
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):
178
@pytest.mark.parametrize(
181
"Tests/images/crash-5762152299364352.fli",
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):