/
test_imagefile.py
333 lines (253 loc) · 9.91 KB
/
test_imagefile.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
from io import BytesIO
from PIL import EpsImagePlugin, Image, ImageFile
from .helper import PillowTestCase, fromstring, hopper, tostring, unittest
try:
from PIL import _webp
HAVE_WEBP = True
except ImportError:
HAVE_WEBP = False
codecs = dir(Image.core)
# save original block sizes
MAXBLOCK = ImageFile.MAXBLOCK
SAFEBLOCK = ImageFile.SAFEBLOCK
class TestImageFile(PillowTestCase):
def test_parser(self):
def roundtrip(format):
im = hopper("L").resize((1000, 1000))
if format in ("MSP", "XBM"):
im = im.convert("1")
test_file = BytesIO()
im.copy().save(test_file, format)
data = test_file.getvalue()
parser = ImageFile.Parser()
parser.feed(data)
imOut = parser.close()
return im, imOut
self.assert_image_equal(*roundtrip("BMP"))
im1, im2 = roundtrip("GIF")
self.assert_image_similar(im1.convert("P"), im2, 1)
self.assert_image_equal(*roundtrip("IM"))
self.assert_image_equal(*roundtrip("MSP"))
if "zip_encoder" in codecs:
try:
# force multiple blocks in PNG driver
ImageFile.MAXBLOCK = 8192
self.assert_image_equal(*roundtrip("PNG"))
finally:
ImageFile.MAXBLOCK = MAXBLOCK
self.assert_image_equal(*roundtrip("PPM"))
self.assert_image_equal(*roundtrip("TIFF"))
self.assert_image_equal(*roundtrip("XBM"))
self.assert_image_equal(*roundtrip("TGA"))
self.assert_image_equal(*roundtrip("PCX"))
if EpsImagePlugin.has_ghostscript():
im1, im2 = roundtrip("EPS")
# This test fails on Ubuntu 12.04, PPC (Bigendian) It
# appears to be a ghostscript 9.05 bug, since the
# ghostscript rendering is wonky and the file is identical
# to that written on ubuntu 12.04 x64
# md5sum: ba974835ff2d6f3f2fd0053a23521d4a
# EPS comes back in RGB:
self.assert_image_similar(im1, im2.convert("L"), 20)
if "jpeg_encoder" in codecs:
im1, im2 = roundtrip("JPEG") # lossy compression
self.assert_image(im1, im2.mode, im2.size)
self.assertRaises(IOError, roundtrip, "PDF")
def test_ico(self):
with open("Tests/images/python.ico", "rb") as f:
data = f.read()
with ImageFile.Parser() as p:
p.feed(data)
self.assertEqual((48, 48), p.image.size)
def test_safeblock(self):
if "zip_encoder" not in codecs:
self.skipTest("PNG (zlib) encoder not available")
im1 = hopper()
try:
ImageFile.SAFEBLOCK = 1
im2 = fromstring(tostring(im1, "PNG"))
finally:
ImageFile.SAFEBLOCK = SAFEBLOCK
self.assert_image_equal(im1, im2)
def test_raise_ioerror(self):
self.assertRaises(IOError, ImageFile.raise_ioerror, 1)
def test_raise_typeerror(self):
with self.assertRaises(TypeError):
parser = ImageFile.Parser()
parser.feed(1)
def test_truncated_with_errors(self):
if "zip_encoder" not in codecs:
self.skipTest("PNG (zlib) encoder not available")
im = Image.open("Tests/images/truncated_image.png")
with self.assertRaises(IOError):
im.load()
def test_truncated_without_errors(self):
if "zip_encoder" not in codecs:
self.skipTest("PNG (zlib) encoder not available")
im = Image.open("Tests/images/truncated_image.png")
ImageFile.LOAD_TRUNCATED_IMAGES = True
try:
im.load()
finally:
ImageFile.LOAD_TRUNCATED_IMAGES = False
def test_broken_datastream_with_errors(self):
if "zip_encoder" not in codecs:
self.skipTest("PNG (zlib) encoder not available")
im = Image.open("Tests/images/broken_data_stream.png")
with self.assertRaises(IOError):
im.load()
def test_broken_datastream_without_errors(self):
if "zip_encoder" not in codecs:
self.skipTest("PNG (zlib) encoder not available")
im = Image.open("Tests/images/broken_data_stream.png")
ImageFile.LOAD_TRUNCATED_IMAGES = True
try:
im.load()
finally:
ImageFile.LOAD_TRUNCATED_IMAGES = False
class MockPyDecoder(ImageFile.PyDecoder):
def decode(self, buffer):
# eof
return -1, 0
xoff, yoff, xsize, ysize = 10, 20, 100, 100
class MockImageFile(ImageFile.ImageFile):
def _open(self):
self.rawmode = "RGBA"
self.mode = "RGBA"
self._size = (200, 200)
self.tile = [("MOCK", (xoff, yoff, xoff + xsize, yoff + ysize), 32, None)]
class TestPyDecoder(PillowTestCase):
def get_decoder(self):
decoder = MockPyDecoder(None)
def closure(mode, *args):
decoder.__init__(mode, *args)
return decoder
Image.register_decoder("MOCK", closure)
return decoder
def test_setimage(self):
buf = BytesIO(b"\x00" * 255)
im = MockImageFile(buf)
d = self.get_decoder()
im.load()
self.assertEqual(d.state.xoff, xoff)
self.assertEqual(d.state.yoff, yoff)
self.assertEqual(d.state.xsize, xsize)
self.assertEqual(d.state.ysize, ysize)
self.assertRaises(ValueError, d.set_as_raw, b"\x00")
def test_extents_none(self):
buf = BytesIO(b"\x00" * 255)
im = MockImageFile(buf)
im.tile = [("MOCK", None, 32, None)]
d = self.get_decoder()
im.load()
self.assertEqual(d.state.xoff, 0)
self.assertEqual(d.state.yoff, 0)
self.assertEqual(d.state.xsize, 200)
self.assertEqual(d.state.ysize, 200)
def test_negsize(self):
buf = BytesIO(b"\x00" * 255)
im = MockImageFile(buf)
im.tile = [("MOCK", (xoff, yoff, -10, yoff + ysize), 32, None)]
self.get_decoder()
self.assertRaises(ValueError, im.load)
im.tile = [("MOCK", (xoff, yoff, xoff + xsize, -10), 32, None)]
self.assertRaises(ValueError, im.load)
def test_oversize(self):
buf = BytesIO(b"\x00" * 255)
im = MockImageFile(buf)
im.tile = [("MOCK", (xoff, yoff, xoff + xsize + 100, yoff + ysize), 32, None)]
self.get_decoder()
self.assertRaises(ValueError, im.load)
im.tile = [("MOCK", (xoff, yoff, xoff + xsize, yoff + ysize + 100), 32, None)]
self.assertRaises(ValueError, im.load)
def test_no_format(self):
buf = BytesIO(b"\x00" * 255)
im = MockImageFile(buf)
self.assertIsNone(im.format)
self.assertIsNone(im.get_format_mimetype())
def test_exif_jpeg(self):
im = Image.open("Tests/images/exif-72dpi-int.jpg") # Little endian
exif = im.getexif()
self.assertNotIn(258, exif)
self.assertIn(40960, exif)
self.assertEqual(exif[40963], 450)
self.assertEqual(exif[11], "gThumb 3.0.1")
out = self.tempfile("temp.jpg")
exif[258] = 8
del exif[40960]
exif[40963] = 455
exif[11] = "Pillow test"
im.save(out, exif=exif)
reloaded = Image.open(out)
reloaded_exif = reloaded.getexif()
self.assertEqual(reloaded_exif[258], 8)
self.assertNotIn(40960, exif)
self.assertEqual(reloaded_exif[40963], 455)
self.assertEqual(exif[11], "Pillow test")
im = Image.open("Tests/images/no-dpi-in-exif.jpg") # Big endian
exif = im.getexif()
self.assertNotIn(258, exif)
self.assertIn(40962, exif)
self.assertEqual(exif[40963], 200)
self.assertEqual(exif[305], "Adobe Photoshop CC 2017 (Macintosh)")
out = self.tempfile("temp.jpg")
exif[258] = 8
del exif[34665]
exif[40963] = 455
exif[305] = "Pillow test"
im.save(out, exif=exif)
reloaded = Image.open(out)
reloaded_exif = reloaded.getexif()
self.assertEqual(reloaded_exif[258], 8)
self.assertNotIn(40960, exif)
self.assertEqual(reloaded_exif[40963], 455)
self.assertEqual(exif[305], "Pillow test")
@unittest.skipIf(
not HAVE_WEBP or not _webp.HAVE_WEBPANIM,
"WebP support not installed with animation",
)
def test_exif_webp(self):
im = Image.open("Tests/images/hopper.webp")
exif = im.getexif()
self.assertEqual(exif, {})
out = self.tempfile("temp.webp")
exif[258] = 8
exif[40963] = 455
exif[305] = "Pillow test"
def check_exif():
reloaded = Image.open(out)
reloaded_exif = reloaded.getexif()
self.assertEqual(reloaded_exif[258], 8)
self.assertEqual(reloaded_exif[40963], 455)
self.assertEqual(exif[305], "Pillow test")
im.save(out, exif=exif)
check_exif()
im.save(out, exif=exif, save_all=True)
check_exif()
def test_exif_png(self):
im = Image.open("Tests/images/exif.png")
exif = im.getexif()
self.assertEqual(exif, {274: 1})
out = self.tempfile("temp.png")
exif[258] = 8
del exif[274]
exif[40963] = 455
exif[305] = "Pillow test"
im.save(out, exif=exif)
reloaded = Image.open(out)
reloaded_exif = reloaded.getexif()
self.assertEqual(reloaded_exif, {258: 8, 40963: 455, 305: "Pillow test"})
def test_exif_interop(self):
im = Image.open("Tests/images/flower.jpg")
exif = im.getexif()
self.assertEqual(
exif.get_ifd(0xA005), {1: "R98", 2: b"0100", 4097: 2272, 4098: 1704}
)
def test_exif_shared(self):
im = Image.open("Tests/images/exif.png")
exif = im.getexif()
self.assertIs(im.getexif(), exif)
def test_exif_str(self):
im = Image.open("Tests/images/exif.png")
exif = im.getexif()
self.assertEqual(str(exif), "{274: 1}")