forked from python-pillow/Pillow
/
test_pickle.py
142 lines (108 loc) · 3.92 KB
/
test_pickle.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
import pickle
import pytest
from PIL import Image, ImageDraw, ImageFont
from .helper import assert_image_equal, skip_unless_feature
FONT_SIZE = 20
FONT_PATH = "Tests/fonts/DejaVuSans/DejaVuSans.ttf"
def helper_pickle_file(tmp_path, pickle, protocol, test_file, mode):
# Arrange
with Image.open(test_file) as im:
filename = str(tmp_path / "temp.pkl")
if mode:
im = im.convert(mode)
# Act
with open(filename, "wb") as f:
pickle.dump(im, f, protocol)
with open(filename, "rb") as f:
loaded_im = pickle.load(f)
# Assert
assert im == loaded_im
def helper_pickle_string(pickle, protocol, test_file, mode):
with Image.open(test_file) as im:
if mode:
im = im.convert(mode)
# Act
dumped_string = pickle.dumps(im, protocol)
loaded_im = pickle.loads(dumped_string)
# Assert
assert im == loaded_im
@pytest.mark.parametrize(
("test_file", "test_mode"),
[
("Tests/images/hopper.jpg", None),
("Tests/images/hopper.jpg", "L"),
("Tests/images/hopper.jpg", "PA"),
pytest.param(
"Tests/images/hopper.webp", None, marks=skip_unless_feature("webp")
),
("Tests/images/hopper.tif", None),
("Tests/images/test-card.png", None),
("Tests/images/zero_bb.png", None),
("Tests/images/zero_bb_scale2.png", None),
("Tests/images/non_zero_bb.png", None),
("Tests/images/non_zero_bb_scale2.png", None),
("Tests/images/p_trns_single.png", None),
("Tests/images/pil123p.png", None),
("Tests/images/itxt_chunks.png", None),
],
)
def test_pickle_image(tmp_path, test_file, test_mode):
# Act / Assert
for protocol in range(0, pickle.HIGHEST_PROTOCOL + 1):
helper_pickle_string(pickle, protocol, test_file, test_mode)
helper_pickle_file(tmp_path, pickle, protocol, test_file, test_mode)
def test_pickle_la_mode_with_palette(tmp_path):
# Arrange
filename = str(tmp_path / "temp.pkl")
with Image.open("Tests/images/hopper.jpg") as im:
im = im.convert("PA")
# Act / Assert
for protocol in range(0, pickle.HIGHEST_PROTOCOL + 1):
im.mode = "LA"
with open(filename, "wb") as f:
pickle.dump(im, f, protocol)
with open(filename, "rb") as f:
loaded_im = pickle.load(f)
im.mode = "PA"
assert im == loaded_im
@skip_unless_feature("webp")
def test_pickle_tell():
# Arrange
image = Image.open("Tests/images/hopper.webp")
# Act: roundtrip
unpickled_image = pickle.loads(pickle.dumps(image))
# Assert
assert unpickled_image.tell() == 0
def helper_assert_pickled_font_images(font1, font2):
# Arrange
im1 = Image.new(mode="RGBA", size=(300, 100))
im2 = Image.new(mode="RGBA", size=(300, 100))
draw1 = ImageDraw.Draw(im1)
draw2 = ImageDraw.Draw(im2)
txt = "Hello World!"
# Act
draw1.text((10, 10), txt, font=font1)
draw2.text((10, 10), txt, font=font2)
# Assert
assert_image_equal(im1, im2)
@pytest.mark.parametrize("protocol", list(range(0, pickle.HIGHEST_PROTOCOL + 1)))
def test_pickle_font_string(protocol):
# Arrange
font = ImageFont.truetype(FONT_PATH, FONT_SIZE)
# Act: roundtrip
pickled_font = pickle.dumps(font, protocol)
unpickled_font = pickle.loads(pickled_font)
# Assert
helper_assert_pickled_font_images(font, unpickled_font)
@pytest.mark.parametrize("protocol", list(range(0, pickle.HIGHEST_PROTOCOL + 1)))
def test_pickle_font_file(tmp_path, protocol):
# Arrange
font = ImageFont.truetype(FONT_PATH, FONT_SIZE)
filename = str(tmp_path / "temp.pkl")
# Act: roundtrip
with open(filename, "wb") as f:
pickle.dump(font, f, protocol)
with open(filename, "rb") as f:
unpickled_font = pickle.load(f)
# Assert
helper_assert_pickled_font_images(font, unpickled_font)