1
from __future__ import annotations
6
from pathlib import Path
10
from PIL import Image, ImageSequence, SpiderImagePlugin
12
from .helper import assert_image_equal, hopper, is_pypy
14
TEST_FILE = "Tests/images/hopper.spider"
17
def test_sanity() -> None:
18
with Image.open(TEST_FILE) as im:
21
assert im.size == (128, 128)
22
assert im.format == "SPIDER"
25
@pytest.mark.skipif(is_pypy(), reason="Requires CPython")
26
def test_unclosed_file() -> None:
28
im = Image.open(TEST_FILE)
31
with pytest.warns(ResourceWarning):
35
def test_closed_file() -> None:
36
with warnings.catch_warnings():
37
im = Image.open(TEST_FILE)
42
def test_context_manager() -> None:
43
with warnings.catch_warnings():
44
with Image.open(TEST_FILE) as im:
48
def test_save(tmp_path: Path) -> None:
50
temp = str(tmp_path / "temp.spider")
54
im.save(temp, "SPIDER")
57
with Image.open(temp) as im2:
58
assert im2.mode == "F"
59
assert im2.size == (128, 128)
60
assert im2.format == "SPIDER"
63
def test_tempfile() -> None:
68
with tempfile.TemporaryFile() as fp:
73
with Image.open(fp) as reloaded:
74
assert reloaded.mode == "F"
75
assert reloaded.size == (128, 128)
76
assert reloaded.format == "SPIDER"
79
def test_is_spider_image() -> None:
80
assert SpiderImagePlugin.isSpiderImage(TEST_FILE)
83
def test_tell() -> None:
85
with Image.open(TEST_FILE) as im:
93
def test_n_frames() -> None:
94
with Image.open(TEST_FILE) as im:
95
assert im.n_frames == 1
96
assert not im.is_animated
99
def test_load_image_series() -> None:
101
not_spider_file = "Tests/images/hopper.ppm"
102
file_list = [TEST_FILE, not_spider_file, "path/not_found.ext"]
105
img_list = SpiderImagePlugin.loadImageSeries(file_list)
108
assert img_list is not None
109
assert len(img_list) == 1
110
assert isinstance(img_list[0], Image.Image)
111
assert img_list[0].size == (128, 128)
114
def test_load_image_series_no_input() -> None:
119
img_list = SpiderImagePlugin.loadImageSeries(file_list)
122
assert img_list is None
125
def test_is_int_not_a_number() -> None:
130
ret = SpiderImagePlugin.isInt(not_a_number)
136
def test_invalid_file() -> None:
137
invalid_file = "Tests/images/invalid.spider"
139
with pytest.raises(OSError):
140
with Image.open(invalid_file):
144
def test_nonstack_file() -> None:
145
with Image.open(TEST_FILE) as im:
146
with pytest.raises(EOFError):
150
def test_nonstack_dos() -> None:
151
with Image.open(TEST_FILE) as im:
152
for i, frame in enumerate(ImageSequence.Iterator(im)):
153
assert i <= 1, "Non-stack DOS file test failed"
157
def test_odd_size() -> None:
160
im = Image.new("F", (width, 64))
161
im.save(data, format="SPIDER")
164
with Image.open(data) as im2:
165
assert_image_equal(im, im2)