This repository has been archived by the owner on Jun 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
157 lines (131 loc) · 4.66 KB
/
tests.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
157
#!/usr/bin/env python
#
# Image merge module unit tests.
__author__ = "Sylvain Pasche (sylvain.pasche@gmail.com)"
import itertools
import os
import random
import StringIO
import unittest
import Image
from image_merge import merge
class TestImageMerge(unittest.TestCase):
def setUp(self):
self.data_dir = os.path.normpath(os.path.join(os.path.dirname(__file__), 'test-data'))
def testInvalidParams(self):
self.assertRaises(TypeError, lambda: merge(1))
self.assertRaises(TypeError, lambda: merge("foo", "bar", []))
self.assertRaises(SystemError, lambda: merge([]))
self.assertRaises(TypeError, lambda: merge([1]))
self.assertRaises(TypeError, lambda: merge(["foo", "bar", []]))
def _getImg(self, file_name):
"""Returns a string of the image file named file_name in the test-data
directory"""
return open(os.path.join(self.data_dir, file_name)).read()
def assertPNGImage(self, data):
self.assertEquals(data[:4], '\x89PNG', "Result image is not a png")
def testMergeImage(self):
img = merge([self._getImg("img1.png")])
self.assertPNGImage(img)
img = merge([self._getImg("img1.png"), self._getImg("img2.png")])
self.assertPNGImage(img)
def testMergeImagePixels(self):
def merge_pixels(input_pixels, preserve_colors, expected_output_pixel):
images = []
for p in input_pixels:
pil_img = Image.new("RGBA", (1, 1))
pil_img.putdata([p])
buffer = StringIO.StringIO()
pil_img.save(buffer, "PNG")
buffer.seek(0)
images.append(buffer.read())
result = merge(images, preserve_colors=preserve_colors)
self.assertPNGImage(result)
pil_result = Image.open(StringIO.StringIO(result))
self.assertEquals(pil_result.getdata()[0], expected_output_pixel)
merge_pixels([
(0, 0, 0, 255), # black opaque
], False,
(0, 0, 0, 255)
)
merge_pixels([
(255, 255, 255, 255), # white opaque
(0, 0, 0, 0), # fully transparent
], False,
(255, 255, 255, 255),
)
merge_pixels([
(255, 255, 255, 255), # white opaque
(0, 0, 0, 0), # fully transparent
(0, 0, 0, 0), # fully transparent
(0, 0, 0, 0), # fully transparent
], False,
(255, 255, 255, 255),
)
merge_pixels([
(255, 255, 255, 255), # white opaque
(0, 0, 0, 255), # black opaque
], False,
(0, 0, 0, 255)
)
merge_pixels([
(255, 255, 255, 255), # white opaque
(0, 0, 0, 255), # black opaque
(0, 0, 0, 255), # black opaque
(0, 0, 0, 255), # black opaque
(0, 0, 0, 255), # black opaque
(0, 0, 0, 255), # black opaque
], False,
(0, 0, 0, 255)
)
merge_pixels([
(0, 202, 0, 155),
(0, 0, 0, 0),
], False,
(0, 202, 0, 155)
)
merge_pixels([
(0, 0, 0, 0),
(0, 152, 253, 155),
], False,
(0, 151, 253, 155)
)
# Tests preserve_colors flag.
# Pixels are composed if preserve_colors if False
merge_pixels([
(156, 156, 156, 156),
(156, 156, 156, 156),
], False,
(155, 155, 155, 217)
)
# ... but not if preserve_colors is true (minus some rounding).
merge_pixels([
(156, 156, 156, 156),
(156, 156, 156, 156),
], True,
(155, 155, 155, 156),
)
def testImageMergeLimit(self):
MAX_IMAGES = 1024
# this one shouldn't throw. It's a bit slow, disable for faster
# testing.
if True:
images = list(itertools.repeat(
self._getImg("img1.png"), MAX_IMAGES))
merge(images)
def doTest():
images = list(itertools.repeat(
self._getImg("img1.png"), MAX_IMAGES + 1))
merge(images)
self.assertRaises(SystemError, doTest)
def testUnsupportedFormat(self):
self.assertRaises(SystemError, lambda: merge(["foo"]))
def doTest():
merge([self._getImg("img1.png"), "foo"])
self.assertRaises(SystemError, doTest)
def testInvalidImageSize(self):
def doTest():
merge([self._getImg("img1.png"), self._getImg("img1-10x10.png")])
self.assertRaises(SystemError, doTest)
if __name__ == '__main__':
unittest.main()