forked from python-pillow/Pillow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_file_psd.py
156 lines (111 loc) · 3.99 KB
/
test_file_psd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import warnings
import pytest
from PIL import Image, PsdImagePlugin
from .helper import assert_image_equal_tofile, assert_image_similar, hopper, is_pypy
test_file = "Tests/images/hopper.psd"
def test_sanity():
with Image.open(test_file) as im:
im.load()
assert im.mode == "RGB"
assert im.size == (128, 128)
assert im.format == "PSD"
assert im.get_format_mimetype() == "image/vnd.adobe.photoshop"
im2 = hopper()
assert_image_similar(im, im2, 4.8)
@pytest.mark.skipif(is_pypy(), reason="Requires CPython")
def test_unclosed_file():
def open():
im = Image.open(test_file)
im.load()
pytest.warns(ResourceWarning, open)
def test_closed_file():
with warnings.catch_warnings():
im = Image.open(test_file)
im.load()
im.close()
def test_context_manager():
with warnings.catch_warnings():
with Image.open(test_file) as im:
im.load()
def test_invalid_file():
invalid_file = "Tests/images/flower.jpg"
with pytest.raises(SyntaxError):
PsdImagePlugin.PsdImageFile(invalid_file)
def test_n_frames():
with Image.open("Tests/images/hopper_merged.psd") as im:
assert im.n_frames == 1
assert not im.is_animated
for path in [test_file, "Tests/images/negative_layer_count.psd"]:
with Image.open(path) as im:
assert im.n_frames == 2
assert im.is_animated
def test_eoferror():
with Image.open(test_file) as im:
# PSD seek index starts at 1 rather than 0
n_frames = im.n_frames + 1
# Test seeking past the last frame
with pytest.raises(EOFError):
im.seek(n_frames)
assert im.tell() < n_frames
# Test that seeking to the last frame does not raise an error
im.seek(n_frames - 1)
def test_seek_tell():
with Image.open(test_file) as im:
layer_number = im.tell()
assert layer_number == 1
with pytest.raises(EOFError):
im.seek(0)
im.seek(1)
layer_number = im.tell()
assert layer_number == 1
im.seek(2)
layer_number = im.tell()
assert layer_number == 2
def test_seek_eoferror():
with Image.open(test_file) as im:
with pytest.raises(EOFError):
im.seek(-1)
def test_open_after_exclusive_load():
with Image.open(test_file) as im:
im.load()
im.seek(im.tell() + 1)
im.load()
def test_rgba():
with Image.open("Tests/images/rgba.psd") as im:
assert_image_equal_tofile(im, "Tests/images/imagedraw_square.png")
def test_icc_profile():
with Image.open(test_file) as im:
assert "icc_profile" in im.info
icc_profile = im.info["icc_profile"]
assert len(icc_profile) == 3144
def test_no_icc_profile():
with Image.open("Tests/images/hopper_merged.psd") as im:
assert "icc_profile" not in im.info
def test_combined_larger_than_size():
# The combined size of the individual parts is larger than the
# declared 'size' of the extra data field, resulting in a backwards seek.
# If we instead take the 'size' of the extra data field as the source of truth,
# then the seek can't be negative
with pytest.raises(OSError):
with Image.open("Tests/images/combined_larger_than_size.psd"):
pass
@pytest.mark.parametrize(
"test_file,raises",
[
(
"Tests/images/timeout-1ee28a249896e05b83840ae8140622de8e648ba9.psd",
Image.UnidentifiedImageError,
),
(
"Tests/images/timeout-598843abc37fc080ec36a2699ebbd44f795d3a6f.psd",
Image.UnidentifiedImageError,
),
("Tests/images/timeout-c8efc3fded6426986ba867a399791bae544f59bc.psd", OSError),
("Tests/images/timeout-dedc7a4ebd856d79b4359bbcc79e8ef231ce38f6.psd", OSError),
],
)
def test_crashes(test_file, raises):
with open(test_file, "rb") as f:
with pytest.raises(raises):
with Image.open(f):
pass