Pillow

Форк
0
/
test_file_icns.py 
156 строк · 4.5 Кб
1
from __future__ import annotations
2

3
import io
4
import os
5
import warnings
6
from pathlib import Path
7

8
import pytest
9

10
from PIL import IcnsImagePlugin, Image, _binary
11

12
from .helper import assert_image_equal, assert_image_similar_tofile, skip_unless_feature
13

14
# sample icon file
15
TEST_FILE = "Tests/images/pillow.icns"
16

17

18
def test_sanity() -> None:
19
    # Loading this icon by default should result in the largest size
20
    # (512x512@2x) being loaded
21
    with Image.open(TEST_FILE) as im:
22
        # Assert that there is no unclosed file warning
23
        with warnings.catch_warnings():
24
            im.load()
25

26
        assert im.mode == "RGBA"
27
        assert im.size == (1024, 1024)
28
        assert im.format == "ICNS"
29

30

31
def test_load() -> None:
32
    with Image.open(TEST_FILE) as im:
33
        assert im.load()[0, 0] == (0, 0, 0, 0)
34

35
        # Test again now that it has already been loaded once
36
        assert im.load()[0, 0] == (0, 0, 0, 0)
37

38

39
def test_save(tmp_path: Path) -> None:
40
    temp_file = str(tmp_path / "temp.icns")
41

42
    with Image.open(TEST_FILE) as im:
43
        im.save(temp_file)
44

45
    with Image.open(temp_file) as reread:
46
        assert reread.mode == "RGBA"
47
        assert reread.size == (1024, 1024)
48
        assert reread.format == "ICNS"
49

50
    file_length = os.path.getsize(temp_file)
51
    with open(temp_file, "rb") as fp:
52
        fp.seek(4)
53
        assert _binary.i32be(fp.read(4)) == file_length
54

55

56
def test_save_append_images(tmp_path: Path) -> None:
57
    temp_file = str(tmp_path / "temp.icns")
58
    provided_im = Image.new("RGBA", (32, 32), (255, 0, 0, 128))
59

60
    with Image.open(TEST_FILE) as im:
61
        im.save(temp_file, append_images=[provided_im])
62

63
        assert_image_similar_tofile(im, temp_file, 1)
64

65
        with Image.open(temp_file) as reread:
66
            reread.size = (16, 16, 2)
67
            reread.load()
68
            assert_image_equal(reread, provided_im)
69

70

71
def test_save_fp() -> None:
72
    fp = io.BytesIO()
73

74
    with Image.open(TEST_FILE) as im:
75
        im.save(fp, format="ICNS")
76

77
    with Image.open(fp) as reread:
78
        assert reread.mode == "RGBA"
79
        assert reread.size == (1024, 1024)
80
        assert reread.format == "ICNS"
81

82

83
def test_sizes() -> None:
84
    # Check that we can load all of the sizes, and that the final pixel
85
    # dimensions are as expected
86
    with Image.open(TEST_FILE) as im:
87
        for w, h, r in im.info["sizes"]:
88
            wr = w * r
89
            hr = h * r
90
            im.size = (w, h, r)
91
            im.load()
92
            assert im.mode == "RGBA"
93
            assert im.size == (wr, hr)
94

95
        # Check that we cannot load an incorrect size
96
        with pytest.raises(ValueError):
97
            im.size = (1, 1)
98

99

100
def test_older_icon() -> None:
101
    # This icon was made with Icon Composer rather than iconutil; it still
102
    # uses PNG rather than JP2, however (since it was made on 10.9).
103
    with Image.open("Tests/images/pillow2.icns") as im:
104
        for w, h, r in im.info["sizes"]:
105
            wr = w * r
106
            hr = h * r
107
            with Image.open("Tests/images/pillow2.icns") as im2:
108
                im2.size = (w, h, r)
109
                im2.load()
110
                assert im2.mode == "RGBA"
111
                assert im2.size == (wr, hr)
112

113

114
@skip_unless_feature("jpg_2000")
115
def test_jp2_icon() -> None:
116
    # This icon uses JPEG 2000 images instead of the PNG images.
117
    # The advantage of doing this is that OS X 10.5 supports JPEG 2000
118
    # but not PNG; some commercial software therefore does just this.
119

120
    with Image.open("Tests/images/pillow3.icns") as im:
121
        for w, h, r in im.info["sizes"]:
122
            wr = w * r
123
            hr = h * r
124
            with Image.open("Tests/images/pillow3.icns") as im2:
125
                im2.size = (w, h, r)
126
                im2.load()
127
                assert im2.mode == "RGBA"
128
                assert im2.size == (wr, hr)
129

130

131
def test_getimage() -> None:
132
    with open(TEST_FILE, "rb") as fp:
133
        icns_file = IcnsImagePlugin.IcnsFile(fp)
134

135
        im = icns_file.getimage()
136
        assert im.mode == "RGBA"
137
        assert im.size == (1024, 1024)
138

139
        im = icns_file.getimage((512, 512))
140
        assert im.mode == "RGBA"
141
        assert im.size == (512, 512)
142

143

144
def test_not_an_icns_file() -> None:
145
    with io.BytesIO(b"invalid\n") as fp:
146
        with pytest.raises(SyntaxError):
147
            IcnsImagePlugin.IcnsFile(fp)
148

149

150
@skip_unless_feature("jpg_2000")
151
def test_icns_decompression_bomb() -> None:
152
    with Image.open(
153
        "Tests/images/oom-8ed3316a4109213ca96fb8a256a0bfefdece1461.icns"
154
    ) as im:
155
        with pytest.raises(Image.DecompressionBombError):
156
            im.load()
157

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

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

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

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