Merge pull request #1984 from wiredfool/pr_1824

Pr 1824 rebase
This commit is contained in:
wiredfool 2016-06-25 15:01:34 +01:00 committed by GitHub
commit 95874635e1
4 changed files with 74 additions and 25 deletions

View File

@ -2210,10 +2210,14 @@ _fromarray_typemap = {
# ((1, 1), "|b1"): ("1", "1"), # broken # ((1, 1), "|b1"): ("1", "1"), # broken
((1, 1), "|u1"): ("L", "L"), ((1, 1), "|u1"): ("L", "L"),
((1, 1), "|i1"): ("I", "I;8"), ((1, 1), "|i1"): ("I", "I;8"),
((1, 1), "<i2"): ("I", "I;16"), ((1, 1), "<u2"): ("I", "I;16"),
((1, 1), ">i2"): ("I", "I;16B"), ((1, 1), ">u2"): ("I", "I;16B"),
((1, 1), "<i4"): ("I", "I;32"), ((1, 1), "<i2"): ("I", "I;16S"),
((1, 1), ">i4"): ("I", "I;32B"), ((1, 1), ">i2"): ("I", "I;16BS"),
((1, 1), "<u4"): ("I", "I;32"),
((1, 1), ">u4"): ("I", "I;32B"),
((1, 1), "<i4"): ("I", "I;32S"),
((1, 1), ">i4"): ("I", "I;32BS"),
((1, 1), "<f4"): ("F", "F;32F"), ((1, 1), "<f4"): ("F", "F;32F"),
((1, 1), ">f4"): ("F", "F;32BF"), ((1, 1), ">f4"): ("F", "F;32BF"),
((1, 1), "<f8"): ("F", "F;64F"), ((1, 1), "<f8"): ("F", "F;64F"),

Binary file not shown.

View File

@ -463,6 +463,13 @@ class TestFileTiff(PillowTestCase):
im2 = hopper() im2 = hopper()
self.assert_image_similar(im, im2, 5) self.assert_image_similar(im, im2, 5)
def test_open_tiff_uint16(self):
# Test an image of all '0' values
pixel_value = 0x1234
infile = "Tests/images/uint16_1_4660.tif"
im = Image.open(infile)
self.assertEqual(im.getpixel((0, 0)), pixel_value)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View File

@ -10,6 +10,7 @@ except ImportError:
# Skip via setUp() # Skip via setUp()
pass pass
TEST_IMAGE_SIZE = (10, 10)
class TestNumpy(PillowTestCase): class TestNumpy(PillowTestCase):
@ -29,40 +30,57 @@ class TestNumpy(PillowTestCase):
else: else:
data = list(range(100)) data = list(range(100))
a = numpy.array(data, dtype=dtype) a = numpy.array(data, dtype=dtype)
a.shape = 10, 10 a.shape = TEST_IMAGE_SIZE
i = Image.fromarray(a) i = Image.fromarray(a)
if list(i.getdata()) != data: if list(i.getdata()) != data:
print("data mismatch for", dtype) print("data mismatch for", dtype)
else: else:
data = list(range(100)) data = list(range(100))
a = numpy.array([[x]*bands for x in data], dtype=dtype) a = numpy.array([[x]*bands for x in data], dtype=dtype)
a.shape = 10, 10, bands a.shape = TEST_IMAGE_SIZE[0], TEST_IMAGE_SIZE[1], bands
i = Image.fromarray(a) i = Image.fromarray(a)
if list(i.split()[0].getdata()) != list(range(100)): if list(i.split()[0].getdata()) != list(range(100)):
print("data mismatch for", dtype) print("data mismatch for", dtype)
# print dtype, list(i.getdata()) # print dtype, list(i.getdata())
return i return i
# self.assert_image(to_image(numpy.bool, boolean=1), "1", (10, 10)) # Check supported 1-bit integer formats
# self.assert_image(to_image(numpy.bool8, boolean=1), "1", (10, 10)) self.assertRaises(TypeError, lambda: to_image(numpy.bool))
self.assertRaises(TypeError, lambda: to_image(numpy.bool8))
self.assertRaises(TypeError, lambda: to_image(numpy.uint)) # Check supported 8-bit integer formats
self.assert_image(to_image(numpy.uint8), "L", (10, 10)) self.assert_image(to_image(numpy.uint8), "L", TEST_IMAGE_SIZE)
self.assertRaises(TypeError, lambda: to_image(numpy.uint16)) self.assert_image(to_image(numpy.uint8, 3), "RGB", TEST_IMAGE_SIZE)
self.assertRaises(TypeError, lambda: to_image(numpy.uint32)) self.assert_image(to_image(numpy.uint8, 4), "RGBA", TEST_IMAGE_SIZE)
self.assertRaises(TypeError, lambda: to_image(numpy.uint64)) self.assert_image(to_image(numpy.int8), "I", TEST_IMAGE_SIZE)
self.assert_image(to_image(numpy.int8), "I", (10, 10)) # Check non-fixed-size integer types
if Image._ENDIAN == '<': # Little endian # These may fail, depending on the platform, since we have no native
self.assert_image(to_image(numpy.int16), "I;16", (10, 10)) # 64 bit int image types.
# self.assert_image(to_image(numpy.uint), "I", TEST_IMAGE_SIZE)
# self.assert_image(to_image(numpy.int), "I", TEST_IMAGE_SIZE)
# Check 16-bit integer formats
if Image._ENDIAN == '<':
self.assert_image(to_image(numpy.uint16), "I;16", TEST_IMAGE_SIZE)
else: else:
self.assert_image(to_image(numpy.int16), "I;16B", (10, 10)) self.assert_image(to_image(numpy.uint16), "I;16B", TEST_IMAGE_SIZE)
self.assert_image(to_image(numpy.int32), "I", (10, 10))
self.assert_image(to_image(numpy.int16), "I", TEST_IMAGE_SIZE)
# Check 32-bit integer formats
self.assert_image(to_image(numpy.uint32), "I", TEST_IMAGE_SIZE)
self.assert_image(to_image(numpy.int32), "I", TEST_IMAGE_SIZE)
# Check 64-bit integer formats
self.assertRaises(TypeError, lambda: to_image(numpy.uint64))
self.assertRaises(TypeError, lambda: to_image(numpy.int64)) self.assertRaises(TypeError, lambda: to_image(numpy.int64))
self.assert_image(to_image(numpy.float), "F", (10, 10)) # Check floating-point formats
self.assert_image(to_image(numpy.float32), "F", (10, 10)) self.assert_image(to_image(numpy.float), "F", TEST_IMAGE_SIZE)
self.assert_image(to_image(numpy.float64), "F", (10, 10)) self.assertRaises(TypeError, lambda: to_image(numpy.float16))
self.assert_image(to_image(numpy.float32), "F", TEST_IMAGE_SIZE)
self.assert_image(to_image(numpy.float64), "F", TEST_IMAGE_SIZE)
self.assert_image(to_image(numpy.uint8, 2), "LA", (10, 10)) self.assert_image(to_image(numpy.uint8, 2), "LA", (10, 10))
self.assert_image(to_image(numpy.uint8, 3), "RGB", (10, 10)) self.assert_image(to_image(numpy.uint8, 3), "RGB", (10, 10))
@ -71,10 +89,15 @@ class TestNumpy(PillowTestCase):
# based on an erring example at # based on an erring example at
# http://stackoverflow.com/questions/10854903/what-is-causing-dimension-dependent-attributeerror-in-pil-fromarray-function # http://stackoverflow.com/questions/10854903/what-is-causing-dimension-dependent-attributeerror-in-pil-fromarray-function
def test_3d_array(self): def test_3d_array(self):
a = numpy.ones((10, 10, 10), dtype=numpy.uint8) size = (5, TEST_IMAGE_SIZE[0], TEST_IMAGE_SIZE[1])
self.assert_image(Image.fromarray(a[1, :, :]), "L", (10, 10)) a = numpy.ones(size, dtype=numpy.uint8)
self.assert_image(Image.fromarray(a[:, 1, :]), "L", (10, 10)) self.assert_image(Image.fromarray(a[1, :, :]), "L", TEST_IMAGE_SIZE)
self.assert_image(Image.fromarray(a[:, :, 1]), "L", (10, 10)) size = (TEST_IMAGE_SIZE[0], 5, TEST_IMAGE_SIZE[1])
a = numpy.ones(size, dtype=numpy.uint8)
self.assert_image(Image.fromarray(a[:, 1, :]), "L", TEST_IMAGE_SIZE)
size = (TEST_IMAGE_SIZE[0], TEST_IMAGE_SIZE[1], 5)
a = numpy.ones(size, dtype=numpy.uint8)
self.assert_image(Image.fromarray(a[:, :, 1]), "L", TEST_IMAGE_SIZE)
def _test_img_equals_nparray(self, img, np): def _test_img_equals_nparray(self, img, np):
np_size = np.shape[1], np.shape[0] np_size = np.shape[1], np.shape[0]
@ -90,6 +113,21 @@ class TestNumpy(PillowTestCase):
self._test_img_equals_nparray(img, np_img) self._test_img_equals_nparray(img, np_img)
self.assertEqual(np_img.dtype, numpy.dtype('<u2')) self.assertEqual(np_img.dtype, numpy.dtype('<u2'))
def test_save_tiff_uint16(self):
'''
Open a single-channel uint16 greyscale image and verify that it can be saved without
losing precision.
'''
tmpfile = self.tempfile("temp.tif")
pixel_value = 0x1234
filename = "Tests/images/uint16_1_4660.tif"
a = numpy.array([pixel_value] * TEST_IMAGE_SIZE[0] * TEST_IMAGE_SIZE[1], dtype=numpy.uint16)
a.shape = TEST_IMAGE_SIZE
Image.fromarray(a).save(tmpfile)
im_test = Image.open(tmpfile)
im_good = Image.open(filename)
self.assert_image_equal(im_good, im_test)
def test_to_array(self): def test_to_array(self):
def _to_array(mode, dtype): def _to_array(mode, dtype):