Pillow/Tests/test_numpy.py

193 lines
6.9 KiB
Python
Raw Normal View History

from __future__ import print_function
from helper import unittest, PillowTestCase, hopper
from PIL import Image
try:
import site
import numpy
except ImportError:
2014-06-10 13:10:47 +04:00
# Skip via setUp()
pass
2016-04-19 02:58:21 +03:00
TEST_IMAGE_SIZE = (10, 10)
2014-06-10 13:10:47 +04:00
class TestNumpy(PillowTestCase):
def setUp(self):
try:
import site
import numpy
except ImportError:
self.skipTest("ImportError")
def test_numpy_to_image(self):
2015-12-10 01:23:58 +03:00
def to_image(dtype, bands=1, boolean=0):
2014-06-10 13:10:47 +04:00
if bands == 1:
2015-12-10 01:23:58 +03:00
if boolean:
2014-06-10 13:10:47 +04:00
data = [0, 1] * 50
else:
data = list(range(100))
a = numpy.array(data, dtype=dtype)
2016-04-19 02:58:21 +03:00
a.shape = TEST_IMAGE_SIZE
2014-06-10 13:10:47 +04:00
i = Image.fromarray(a)
if list(i.getdata()) != data:
print("data mismatch for", dtype)
else:
data = list(range(100))
2014-06-10 13:10:47 +04:00
a = numpy.array([[x]*bands for x in data], dtype=dtype)
2016-04-19 02:58:21 +03:00
a.shape = TEST_IMAGE_SIZE[0], TEST_IMAGE_SIZE[1], bands
2014-06-10 13:10:47 +04:00
i = Image.fromarray(a)
if list(i.split()[0].getdata()) != list(range(100)):
print("data mismatch for", dtype)
# print dtype, list(i.getdata())
return i
# Check supported 1-bit integer formats
self.assertRaises(TypeError, lambda: to_image(numpy.bool))
self.assertRaises(TypeError, lambda: to_image(numpy.bool8))
2014-06-10 13:10:47 +04:00
# Check supported 8-bit integer formats
2016-04-19 02:58:21 +03:00
self.assert_image(to_image(numpy.uint8), "L", TEST_IMAGE_SIZE)
self.assert_image(to_image(numpy.uint8, 3), "RGB", TEST_IMAGE_SIZE)
self.assert_image(to_image(numpy.uint8, 4), "RGBA", TEST_IMAGE_SIZE)
self.assert_image(to_image(numpy.int8), "I", TEST_IMAGE_SIZE)
# Check non-fixed-size integer types
2016-04-19 02:58:21 +03:00
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 == '<':
2016-04-19 02:58:21 +03:00
self.assert_image(to_image(numpy.uint16), "I;16", TEST_IMAGE_SIZE)
else:
2016-04-19 02:58:21 +03:00
self.assert_image(to_image(numpy.uint16), "I;16B", TEST_IMAGE_SIZE)
if Image._ENDIAN == '<':
2016-04-19 02:58:21 +03:00
self.assert_image(to_image(numpy.int16), "I;16S", TEST_IMAGE_SIZE)
else:
2016-04-19 02:58:21 +03:00
self.assert_image(to_image(numpy.int16), "I;16BS", TEST_IMAGE_SIZE)
# Check 32-bit integer formats
if Image._ENDIAN == '<':
2016-04-19 02:58:21 +03:00
self.assert_image(to_image(numpy.uint32), "I;32", TEST_IMAGE_SIZE)
else:
2016-04-19 02:58:21 +03:00
self.assert_image(to_image(numpy.uint32), "I;32B", TEST_IMAGE_SIZE)
if Image._ENDIAN == '<':
2016-04-19 02:58:21 +03:00
self.assert_image(to_image(numpy.int32), "I;32S", TEST_IMAGE_SIZE)
else:
2016-04-19 02:58:21 +03:00
self.assert_image(to_image(numpy.int32), "I;32BS", TEST_IMAGE_SIZE)
# Check 64-bit integer formats
self.assertRaises(TypeError, lambda: to_image(numpy.uint64))
2014-06-10 13:10:47 +04:00
self.assertRaises(TypeError, lambda: to_image(numpy.int64))
# Check floating-point formats
2016-04-19 02:58:21 +03:00
self.assert_image(to_image(numpy.float), "F", TEST_IMAGE_SIZE)
self.assertRaises(TypeError, lambda: to_image(numpy.float16))
2016-04-19 02:58:21 +03:00
self.assert_image(to_image(numpy.float32), "F", TEST_IMAGE_SIZE)
self.assert_image(to_image(numpy.float64), "F", TEST_IMAGE_SIZE)
2014-06-10 13:10:47 +04:00
self.assert_image(to_image(numpy.uint8, 2), "LA", (10, 10))
2014-06-10 13:10:47 +04:00
self.assert_image(to_image(numpy.uint8, 3), "RGB", (10, 10))
self.assert_image(to_image(numpy.uint8, 4), "RGBA", (10, 10))
2015-11-11 15:25:55 +03:00
# based on an erring example at
2014-06-10 13:10:47 +04:00
# http://stackoverflow.com/questions/10854903/what-is-causing-dimension-dependent-attributeerror-in-pil-fromarray-function
def test_3d_array(self):
2016-04-19 02:58:21 +03:00
size = (5, TEST_IMAGE_SIZE[0], TEST_IMAGE_SIZE[1])
a = numpy.ones(size, dtype=numpy.uint8)
2016-04-19 02:58:21 +03:00
self.assert_image(Image.fromarray(a[1, :, :]), "L", TEST_IMAGE_SIZE)
size = (TEST_IMAGE_SIZE[0], 5, TEST_IMAGE_SIZE[1])
a = numpy.ones(size, dtype=numpy.uint8)
2016-04-19 02:58:21 +03:00
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)
2016-04-19 02:58:21 +03:00
self.assert_image(Image.fromarray(a[:, :, 1]), "L", TEST_IMAGE_SIZE)
2014-06-10 13:10:47 +04:00
def _test_img_equals_nparray(self, img, np):
np_size = np.shape[1], np.shape[0]
self.assertEqual(img.size, np_size)
2014-06-10 13:10:47 +04:00
px = img.load()
for x in range(0, img.size[0], int(img.size[0]/10)):
for y in range(0, img.size[1], int(img.size[1]/10)):
self.assert_deep_equal(px[x, y], np[y, x])
def test_16bit(self):
img = Image.open('Tests/images/16bit.cropped.tif')
2013-10-01 01:10:58 +04:00
np_img = numpy.array(img)
2014-06-10 13:10:47 +04:00
self._test_img_equals_nparray(img, np_img)
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"
2016-04-19 02:58:21 +03:00
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)
2014-06-10 13:10:47 +04:00
def test_to_array(self):
2014-06-10 13:10:47 +04:00
def _to_array(mode, dtype):
img = hopper(mode)
# Resize to non-square
img = img.crop((3, 0, 124, 127))
self.assertEqual(img.size, (121, 127))
2014-06-10 13:10:47 +04:00
np_img = numpy.array(img)
self._test_img_equals_nparray(img, np_img)
self.assertEqual(np_img.dtype, numpy.dtype(dtype))
2014-06-10 13:10:47 +04:00
modes = [("L", 'uint8'),
("I", 'int32'),
("F", 'float32'),
("LA", 'uint8'),
2014-06-10 13:10:47 +04:00
("RGB", 'uint8'),
("RGBA", 'uint8'),
("RGBX", 'uint8'),
("CMYK", 'uint8'),
("YCbCr", 'uint8'),
("I;16", '<u2'),
("I;16B", '>u2'),
("I;16L", '<u2'),
("HSV", 'uint8'),
2014-06-10 13:10:47 +04:00
]
2013-10-01 01:10:58 +04:00
2014-06-10 13:10:47 +04:00
for mode in modes:
_to_array(*mode)
2013-10-01 01:10:58 +04:00
2014-06-10 13:10:47 +04:00
def test_point_lut(self):
# see https://github.com/python-pillow/Pillow/issues/439
2014-06-10 13:10:47 +04:00
data = list(range(256))*3
lut = numpy.array(data, dtype='uint8')
im = hopper()
2014-06-10 13:10:47 +04:00
im.point(lut)
2014-11-14 01:56:31 +03:00
def test_putdata(self):
# shouldn't segfault
# see https://github.com/python-pillow/Pillow/issues/1008
im = Image.new('F', (150, 100))
arr = numpy.zeros((15000,), numpy.float32)
im.putdata(arr)
2015-04-24 02:26:52 +03:00
self.assertEqual(len(im.getdata()), len(arr))
2014-06-10 13:10:47 +04:00
if __name__ == '__main__':
unittest.main()
2014-06-10 13:10:47 +04:00
# End of file