forked from python-pillow/Pillow
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_file_tiff.py
655 lines (527 loc) · 21.7 KB
/
test_file_tiff.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
import os
from io import BytesIO
import pytest
from PIL import Image, TiffImagePlugin
from PIL.TiffImagePlugin import RESOLUTION_UNIT, X_RESOLUTION, Y_RESOLUTION
from .helper import (
assert_image_equal,
assert_image_equal_tofile,
assert_image_similar,
assert_image_similar_tofile,
hopper,
is_pypy,
is_win32,
)
class TestFileTiff:
def test_sanity(self, tmp_path):
filename = str(tmp_path / "temp.tif")
hopper("RGB").save(filename)
with Image.open(filename) as im:
im.load()
assert im.mode == "RGB"
assert im.size == (128, 128)
assert im.format == "TIFF"
hopper("1").save(filename)
with Image.open(filename):
pass
hopper("L").save(filename)
with Image.open(filename):
pass
hopper("P").save(filename)
with Image.open(filename):
pass
hopper("RGB").save(filename)
with Image.open(filename):
pass
hopper("I").save(filename)
with Image.open(filename):
pass
@pytest.mark.skipif(is_pypy(), reason="Requires CPython")
def test_unclosed_file(self):
def open():
im = Image.open("Tests/images/multipage.tiff")
im.load()
pytest.warns(ResourceWarning, open)
def test_closed_file(self):
with pytest.warns(None) as record:
im = Image.open("Tests/images/multipage.tiff")
im.load()
im.close()
assert not record
def test_context_manager(self):
with pytest.warns(None) as record:
with Image.open("Tests/images/multipage.tiff") as im:
im.load()
assert not record
def test_mac_tiff(self):
# Read RGBa images from macOS [@PIL136]
filename = "Tests/images/pil136.tiff"
with Image.open(filename) as im:
assert im.mode == "RGBA"
assert im.size == (55, 43)
assert im.tile == [("raw", (0, 0, 55, 43), 8, ("RGBa", 0, 1))]
im.load()
assert_image_similar_tofile(im, "Tests/images/pil136.png", 1)
def test_wrong_bits_per_sample(self):
with Image.open("Tests/images/tiff_wrong_bits_per_sample.tiff") as im:
assert im.mode == "RGBA"
assert im.size == (52, 53)
assert im.tile == [("raw", (0, 0, 52, 53), 160, ("RGBA", 0, 1))]
im.load()
def test_set_legacy_api(self):
ifd = TiffImagePlugin.ImageFileDirectory_v2()
with pytest.raises(Exception) as e:
ifd.legacy_api = None
assert str(e.value) == "Not allowing setting of legacy api"
def test_xyres_tiff(self):
filename = "Tests/images/pil168.tif"
with Image.open(filename) as im:
# legacy api
assert isinstance(im.tag[X_RESOLUTION][0], tuple)
assert isinstance(im.tag[Y_RESOLUTION][0], tuple)
# v2 api
assert isinstance(im.tag_v2[X_RESOLUTION], TiffImagePlugin.IFDRational)
assert isinstance(im.tag_v2[Y_RESOLUTION], TiffImagePlugin.IFDRational)
assert im.info["dpi"] == (72.0, 72.0)
def test_xyres_fallback_tiff(self):
filename = "Tests/images/compression.tif"
with Image.open(filename) as im:
# v2 api
assert isinstance(im.tag_v2[X_RESOLUTION], TiffImagePlugin.IFDRational)
assert isinstance(im.tag_v2[Y_RESOLUTION], TiffImagePlugin.IFDRational)
with pytest.raises(KeyError):
im.tag_v2[RESOLUTION_UNIT]
# Legacy.
assert im.info["resolution"] == (100.0, 100.0)
# Fallback "inch".
assert im.info["dpi"] == (100.0, 100.0)
def test_int_resolution(self):
filename = "Tests/images/pil168.tif"
with Image.open(filename) as im:
# Try to read a file where X,Y_RESOLUTION are ints
im.tag_v2[X_RESOLUTION] = 71
im.tag_v2[Y_RESOLUTION] = 71
im._setup()
assert im.info["dpi"] == (71.0, 71.0)
def test_load_dpi_rounding(self):
for resolutionUnit, dpi in ((None, (72, 73)), (2, (72, 73)), (3, (183, 185))):
with Image.open(
"Tests/images/hopper_roundDown_" + str(resolutionUnit) + ".tif"
) as im:
assert im.tag_v2.get(RESOLUTION_UNIT) == resolutionUnit
assert im.info["dpi"] == (dpi[0], dpi[0])
with Image.open(
"Tests/images/hopper_roundUp_" + str(resolutionUnit) + ".tif"
) as im:
assert im.tag_v2.get(RESOLUTION_UNIT) == resolutionUnit
assert im.info["dpi"] == (dpi[1], dpi[1])
def test_save_dpi_rounding(self, tmp_path):
outfile = str(tmp_path / "temp.tif")
with Image.open("Tests/images/hopper.tif") as im:
for dpi in (72.2, 72.8):
im.save(outfile, dpi=(dpi, dpi))
with Image.open(outfile) as reloaded:
reloaded.load()
assert (round(dpi), round(dpi)) == reloaded.info["dpi"]
def test_save_setting_missing_resolution(self):
b = BytesIO()
with Image.open("Tests/images/10ct_32bit_128.tiff") as im:
im.save(b, format="tiff", resolution=123.45)
with Image.open(b) as im:
assert float(im.tag_v2[X_RESOLUTION]) == 123.45
assert float(im.tag_v2[Y_RESOLUTION]) == 123.45
def test_invalid_file(self):
invalid_file = "Tests/images/flower.jpg"
with pytest.raises(SyntaxError):
TiffImagePlugin.TiffImageFile(invalid_file)
TiffImagePlugin.PREFIXES.append(b"\xff\xd8\xff\xe0")
with pytest.raises(SyntaxError):
TiffImagePlugin.TiffImageFile(invalid_file)
TiffImagePlugin.PREFIXES.pop()
def test_bad_exif(self):
with Image.open("Tests/images/hopper_bad_exif.jpg") as i:
# Should not raise struct.error.
pytest.warns(UserWarning, i._getexif)
def test_save_rgba(self, tmp_path):
im = hopper("RGBA")
outfile = str(tmp_path / "temp.tif")
im.save(outfile)
def test_save_unsupported_mode(self, tmp_path):
im = hopper("HSV")
outfile = str(tmp_path / "temp.tif")
with pytest.raises(OSError):
im.save(outfile)
def test_little_endian(self):
with Image.open("Tests/images/16bit.cropped.tif") as im:
assert im.getpixel((0, 0)) == 480
assert im.mode == "I;16"
b = im.tobytes()
# Bytes are in image native order (little endian)
assert b[0] == ord(b"\xe0")
assert b[1] == ord(b"\x01")
def test_big_endian(self):
with Image.open("Tests/images/16bit.MM.cropped.tif") as im:
assert im.getpixel((0, 0)) == 480
assert im.mode == "I;16B"
b = im.tobytes()
# Bytes are in image native order (big endian)
assert b[0] == ord(b"\x01")
assert b[1] == ord(b"\xe0")
def test_16bit_s(self):
with Image.open("Tests/images/16bit.s.tif") as im:
im.load()
assert im.mode == "I"
assert im.getpixel((0, 0)) == 32767
assert im.getpixel((0, 1)) == 0
def test_12bit_rawmode(self):
"""Are we generating the same interpretation
of the image as Imagemagick is?"""
with Image.open("Tests/images/12bit.cropped.tif") as im:
# to make the target --
# convert 12bit.cropped.tif -depth 16 tmp.tif
# convert tmp.tif -evaluate RightShift 4 12in16bit2.tif
# imagemagick will auto scale so that a 12bit FFF is 16bit FFF0,
# so we need to unshift so that the integer values are the same.
assert_image_equal_tofile(im, "Tests/images/12in16bit.tif")
def test_32bit_float(self):
# Issue 614, specific 32-bit float format
path = "Tests/images/10ct_32bit_128.tiff"
with Image.open(path) as im:
im.load()
assert im.getpixel((0, 0)) == -0.4526388943195343
assert im.getextrema() == (-3.140936851501465, 3.140684127807617)
def test_unknown_pixel_mode(self):
with pytest.raises(OSError):
with Image.open("Tests/images/hopper_unknown_pixel_mode.tif"):
pass
def test_n_frames(self):
for path, n_frames in [
["Tests/images/multipage-lastframe.tif", 1],
["Tests/images/multipage.tiff", 3],
]:
with Image.open(path) as im:
assert im.n_frames == n_frames
assert im.is_animated == (n_frames != 1)
def test_eoferror(self):
with Image.open("Tests/images/multipage-lastframe.tif") as im:
n_frames = im.n_frames
# 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_multipage(self):
# issue #862
with Image.open("Tests/images/multipage.tiff") as im:
# file is a multipage tiff: 10x10 green, 10x10 red, 20x20 blue
im.seek(0)
assert im.size == (10, 10)
assert im.convert("RGB").getpixel((0, 0)) == (0, 128, 0)
im.seek(1)
im.load()
assert im.size == (10, 10)
assert im.convert("RGB").getpixel((0, 0)) == (255, 0, 0)
im.seek(0)
im.load()
assert im.size == (10, 10)
assert im.convert("RGB").getpixel((0, 0)) == (0, 128, 0)
im.seek(2)
im.load()
assert im.size == (20, 20)
assert im.convert("RGB").getpixel((0, 0)) == (0, 0, 255)
def test_multipage_last_frame(self):
with Image.open("Tests/images/multipage-lastframe.tif") as im:
im.load()
assert im.size == (20, 20)
assert im.convert("RGB").getpixel((0, 0)) == (0, 0, 255)
def test___str__(self):
filename = "Tests/images/pil136.tiff"
with Image.open(filename) as im:
# Act
ret = str(im.ifd)
# Assert
assert isinstance(ret, str)
def test_dict(self):
# Arrange
filename = "Tests/images/pil136.tiff"
with Image.open(filename) as im:
# v2 interface
v2_tags = {
256: 55,
257: 43,
258: (8, 8, 8, 8),
259: 1,
262: 2,
296: 2,
273: (8,),
338: (1,),
277: 4,
279: (9460,),
282: 72.0,
283: 72.0,
284: 1,
}
assert dict(im.tag_v2) == v2_tags
# legacy interface
legacy_tags = {
256: (55,),
257: (43,),
258: (8, 8, 8, 8),
259: (1,),
262: (2,),
296: (2,),
273: (8,),
338: (1,),
277: (4,),
279: (9460,),
282: ((720000, 10000),),
283: ((720000, 10000),),
284: (1,),
}
assert dict(im.tag) == legacy_tags
def test__delitem__(self):
filename = "Tests/images/pil136.tiff"
with Image.open(filename) as im:
len_before = len(dict(im.ifd))
del im.ifd[256]
len_after = len(dict(im.ifd))
assert len_before == len_after + 1
def test_load_byte(self):
for legacy_api in [False, True]:
ifd = TiffImagePlugin.ImageFileDirectory_v2()
data = b"abc"
ret = ifd.load_byte(data, legacy_api)
assert ret == b"abc"
def test_load_string(self):
ifd = TiffImagePlugin.ImageFileDirectory_v2()
data = b"abc\0"
ret = ifd.load_string(data, False)
assert ret == "abc"
def test_load_float(self):
ifd = TiffImagePlugin.ImageFileDirectory_v2()
data = b"abcdabcd"
ret = ifd.load_float(data, False)
assert ret == (1.6777999408082104e22, 1.6777999408082104e22)
def test_load_double(self):
ifd = TiffImagePlugin.ImageFileDirectory_v2()
data = b"abcdefghabcdefgh"
ret = ifd.load_double(data, False)
assert ret == (8.540883223036124e194, 8.540883223036124e194)
def test_ifd_tag_type(self):
with Image.open("Tests/images/ifd_tag_type.tiff") as im:
assert 0x8825 in im.tag_v2
def test_seek(self):
filename = "Tests/images/pil136.tiff"
with Image.open(filename) as im:
im.seek(0)
assert im.tell() == 0
def test_seek_eof(self):
filename = "Tests/images/pil136.tiff"
with Image.open(filename) as im:
assert im.tell() == 0
with pytest.raises(EOFError):
im.seek(-1)
with pytest.raises(EOFError):
im.seek(1)
def test__limit_rational_int(self):
from PIL.TiffImagePlugin import _limit_rational
value = 34
ret = _limit_rational(value, 65536)
assert ret == (34, 1)
def test__limit_rational_float(self):
from PIL.TiffImagePlugin import _limit_rational
value = 22.3
ret = _limit_rational(value, 65536)
assert ret == (223, 10)
def test_4bit(self):
test_file = "Tests/images/hopper_gray_4bpp.tif"
original = hopper("L")
with Image.open(test_file) as im:
assert im.size == (128, 128)
assert im.mode == "L"
assert_image_similar(im, original, 7.3)
def test_gray_semibyte_per_pixel(self):
test_files = (
(
24.8, # epsilon
( # group
"Tests/images/tiff_gray_2_4_bpp/hopper2.tif",
"Tests/images/tiff_gray_2_4_bpp/hopper2I.tif",
"Tests/images/tiff_gray_2_4_bpp/hopper2R.tif",
"Tests/images/tiff_gray_2_4_bpp/hopper2IR.tif",
),
),
(
7.3, # epsilon
( # group
"Tests/images/tiff_gray_2_4_bpp/hopper4.tif",
"Tests/images/tiff_gray_2_4_bpp/hopper4I.tif",
"Tests/images/tiff_gray_2_4_bpp/hopper4R.tif",
"Tests/images/tiff_gray_2_4_bpp/hopper4IR.tif",
),
),
)
original = hopper("L")
for epsilon, group in test_files:
with Image.open(group[0]) as im:
assert im.size == (128, 128)
assert im.mode == "L"
assert_image_similar(im, original, epsilon)
for file in group[1:]:
with Image.open(file) as im2:
assert im2.size == (128, 128)
assert im2.mode == "L"
assert_image_equal(im, im2)
def test_with_underscores(self, tmp_path):
kwargs = {"resolution_unit": "inch", "x_resolution": 72, "y_resolution": 36}
filename = str(tmp_path / "temp.tif")
hopper("RGB").save(filename, **kwargs)
with Image.open(filename) as im:
# legacy interface
assert im.tag[X_RESOLUTION][0][0] == 72
assert im.tag[Y_RESOLUTION][0][0] == 36
# v2 interface
assert im.tag_v2[X_RESOLUTION] == 72
assert im.tag_v2[Y_RESOLUTION] == 36
def test_roundtrip_tiff_uint16(self, tmp_path):
# Test an image of all '0' values
pixel_value = 0x1234
infile = "Tests/images/uint16_1_4660.tif"
with Image.open(infile) as im:
assert im.getpixel((0, 0)) == pixel_value
tmpfile = str(tmp_path / "temp.tif")
im.save(tmpfile)
assert_image_equal_tofile(im, tmpfile)
def test_strip_raw(self):
infile = "Tests/images/tiff_strip_raw.tif"
with Image.open(infile) as im:
assert_image_equal_tofile(im, "Tests/images/tiff_adobe_deflate.png")
def test_strip_planar_raw(self):
# gdal_translate -of GTiff -co INTERLEAVE=BAND \
# tiff_strip_raw.tif tiff_strip_planar_raw.tiff
infile = "Tests/images/tiff_strip_planar_raw.tif"
with Image.open(infile) as im:
assert_image_equal_tofile(im, "Tests/images/tiff_adobe_deflate.png")
def test_strip_planar_raw_with_overviews(self):
# gdaladdo tiff_strip_planar_raw2.tif 2 4 8 16
infile = "Tests/images/tiff_strip_planar_raw_with_overviews.tif"
with Image.open(infile) as im:
assert_image_equal_tofile(im, "Tests/images/tiff_adobe_deflate.png")
def test_tiled_planar_raw(self):
# gdal_translate -of GTiff -co TILED=YES -co BLOCKXSIZE=32 \
# -co BLOCKYSIZE=32 -co INTERLEAVE=BAND \
# tiff_tiled_raw.tif tiff_tiled_planar_raw.tiff
infile = "Tests/images/tiff_tiled_planar_raw.tif"
with Image.open(infile) as im:
assert_image_equal_tofile(im, "Tests/images/tiff_adobe_deflate.png")
def test_palette(self, tmp_path):
def roundtrip(mode):
outfile = str(tmp_path / "temp.tif")
im = hopper(mode)
im.save(outfile)
with Image.open(outfile) as reloaded:
assert_image_equal(im.convert("RGB"), reloaded.convert("RGB"))
for mode in ["P", "PA"]:
roundtrip(mode)
def test_tiff_save_all(self):
mp = BytesIO()
with Image.open("Tests/images/multipage.tiff") as im:
im.save(mp, format="tiff", save_all=True)
mp.seek(0, os.SEEK_SET)
with Image.open(mp) as im:
assert im.n_frames == 3
# Test appending images
mp = BytesIO()
im = Image.new("RGB", (100, 100), "#f00")
ims = [Image.new("RGB", (100, 100), color) for color in ["#0f0", "#00f"]]
im.copy().save(mp, format="TIFF", save_all=True, append_images=ims)
mp.seek(0, os.SEEK_SET)
with Image.open(mp) as reread:
assert reread.n_frames == 3
# Test appending using a generator
def imGenerator(ims):
yield from ims
mp = BytesIO()
im.save(mp, format="TIFF", save_all=True, append_images=imGenerator(ims))
mp.seek(0, os.SEEK_SET)
with Image.open(mp) as reread:
assert reread.n_frames == 3
def test_saving_icc_profile(self, tmp_path):
# Tests saving TIFF with icc_profile set.
# At the time of writing this will only work for non-compressed tiffs
# as libtiff does not support embedded ICC profiles,
# ImageFile._save(..) however does.
im = Image.new("RGB", (1, 1))
im.info["icc_profile"] = "Dummy value"
# Try save-load round trip to make sure both handle icc_profile.
tmpfile = str(tmp_path / "temp.tif")
im.save(tmpfile, "TIFF", compression="raw")
with Image.open(tmpfile) as reloaded:
assert b"Dummy value" == reloaded.info["icc_profile"]
def test_save_icc_profile(self, tmp_path):
im = hopper()
assert "icc_profile" not in im.info
outfile = str(tmp_path / "temp.tif")
icc_profile = b"Dummy value"
im.save(outfile, icc_profile=icc_profile)
with Image.open(outfile) as reloaded:
assert reloaded.info["icc_profile"] == icc_profile
def test_discard_icc_profile(self, tmp_path):
outfile = str(tmp_path / "temp.tif")
with Image.open("Tests/images/icc_profile.png") as im:
assert "icc_profile" in im.info
im.save(outfile, icc_profile=None)
with Image.open(outfile) as reloaded:
assert "icc_profile" not in reloaded.info
def test_close_on_load_exclusive(self, tmp_path):
# similar to test_fd_leak, but runs on unixlike os
tmpfile = str(tmp_path / "temp.tif")
with Image.open("Tests/images/uint16_1_4660.tif") as im:
im.save(tmpfile)
im = Image.open(tmpfile)
fp = im.fp
assert not fp.closed
im.load()
assert fp.closed
def test_close_on_load_nonexclusive(self, tmp_path):
tmpfile = str(tmp_path / "temp.tif")
with Image.open("Tests/images/uint16_1_4660.tif") as im:
im.save(tmpfile)
with open(tmpfile, "rb") as f:
im = Image.open(f)
fp = im.fp
assert not fp.closed
im.load()
assert not fp.closed
# Ignore this UserWarning which triggers for four tags:
# "Possibly corrupt EXIF data. Expecting to read 50404352 bytes but..."
@pytest.mark.filterwarnings("ignore:Possibly corrupt EXIF data")
@pytest.mark.skipif(
not os.path.exists("Tests/images/string_dimension.tiff"),
reason="Extra image files not installed",
)
def test_string_dimension(self):
# Assert that an error is raised if one of the dimensions is a string
with Image.open("Tests/images/string_dimension.tiff") as im:
with pytest.raises(OSError):
im.load()
@pytest.mark.skipif(not is_win32(), reason="Windows only")
class TestFileTiffW32:
def test_fd_leak(self, tmp_path):
tmpfile = str(tmp_path / "temp.tif")
# this is an mmaped file.
with Image.open("Tests/images/uint16_1_4660.tif") as im:
im.save(tmpfile)
im = Image.open(tmpfile)
fp = im.fp
assert not fp.closed
with pytest.raises(OSError):
os.remove(tmpfile)
im.load()
assert fp.closed
# this closes the mmap
im.close()
# this should not fail, as load should have closed the file pointer,
# and close should have closed the mmap
os.remove(tmpfile)