Pillow

Форк
0
/
test_file_spider.py 
165 строк · 3.5 Кб
1
from __future__ import annotations
2

3
import tempfile
4
import warnings
5
from io import BytesIO
6
from pathlib import Path
7

8
import pytest
9

10
from PIL import Image, ImageSequence, SpiderImagePlugin
11

12
from .helper import assert_image_equal, hopper, is_pypy
13

14
TEST_FILE = "Tests/images/hopper.spider"
15

16

17
def test_sanity() -> None:
18
    with Image.open(TEST_FILE) as im:
19
        im.load()
20
        assert im.mode == "F"
21
        assert im.size == (128, 128)
22
        assert im.format == "SPIDER"
23

24

25
@pytest.mark.skipif(is_pypy(), reason="Requires CPython")
26
def test_unclosed_file() -> None:
27
    def open() -> None:
28
        im = Image.open(TEST_FILE)
29
        im.load()
30

31
    with pytest.warns(ResourceWarning):
32
        open()
33

34

35
def test_closed_file() -> None:
36
    with warnings.catch_warnings():
37
        im = Image.open(TEST_FILE)
38
        im.load()
39
        im.close()
40

41

42
def test_context_manager() -> None:
43
    with warnings.catch_warnings():
44
        with Image.open(TEST_FILE) as im:
45
            im.load()
46

47

48
def test_save(tmp_path: Path) -> None:
49
    # Arrange
50
    temp = str(tmp_path / "temp.spider")
51
    im = hopper()
52

53
    # Act
54
    im.save(temp, "SPIDER")
55

56
    # Assert
57
    with Image.open(temp) as im2:
58
        assert im2.mode == "F"
59
        assert im2.size == (128, 128)
60
        assert im2.format == "SPIDER"
61

62

63
def test_tempfile() -> None:
64
    # Arrange
65
    im = hopper()
66

67
    # Act
68
    with tempfile.TemporaryFile() as fp:
69
        im.save(fp, "SPIDER")
70

71
        # Assert
72
        fp.seek(0)
73
        with Image.open(fp) as reloaded:
74
            assert reloaded.mode == "F"
75
            assert reloaded.size == (128, 128)
76
            assert reloaded.format == "SPIDER"
77

78

79
def test_is_spider_image() -> None:
80
    assert SpiderImagePlugin.isSpiderImage(TEST_FILE)
81

82

83
def test_tell() -> None:
84
    # Arrange
85
    with Image.open(TEST_FILE) as im:
86
        # Act
87
        index = im.tell()
88

89
        # Assert
90
        assert index == 0
91

92

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
97

98

99
def test_load_image_series() -> None:
100
    # Arrange
101
    not_spider_file = "Tests/images/hopper.ppm"
102
    file_list = [TEST_FILE, not_spider_file, "path/not_found.ext"]
103

104
    # Act
105
    img_list = SpiderImagePlugin.loadImageSeries(file_list)
106

107
    # Assert
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)
112

113

114
def test_load_image_series_no_input() -> None:
115
    # Arrange
116
    file_list = None
117

118
    # Act
119
    img_list = SpiderImagePlugin.loadImageSeries(file_list)
120

121
    # Assert
122
    assert img_list is None
123

124

125
def test_is_int_not_a_number() -> None:
126
    # Arrange
127
    not_a_number = "a"
128

129
    # Act
130
    ret = SpiderImagePlugin.isInt(not_a_number)
131

132
    # Assert
133
    assert ret == 0
134

135

136
def test_invalid_file() -> None:
137
    invalid_file = "Tests/images/invalid.spider"
138

139
    with pytest.raises(OSError):
140
        with Image.open(invalid_file):
141
            pass
142

143

144
def test_nonstack_file() -> None:
145
    with Image.open(TEST_FILE) as im:
146
        with pytest.raises(EOFError):
147
            im.seek(0)
148

149

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

155

156
# for issue #4093
157
def test_odd_size() -> None:
158
    data = BytesIO()
159
    width = 100
160
    im = Image.new("F", (width, 64))
161
    im.save(data, format="SPIDER")
162

163
    data.seek(0)
164
    with Image.open(data) as im2:
165
        assert_image_equal(im, im2)
166

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

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

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

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