1
from __future__ import annotations
6
from pathlib import Path
10
from PIL import IcnsImagePlugin, Image, _binary
12
from .helper import assert_image_equal, assert_image_similar_tofile, skip_unless_feature
15
TEST_FILE = "Tests/images/pillow.icns"
18
def test_sanity() -> None:
21
with Image.open(TEST_FILE) as im:
23
with warnings.catch_warnings():
26
assert im.mode == "RGBA"
27
assert im.size == (1024, 1024)
28
assert im.format == "ICNS"
31
def test_load() -> None:
32
with Image.open(TEST_FILE) as im:
33
assert im.load()[0, 0] == (0, 0, 0, 0)
36
assert im.load()[0, 0] == (0, 0, 0, 0)
39
def test_save(tmp_path: Path) -> None:
40
temp_file = str(tmp_path / "temp.icns")
42
with Image.open(TEST_FILE) as im:
45
with Image.open(temp_file) as reread:
46
assert reread.mode == "RGBA"
47
assert reread.size == (1024, 1024)
48
assert reread.format == "ICNS"
50
file_length = os.path.getsize(temp_file)
51
with open(temp_file, "rb") as fp:
53
assert _binary.i32be(fp.read(4)) == file_length
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))
60
with Image.open(TEST_FILE) as im:
61
im.save(temp_file, append_images=[provided_im])
63
assert_image_similar_tofile(im, temp_file, 1)
65
with Image.open(temp_file) as reread:
66
reread.size = (16, 16, 2)
68
assert_image_equal(reread, provided_im)
71
def test_save_fp() -> None:
74
with Image.open(TEST_FILE) as im:
75
im.save(fp, format="ICNS")
77
with Image.open(fp) as reread:
78
assert reread.mode == "RGBA"
79
assert reread.size == (1024, 1024)
80
assert reread.format == "ICNS"
83
def test_sizes() -> None:
86
with Image.open(TEST_FILE) as im:
87
for w, h, r in im.info["sizes"]:
92
assert im.mode == "RGBA"
93
assert im.size == (wr, hr)
96
with pytest.raises(ValueError):
100
def test_older_icon() -> None:
103
with Image.open("Tests/images/pillow2.icns") as im:
104
for w, h, r in im.info["sizes"]:
107
with Image.open("Tests/images/pillow2.icns") as im2:
110
assert im2.mode == "RGBA"
111
assert im2.size == (wr, hr)
114
@skip_unless_feature("jpg_2000")
115
def test_jp2_icon() -> None:
120
with Image.open("Tests/images/pillow3.icns") as im:
121
for w, h, r in im.info["sizes"]:
124
with Image.open("Tests/images/pillow3.icns") as im2:
127
assert im2.mode == "RGBA"
128
assert im2.size == (wr, hr)
131
def test_getimage() -> None:
132
with open(TEST_FILE, "rb") as fp:
133
icns_file = IcnsImagePlugin.IcnsFile(fp)
135
im = icns_file.getimage()
136
assert im.mode == "RGBA"
137
assert im.size == (1024, 1024)
139
im = icns_file.getimage((512, 512))
140
assert im.mode == "RGBA"
141
assert im.size == (512, 512)
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)
150
@skip_unless_feature("jpg_2000")
151
def test_icns_decompression_bomb() -> None:
153
"Tests/images/oom-8ed3316a4109213ca96fb8a256a0bfefdece1461.icns"
155
with pytest.raises(Image.DecompressionBombError):