2015-02-06 21:58:07 +03:00
|
|
|
import logging
|
2016-09-16 21:07:25 +03:00
|
|
|
from io import BytesIO
|
2015-02-06 21:58:07 +03:00
|
|
|
import struct
|
2016-11-04 19:09:30 +03:00
|
|
|
import sys
|
2015-02-06 21:58:07 +03:00
|
|
|
|
2014-09-05 13:36:24 +04:00
|
|
|
from helper import unittest, PillowTestCase, hopper, py3
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-08-21 08:43:46 +04:00
|
|
|
from PIL import Image, TiffImagePlugin
|
2017-05-27 23:55:14 +03:00
|
|
|
from PIL.TiffImagePlugin import X_RESOLUTION, Y_RESOLUTION, RESOLUTION_UNIT
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2015-02-06 21:58:07 +03:00
|
|
|
logger = logging.getLogger(__name__)
|
2015-06-06 17:04:39 +03:00
|
|
|
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
class TestFileTiff(PillowTestCase):
|
|
|
|
|
|
|
|
def test_sanity(self):
|
|
|
|
|
2014-10-01 17:50:33 +04:00
|
|
|
filename = self.tempfile("temp.tif")
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2014-10-01 17:50:33 +04:00
|
|
|
hopper("RGB").save(filename)
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2014-10-01 17:50:33 +04:00
|
|
|
im = Image.open(filename)
|
2014-06-10 13:10:47 +04:00
|
|
|
im.load()
|
|
|
|
self.assertEqual(im.mode, "RGB")
|
|
|
|
self.assertEqual(im.size, (128, 128))
|
|
|
|
self.assertEqual(im.format, "TIFF")
|
|
|
|
|
2014-10-01 17:50:33 +04:00
|
|
|
hopper("1").save(filename)
|
|
|
|
im = Image.open(filename)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-10-01 17:50:33 +04:00
|
|
|
hopper("L").save(filename)
|
|
|
|
im = Image.open(filename)
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2014-10-01 17:50:33 +04:00
|
|
|
hopper("P").save(filename)
|
|
|
|
im = Image.open(filename)
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2014-10-01 17:50:33 +04:00
|
|
|
hopper("RGB").save(filename)
|
|
|
|
im = Image.open(filename)
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2014-10-01 17:50:33 +04:00
|
|
|
hopper("I").save(filename)
|
|
|
|
im = Image.open(filename)
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
def test_mac_tiff(self):
|
2016-09-23 14:12:03 +03:00
|
|
|
# Read RGBa images from macOS [@PIL136]
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2014-10-01 17:50:33 +04:00
|
|
|
filename = "Tests/images/pil136.tiff"
|
|
|
|
im = Image.open(filename)
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
self.assertEqual(im.mode, "RGBA")
|
|
|
|
self.assertEqual(im.size, (55, 43))
|
|
|
|
self.assertEqual(im.tile, [('raw', (0, 0, 55, 43), 8, ('RGBa', 0, 1))])
|
|
|
|
im.load()
|
|
|
|
|
2017-08-21 17:10:05 +03:00
|
|
|
def test_16bit_RGBa_tiff(self):
|
|
|
|
im = Image.open("Tests/images/tiff_16bit_RGBa.tiff")
|
|
|
|
|
|
|
|
self.assertEqual(im.mode, "RGBA")
|
|
|
|
self.assertEqual(im.size, (100, 40))
|
|
|
|
self.assertEqual(im.tile, [('tiff_lzw', (0, 0, 100, 40), 50, 'RGBa;16B')])
|
|
|
|
im.load()
|
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_gimp_tiff(self):
|
|
|
|
# Read TIFF JPEG images from GIMP [@PIL168]
|
|
|
|
|
|
|
|
codecs = dir(Image.core)
|
|
|
|
if "jpeg_decoder" not in codecs:
|
|
|
|
self.skipTest("jpeg support not available")
|
|
|
|
|
2014-10-01 17:50:33 +04:00
|
|
|
filename = "Tests/images/pil168.tif"
|
|
|
|
im = Image.open(filename)
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
self.assertEqual(im.mode, "RGB")
|
|
|
|
self.assertEqual(im.size, (256, 256))
|
|
|
|
self.assertEqual(
|
|
|
|
im.tile, [
|
|
|
|
('jpeg', (0, 0, 256, 64), 8, ('RGB', '')),
|
|
|
|
('jpeg', (0, 64, 256, 128), 1215, ('RGB', '')),
|
|
|
|
('jpeg', (0, 128, 256, 192), 2550, ('RGB', '')),
|
|
|
|
('jpeg', (0, 192, 256, 256), 3890, ('RGB', '')),
|
|
|
|
])
|
|
|
|
im.load()
|
|
|
|
|
2015-10-04 00:46:01 +03:00
|
|
|
def test_sampleformat(self):
|
|
|
|
# https://github.com/python-pillow/Pillow/issues/1466
|
|
|
|
im = Image.open("Tests/images/copyleft.tiff")
|
|
|
|
self.assertEqual(im.mode, 'RGB')
|
|
|
|
|
2017-09-01 13:36:51 +03:00
|
|
|
def test_set_legacy_api(self):
|
|
|
|
with self.assertRaises(Exception):
|
|
|
|
ImageFileDirectory_v2.legacy_api = None
|
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_xyres_tiff(self):
|
2014-10-01 17:50:33 +04:00
|
|
|
filename = "Tests/images/pil168.tif"
|
|
|
|
im = Image.open(filename)
|
2015-09-11 20:09:14 +03:00
|
|
|
|
2015-12-10 01:35:35 +03:00
|
|
|
# legacy api
|
2015-12-16 06:30:17 +03:00
|
|
|
self.assertIsInstance(im.tag[X_RESOLUTION][0], tuple)
|
|
|
|
self.assertIsInstance(im.tag[Y_RESOLUTION][0], tuple)
|
2015-09-11 20:09:14 +03:00
|
|
|
|
2016-02-05 01:57:13 +03:00
|
|
|
# v2 api
|
2016-04-21 23:13:10 +03:00
|
|
|
self.assertIsInstance(im.tag_v2[X_RESOLUTION],
|
|
|
|
TiffImagePlugin.IFDRational)
|
|
|
|
self.assertIsInstance(im.tag_v2[Y_RESOLUTION],
|
|
|
|
TiffImagePlugin.IFDRational)
|
2015-09-11 20:09:14 +03:00
|
|
|
|
|
|
|
self.assertEqual(im.info['dpi'], (72., 72.))
|
|
|
|
|
2017-01-19 19:24:28 +03:00
|
|
|
def test_xyres_fallback_tiff(self):
|
|
|
|
filename = "Tests/images/compression.tif"
|
|
|
|
im = Image.open(filename)
|
|
|
|
|
|
|
|
# v2 api
|
|
|
|
self.assertIsInstance(im.tag_v2[X_RESOLUTION],
|
|
|
|
TiffImagePlugin.IFDRational)
|
|
|
|
self.assertIsInstance(im.tag_v2[Y_RESOLUTION],
|
|
|
|
TiffImagePlugin.IFDRational)
|
|
|
|
self.assertRaises(KeyError,
|
|
|
|
lambda: im.tag_v2[RESOLUTION_UNIT])
|
|
|
|
|
|
|
|
# Legacy.
|
|
|
|
self.assertEqual(im.info['resolution'], (100., 100.))
|
|
|
|
# Fallback "inch".
|
|
|
|
self.assertEqual(im.info['dpi'], (100., 100.))
|
|
|
|
|
2015-09-14 15:10:27 +03:00
|
|
|
def test_int_resolution(self):
|
|
|
|
filename = "Tests/images/pil168.tif"
|
|
|
|
im = Image.open(filename)
|
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Try to read a file where X,Y_RESOLUTION are ints
|
2015-09-14 15:10:27 +03:00
|
|
|
im.tag_v2[X_RESOLUTION] = 71
|
|
|
|
im.tag_v2[Y_RESOLUTION] = 71
|
2014-06-10 13:10:47 +04:00
|
|
|
im._setup()
|
2015-09-14 15:10:27 +03:00
|
|
|
self.assertEqual(im.info['dpi'], (71., 71.))
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2016-09-16 21:07:25 +03:00
|
|
|
def test_save_setting_missing_resolution(self):
|
|
|
|
b = BytesIO()
|
|
|
|
Image.open("Tests/images/10ct_32bit_128.tiff").save(
|
|
|
|
b, format="tiff", resolution=123.45)
|
|
|
|
im = Image.open(b)
|
|
|
|
self.assertEqual(float(im.tag_v2[X_RESOLUTION]), 123.45)
|
|
|
|
self.assertEqual(float(im.tag_v2[Y_RESOLUTION]), 123.45)
|
|
|
|
|
2015-07-13 16:37:44 +03:00
|
|
|
def test_invalid_file(self):
|
|
|
|
invalid_file = "Tests/images/flower.jpg"
|
|
|
|
|
|
|
|
self.assertRaises(SyntaxError,
|
2017-09-01 14:05:40 +03:00
|
|
|
TiffImagePlugin.TiffImageFile, invalid_file)
|
2015-07-13 16:37:44 +03:00
|
|
|
|
2016-10-22 20:55:50 +03:00
|
|
|
TiffImagePlugin.PREFIXES.append(b"\xff\xd8\xff\xe0")
|
2016-01-02 04:27:40 +03:00
|
|
|
self.assertRaises(SyntaxError,
|
2017-09-01 14:05:40 +03:00
|
|
|
TiffImagePlugin.TiffImageFile, invalid_file)
|
2016-01-02 04:27:40 +03:00
|
|
|
TiffImagePlugin.PREFIXES.pop()
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2015-06-06 17:04:39 +03:00
|
|
|
def test_bad_exif(self):
|
2015-09-15 02:52:02 +03:00
|
|
|
i = Image.open('Tests/images/hopper_bad_exif.jpg')
|
2015-06-06 17:04:39 +03:00
|
|
|
try:
|
2015-12-02 08:49:30 +03:00
|
|
|
self.assert_warning(UserWarning, i._getexif)
|
2015-06-06 17:04:39 +03:00
|
|
|
except struct.error:
|
2015-09-11 20:45:10 +03:00
|
|
|
self.fail(
|
2015-12-30 23:27:27 +03:00
|
|
|
"Bad EXIF data passed incorrect values to _binary unpack")
|
2015-06-06 17:04:39 +03:00
|
|
|
|
2015-12-01 20:14:32 +03:00
|
|
|
def test_save_rgba(self):
|
|
|
|
im = hopper("RGBA")
|
|
|
|
outfile = self.tempfile("temp.tif")
|
|
|
|
im.save(outfile)
|
|
|
|
|
2015-07-13 16:37:44 +03:00
|
|
|
def test_save_unsupported_mode(self):
|
|
|
|
im = hopper("HSV")
|
|
|
|
outfile = self.tempfile("temp.tif")
|
2017-09-01 14:05:40 +03:00
|
|
|
self.assertRaises(IOError, im.save, outfile)
|
2015-06-06 17:04:39 +03:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_little_endian(self):
|
|
|
|
im = Image.open('Tests/images/16bit.cropped.tif')
|
|
|
|
self.assertEqual(im.getpixel((0, 0)), 480)
|
|
|
|
self.assertEqual(im.mode, 'I;16')
|
|
|
|
|
|
|
|
b = im.tobytes()
|
|
|
|
# Bytes are in image native order (little endian)
|
|
|
|
if py3:
|
|
|
|
self.assertEqual(b[0], ord(b'\xe0'))
|
|
|
|
self.assertEqual(b[1], ord(b'\x01'))
|
|
|
|
else:
|
|
|
|
self.assertEqual(b[0], b'\xe0')
|
|
|
|
self.assertEqual(b[1], b'\x01')
|
|
|
|
|
|
|
|
def test_big_endian(self):
|
|
|
|
im = Image.open('Tests/images/16bit.MM.cropped.tif')
|
|
|
|
self.assertEqual(im.getpixel((0, 0)), 480)
|
|
|
|
self.assertEqual(im.mode, 'I;16B')
|
|
|
|
|
|
|
|
b = im.tobytes()
|
|
|
|
|
|
|
|
# Bytes are in image native order (big endian)
|
|
|
|
if py3:
|
|
|
|
self.assertEqual(b[0], ord(b'\x01'))
|
|
|
|
self.assertEqual(b[1], ord(b'\xe0'))
|
|
|
|
else:
|
|
|
|
self.assertEqual(b[0], b'\x01')
|
|
|
|
self.assertEqual(b[1], b'\xe0')
|
|
|
|
|
2015-09-19 03:12:00 +03:00
|
|
|
def test_16bit_s(self):
|
|
|
|
im = Image.open('Tests/images/16bit.s.tif')
|
|
|
|
im.load()
|
2017-09-20 12:26:40 +03:00
|
|
|
self.assertEqual(im.mode, 'I')
|
|
|
|
self.assertEqual(im.getpixel((0,0)),32767)
|
|
|
|
self.assertEqual(im.getpixel((0,1)),0)
|
2015-09-19 03:12:00 +03:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_12bit_rawmode(self):
|
|
|
|
""" Are we generating the same interpretation
|
|
|
|
of the image as Imagemagick is? """
|
|
|
|
|
|
|
|
im = Image.open('Tests/images/12bit.cropped.tif')
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
im2 = Image.open('Tests/images/12in16bit.tif')
|
|
|
|
|
2015-02-06 21:58:07 +03:00
|
|
|
logger.debug("%s", [img.getpixel((0, idx))
|
|
|
|
for img in [im, im2] for idx in range(3)])
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
self.assert_image_equal(im, im2)
|
|
|
|
|
|
|
|
def test_32bit_float(self):
|
2015-10-14 16:49:03 +03:00
|
|
|
# Issue 614, specific 32-bit float format
|
2014-06-10 13:10:47 +04:00
|
|
|
path = 'Tests/images/10ct_32bit_128.tiff'
|
|
|
|
im = Image.open(path)
|
|
|
|
im.load()
|
|
|
|
|
|
|
|
self.assertEqual(im.getpixel((0, 0)), -0.4526388943195343)
|
|
|
|
self.assertEqual(
|
|
|
|
im.getextrema(), (-3.140936851501465, 3.140684127807617))
|
|
|
|
|
2015-06-07 18:01:34 +03:00
|
|
|
def test_n_frames(self):
|
2017-08-18 13:20:27 +03:00
|
|
|
for path, n_frames in [
|
|
|
|
['Tests/images/multipage-lastframe.tif', 1],
|
|
|
|
['Tests/images/multipage.tiff', 3]
|
|
|
|
]:
|
|
|
|
# Test is_animated before n_frames
|
|
|
|
im = Image.open(path)
|
|
|
|
self.assertEqual(im.is_animated, n_frames != 1)
|
|
|
|
|
|
|
|
# Test is_animated after n_frames
|
|
|
|
im = Image.open(path)
|
|
|
|
self.assertEqual(im.n_frames, n_frames)
|
|
|
|
self.assertEqual(im.is_animated, n_frames != 1)
|
2015-06-07 18:01:34 +03:00
|
|
|
|
2015-06-18 17:49:18 +03:00
|
|
|
def test_eoferror(self):
|
|
|
|
im = Image.open('Tests/images/multipage-lastframe.tif')
|
|
|
|
n_frames = im.n_frames
|
2017-09-06 06:19:33 +03:00
|
|
|
|
|
|
|
# Test seeking past the last frame
|
|
|
|
self.assertRaises(EOFError, im.seek, n_frames)
|
|
|
|
self.assertLess(im.tell(), n_frames)
|
|
|
|
|
|
|
|
# Test that seeking to the last frame does not raise an error
|
|
|
|
im.seek(n_frames-1)
|
2015-06-07 18:01:34 +03:00
|
|
|
|
2014-08-21 08:43:46 +04:00
|
|
|
def test_multipage(self):
|
|
|
|
# issue #862
|
|
|
|
im = Image.open('Tests/images/multipage.tiff')
|
2014-10-02 11:45:41 +04:00
|
|
|
# file is a multipage tiff: 10x10 green, 10x10 red, 20x20 blue
|
2014-08-21 08:43:46 +04:00
|
|
|
|
|
|
|
im.seek(0)
|
2014-10-26 12:24:57 +03:00
|
|
|
self.assertEqual(im.size, (10, 10))
|
|
|
|
self.assertEqual(im.convert('RGB').getpixel((0, 0)), (0, 128, 0))
|
2014-08-21 08:43:46 +04:00
|
|
|
|
|
|
|
im.seek(1)
|
|
|
|
im.load()
|
2014-10-26 12:24:57 +03:00
|
|
|
self.assertEqual(im.size, (10, 10))
|
|
|
|
self.assertEqual(im.convert('RGB').getpixel((0, 0)), (255, 0, 0))
|
2014-08-21 08:43:46 +04:00
|
|
|
|
|
|
|
im.seek(2)
|
|
|
|
im.load()
|
2014-10-26 12:24:57 +03:00
|
|
|
self.assertEqual(im.size, (20, 20))
|
|
|
|
self.assertEqual(im.convert('RGB').getpixel((0, 0)), (0, 0, 255))
|
2014-08-21 08:43:46 +04:00
|
|
|
|
|
|
|
def test_multipage_last_frame(self):
|
|
|
|
im = Image.open('Tests/images/multipage-lastframe.tif')
|
|
|
|
im.load()
|
2014-10-26 12:24:57 +03:00
|
|
|
self.assertEqual(im.size, (20, 20))
|
|
|
|
self.assertEqual(im.convert('RGB').getpixel((0, 0)), (0, 0, 255))
|
2014-08-21 08:43:46 +04:00
|
|
|
|
2014-07-27 23:18:42 +04:00
|
|
|
def test___str__(self):
|
2014-10-01 17:50:33 +04:00
|
|
|
filename = "Tests/images/pil136.tiff"
|
|
|
|
im = Image.open(filename)
|
2014-07-27 23:18:42 +04:00
|
|
|
|
|
|
|
# Act
|
|
|
|
ret = str(im.ifd)
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
self.assertIsInstance(ret, str)
|
2015-02-08 20:09:39 +03:00
|
|
|
|
2016-04-03 16:41:28 +03:00
|
|
|
def test_dict(self):
|
|
|
|
# Arrange
|
|
|
|
filename = "Tests/images/pil136.tiff"
|
|
|
|
im = Image.open(filename)
|
2015-02-08 20:09:39 +03:00
|
|
|
|
2015-09-11 20:09:14 +03:00
|
|
|
# v2 interface
|
2016-04-03 16:41:28 +03:00
|
|
|
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}
|
|
|
|
self.assertEqual(dict(im.tag_v2), v2_tags)
|
2015-12-10 01:35:35 +03:00
|
|
|
|
2015-09-11 20:09:14 +03:00
|
|
|
# legacy interface
|
2016-04-03 16:41:28 +03:00
|
|
|
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,)}
|
|
|
|
self.assertEqual(dict(im.tag), legacy_tags)
|
2014-07-27 23:18:42 +04:00
|
|
|
|
|
|
|
def test__delitem__(self):
|
2014-10-01 17:50:33 +04:00
|
|
|
filename = "Tests/images/pil136.tiff"
|
|
|
|
im = Image.open(filename)
|
2016-04-03 16:41:28 +03:00
|
|
|
len_before = len(dict(im.ifd))
|
2014-07-27 23:18:42 +04:00
|
|
|
del im.ifd[256]
|
2016-04-03 16:41:28 +03:00
|
|
|
len_after = len(dict(im.ifd))
|
2014-07-27 23:18:42 +04:00
|
|
|
self.assertEqual(len_before, len_after + 1)
|
|
|
|
|
|
|
|
def test_load_byte(self):
|
2015-03-01 06:44:38 +03:00
|
|
|
for legacy_api in [False, True]:
|
2015-09-11 20:09:14 +03:00
|
|
|
ifd = TiffImagePlugin.ImageFileDirectory_v2()
|
2015-03-01 06:44:38 +03:00
|
|
|
data = b"abc"
|
2015-09-14 14:35:09 +03:00
|
|
|
ret = ifd.load_byte(data, legacy_api)
|
2016-06-26 14:06:56 +03:00
|
|
|
self.assertEqual(ret, b"abc")
|
2014-07-27 23:18:42 +04:00
|
|
|
|
|
|
|
def test_load_string(self):
|
2015-09-11 20:09:14 +03:00
|
|
|
ifd = TiffImagePlugin.ImageFileDirectory_v2()
|
2014-07-27 23:18:42 +04:00
|
|
|
data = b"abc\0"
|
2015-09-14 14:35:09 +03:00
|
|
|
ret = ifd.load_string(data, False)
|
2014-07-27 23:18:42 +04:00
|
|
|
self.assertEqual(ret, "abc")
|
|
|
|
|
|
|
|
def test_load_float(self):
|
2015-09-11 20:09:14 +03:00
|
|
|
ifd = TiffImagePlugin.ImageFileDirectory_v2()
|
2014-07-27 23:18:42 +04:00
|
|
|
data = b"abcdabcd"
|
2015-09-14 14:35:09 +03:00
|
|
|
ret = ifd.load_float(data, False)
|
2014-07-27 23:18:42 +04:00
|
|
|
self.assertEqual(ret, (1.6777999408082104e+22, 1.6777999408082104e+22))
|
|
|
|
|
|
|
|
def test_load_double(self):
|
2015-09-11 20:09:14 +03:00
|
|
|
ifd = TiffImagePlugin.ImageFileDirectory_v2()
|
2014-07-27 23:18:42 +04:00
|
|
|
data = b"abcdefghabcdefgh"
|
2015-09-14 14:35:09 +03:00
|
|
|
ret = ifd.load_double(data, False)
|
2014-07-27 23:18:42 +04:00
|
|
|
self.assertEqual(ret, (8.540883223036124e+194, 8.540883223036124e+194))
|
|
|
|
|
|
|
|
def test_seek(self):
|
2014-10-01 17:50:33 +04:00
|
|
|
filename = "Tests/images/pil136.tiff"
|
|
|
|
im = Image.open(filename)
|
2017-09-30 06:32:43 +03:00
|
|
|
im.seek(0)
|
2014-07-27 23:18:42 +04:00
|
|
|
self.assertEqual(im.tell(), 0)
|
|
|
|
|
|
|
|
def test_seek_eof(self):
|
2014-10-01 17:50:33 +04:00
|
|
|
filename = "Tests/images/pil136.tiff"
|
|
|
|
im = Image.open(filename)
|
2014-07-27 23:18:42 +04:00
|
|
|
self.assertEqual(im.tell(), 0)
|
2017-09-30 06:32:43 +03:00
|
|
|
self.assertRaises(EOFError, im.seek, -1)
|
2017-09-01 14:05:40 +03:00
|
|
|
self.assertRaises(EOFError, im.seek, 1)
|
2014-07-27 23:18:42 +04:00
|
|
|
|
2014-12-29 18:48:01 +03:00
|
|
|
def test__limit_rational_int(self):
|
|
|
|
from PIL.TiffImagePlugin import _limit_rational
|
2014-07-27 23:18:42 +04:00
|
|
|
value = 34
|
2014-12-29 18:48:01 +03:00
|
|
|
ret = _limit_rational(value, 65536)
|
2014-07-27 23:18:42 +04:00
|
|
|
self.assertEqual(ret, (34, 1))
|
|
|
|
|
2014-12-29 18:48:01 +03:00
|
|
|
def test__limit_rational_float(self):
|
|
|
|
from PIL.TiffImagePlugin import _limit_rational
|
2014-07-27 23:18:42 +04:00
|
|
|
value = 22.3
|
2014-12-29 18:48:01 +03:00
|
|
|
ret = _limit_rational(value, 65536)
|
|
|
|
self.assertEqual(ret, (223, 10))
|
2014-07-27 23:18:42 +04:00
|
|
|
|
2014-10-02 11:43:22 +04:00
|
|
|
def test_4bit(self):
|
|
|
|
test_file = "Tests/images/hopper_gray_4bpp.tif"
|
2014-10-02 11:45:41 +04:00
|
|
|
original = hopper("L")
|
2014-10-02 11:43:22 +04:00
|
|
|
im = Image.open(test_file)
|
|
|
|
self.assertEqual(im.size, (128, 128))
|
|
|
|
self.assertEqual(im.mode, "L")
|
2014-10-02 11:45:41 +04:00
|
|
|
self.assert_image_similar(im, original, 7.3)
|
2014-10-02 11:43:22 +04:00
|
|
|
|
2016-03-29 13:56:37 +03:00
|
|
|
def test_gray_semibyte_per_pixel(self):
|
|
|
|
test_files = (
|
|
|
|
(
|
2016-04-21 23:13:10 +03:00
|
|
|
24.8, # epsilon
|
|
|
|
( # group
|
2016-03-29 13:56:37 +03:00
|
|
|
"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",
|
|
|
|
)
|
|
|
|
),
|
|
|
|
(
|
2016-04-21 23:13:10 +03:00
|
|
|
7.3, # epsilon
|
|
|
|
( # group
|
2016-03-29 13:56:37 +03:00
|
|
|
"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:
|
|
|
|
im = Image.open(group[0])
|
|
|
|
self.assertEqual(im.size, (128, 128))
|
|
|
|
self.assertEqual(im.mode, "L")
|
|
|
|
self.assert_image_similar(im, original, epsilon)
|
|
|
|
for file in group[1:]:
|
|
|
|
im2 = Image.open(file)
|
|
|
|
self.assertEqual(im2.size, (128, 128))
|
|
|
|
self.assertEqual(im2.mode, "L")
|
|
|
|
self.assert_image_equal(im, im2)
|
|
|
|
|
2014-12-28 17:30:12 +03:00
|
|
|
def test_with_underscores(self):
|
|
|
|
kwargs = {'resolution_unit': 'inch',
|
|
|
|
'x_resolution': 72,
|
|
|
|
'y_resolution': 36}
|
|
|
|
filename = self.tempfile("temp.tif")
|
|
|
|
hopper("RGB").save(filename, **kwargs)
|
|
|
|
from PIL.TiffImagePlugin import X_RESOLUTION, Y_RESOLUTION
|
|
|
|
im = Image.open(filename)
|
2015-09-11 20:09:14 +03:00
|
|
|
|
|
|
|
# legacy interface
|
|
|
|
self.assertEqual(im.tag[X_RESOLUTION][0][0], 72)
|
|
|
|
self.assertEqual(im.tag[Y_RESOLUTION][0][0], 36)
|
|
|
|
|
|
|
|
# v2 interface
|
|
|
|
self.assertEqual(im.tag_v2[X_RESOLUTION], 72)
|
|
|
|
self.assertEqual(im.tag_v2[Y_RESOLUTION], 36)
|
2014-12-28 17:30:12 +03:00
|
|
|
|
2016-04-21 23:10:08 +03:00
|
|
|
def test_lzw(self):
|
|
|
|
# Act
|
|
|
|
im = Image.open("Tests/images/hopper_lzw.tif")
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
self.assertEqual(im.mode, 'RGB')
|
|
|
|
self.assertEqual(im.size, (128, 128))
|
|
|
|
self.assertEqual(im.format, "TIFF")
|
|
|
|
im2 = hopper()
|
|
|
|
self.assert_image_similar(im, im2, 5)
|
|
|
|
|
2016-08-25 22:49:40 +03:00
|
|
|
def test_roundtrip_tiff_uint16(self):
|
2016-04-11 22:35:16 +03:00
|
|
|
# Test an image of all '0' values
|
|
|
|
pixel_value = 0x1234
|
2016-06-25 16:50:40 +03:00
|
|
|
infile = "Tests/images/uint16_1_4660.tif"
|
2016-04-11 22:35:16 +03:00
|
|
|
im = Image.open(infile)
|
|
|
|
self.assertEqual(im.getpixel((0, 0)), pixel_value)
|
|
|
|
|
2016-08-25 22:49:40 +03:00
|
|
|
tmpfile = self.tempfile("temp.tif")
|
|
|
|
im.save(tmpfile)
|
2016-09-03 05:23:42 +03:00
|
|
|
|
2016-08-25 22:49:40 +03:00
|
|
|
reloaded = Image.open(tmpfile)
|
2016-09-03 05:23:42 +03:00
|
|
|
|
2016-08-25 22:49:40 +03:00
|
|
|
self.assert_image_equal(im, reloaded)
|
2016-09-03 05:23:42 +03:00
|
|
|
|
2016-09-29 04:16:04 +03:00
|
|
|
def test_tiff_save_all(self):
|
|
|
|
import io
|
|
|
|
import os
|
|
|
|
|
|
|
|
mp = io.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:
|
|
|
|
self.assertEqual(im.n_frames, 3)
|
2016-04-21 23:10:08 +03:00
|
|
|
|
Allow to save tiff stacks from separate images
This is a quick solution that will allow to save tiff stacks from
separate images, e.g. from Numpy arrays.
Previously, tiff stacks could be saved only from multiframe images.
This behavior is similar to what is possible now with GIFs.
Note however, that for correct results, all the appended images should
have the same encoder{info,config} properties.
Example:
import numpy as np
from PIL import Image
a = np.ones((100,100,100), dtype=np.uint8)
imlist = []
for m in a:
imlist.append(Image.fromarray(m))
imlist[0].save("test.tif", compression="tiff_deflate", save_all=True,
append_images=imlist[1:])
(Should result in a 100-frame, 100x100 tiff stack.)
Signed-off-by: Leonid Bloch <leonid.bloch@esrf.fr>
2017-02-16 03:54:43 +03:00
|
|
|
# Test appending images
|
|
|
|
mp = io.BytesIO()
|
|
|
|
im = Image.new('RGB', (100, 100), '#f00')
|
|
|
|
ims = [Image.new('RGB', (100, 100), color) for color
|
|
|
|
in ['#0f0', '#00f']]
|
|
|
|
im.save(mp, format="TIFF", save_all=True, append_images=ims)
|
|
|
|
|
|
|
|
mp.seek(0, os.SEEK_SET)
|
|
|
|
reread = Image.open(mp)
|
|
|
|
self.assertEqual(reread.n_frames, 3)
|
|
|
|
|
|
|
|
|
2016-08-22 13:47:49 +03:00
|
|
|
def test_saving_icc_profile(self):
|
|
|
|
# Tests saving TIFF with icc_profile set.
|
|
|
|
# At the time of writing this will only work for non-compressed tiffs
|
2017-05-27 23:55:14 +03:00
|
|
|
# as libtiff does not support embedded ICC profiles,
|
|
|
|
# ImageFile._save(..) however does.
|
2016-08-22 13:47:49 +03:00
|
|
|
im = Image.new('RGB', (1, 1))
|
|
|
|
im.info['icc_profile'] = 'Dummy value'
|
2016-04-01 13:47:28 +03:00
|
|
|
|
2016-08-22 13:47:49 +03:00
|
|
|
# Try save-load round trip to make sure both handle icc_profile.
|
|
|
|
tmpfile = self.tempfile('temp.tif')
|
|
|
|
im.save(tmpfile, 'TIFF', compression='raw')
|
|
|
|
reloaded = Image.open(tmpfile)
|
2016-04-01 13:47:28 +03:00
|
|
|
|
2016-08-22 13:47:49 +03:00
|
|
|
self.assertEqual(b'Dummy value', reloaded.info['icc_profile'])
|
|
|
|
|
2017-04-04 10:27:20 +03:00
|
|
|
def test_close_on_load_exclusive(self):
|
|
|
|
# similar to test_fd_leak, but runs on unixlike os
|
2017-01-02 23:56:19 +03:00
|
|
|
tmpfile = self.tempfile("temp.tif")
|
|
|
|
|
|
|
|
with Image.open("Tests/images/uint16_1_4660.tif") as im:
|
|
|
|
im.save(tmpfile)
|
|
|
|
|
|
|
|
im = Image.open(tmpfile)
|
|
|
|
fp = im.fp
|
|
|
|
self.assertFalse(fp.closed)
|
|
|
|
im.load()
|
|
|
|
self.assertTrue(fp.closed)
|
|
|
|
|
2017-04-04 10:27:20 +03:00
|
|
|
def test_close_on_load_nonexclusive(self):
|
|
|
|
tmpfile = self.tempfile("temp.tif")
|
2017-05-27 21:55:42 +03:00
|
|
|
|
2017-04-04 10:27:20 +03:00
|
|
|
with Image.open("Tests/images/uint16_1_4660.tif") as im:
|
|
|
|
im.save(tmpfile)
|
|
|
|
|
2017-08-26 03:20:30 +03:00
|
|
|
with open(tmpfile, 'rb') as f:
|
|
|
|
im = Image.open(f)
|
|
|
|
fp = im.fp
|
|
|
|
self.assertFalse(fp.closed)
|
|
|
|
im.load()
|
|
|
|
self.assertFalse(fp.closed)
|
2017-04-04 10:27:20 +03:00
|
|
|
|
2016-11-04 19:09:30 +03:00
|
|
|
@unittest.skipUnless(sys.platform.startswith('win32'), "Windows only")
|
|
|
|
class TestFileTiffW32(PillowTestCase):
|
|
|
|
def test_fd_leak(self):
|
|
|
|
tmpfile = self.tempfile("temp.tif")
|
|
|
|
import os
|
|
|
|
|
2017-05-27 21:55:42 +03:00
|
|
|
# this is an mmaped file.
|
2016-11-04 19:09:30 +03:00
|
|
|
with Image.open("Tests/images/uint16_1_4660.tif") as im:
|
|
|
|
im.save(tmpfile)
|
|
|
|
|
|
|
|
im = Image.open(tmpfile)
|
2017-01-02 23:56:19 +03:00
|
|
|
fp = im.fp
|
|
|
|
self.assertFalse(fp.closed)
|
2017-09-01 14:05:40 +03:00
|
|
|
self.assertRaises(Exception, os.remove, tmpfile)
|
2017-01-02 23:37:41 +03:00
|
|
|
im.load()
|
2017-01-02 23:56:19 +03:00
|
|
|
self.assertTrue(fp.closed)
|
2017-04-03 18:05:56 +03:00
|
|
|
|
|
|
|
# this closes the mmap
|
|
|
|
im.close()
|
2017-05-27 21:55:42 +03:00
|
|
|
|
2017-04-03 18:05:56 +03:00
|
|
|
# this should not fail, as load should have closed the file pointer,
|
|
|
|
# and close should have closed the mmap
|
2016-11-04 19:09:30 +03:00
|
|
|
os.remove(tmpfile)
|
2016-08-22 13:47:49 +03:00
|
|
|
|
2017-05-27 23:55:14 +03:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|