2015-05-23 13:28:41 +03:00
|
|
|
from __future__ import print_function
|
2016-08-18 00:25:52 +03:00
|
|
|
import sys
|
2014-09-05 14:03:56 +04:00
|
|
|
from helper import unittest, PillowTestCase, hopper
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
from PIL import Image
|
|
|
|
|
|
|
|
try:
|
|
|
|
import site
|
|
|
|
import numpy
|
2016-08-04 09:40:12 +03:00
|
|
|
assert site # silence warning
|
|
|
|
assert numpy # silence warning
|
2012-10-16 00:26:38 +04:00
|
|
|
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
|
|
|
|
2016-08-25 13:22:20 +03:00
|
|
|
# Numpy on pypy as of pypy 5.3.1 is corrupting the numpy.array(Image)
|
|
|
|
# call such that it's returning a object of type numpy.ndarray, but
|
|
|
|
# the repr is that of a PIL.Image. Size and shape are 1 and (), not the
|
|
|
|
# size and shape of the array. This causes failures in several tests.
|
|
|
|
SKIP_NUMPY_ON_PYPY = hasattr(sys, 'pypy_version_info') and (
|
2016-09-03 05:17:22 +03:00
|
|
|
sys.pypy_version_info <= (5, 3, 1, 'final', 0))
|
|
|
|
|
2016-08-25 13:22:20 +03:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
class TestNumpy(PillowTestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
try:
|
|
|
|
import site
|
|
|
|
import numpy
|
2016-08-04 09:40:12 +03:00
|
|
|
assert site # silence warning
|
|
|
|
assert numpy # silence warning
|
2014-06-10 13:10:47 +04:00
|
|
|
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:
|
2017-08-16 21:46:27 +03:00
|
|
|
data = [0, 255] * 50
|
2014-06-10 13:10:47 +04:00
|
|
|
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)
|
2012-10-16 00:26:38 +04:00
|
|
|
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)
|
2017-08-09 02:36:07 +03:00
|
|
|
if list(i.getchannel(0).getdata()) != list(range(100)):
|
2014-06-10 13:10:47 +04:00
|
|
|
print("data mismatch for", dtype)
|
2016-11-19 03:19:43 +03:00
|
|
|
# print(dtype, list(i.getdata()))
|
2014-06-10 13:10:47 +04:00
|
|
|
return i
|
|
|
|
|
2016-04-11 21:13:17 +03:00
|
|
|
# Check supported 1-bit integer formats
|
2017-08-16 21:46:27 +03:00
|
|
|
self.assert_image(to_image(numpy.bool, 1, 1), '1', TEST_IMAGE_SIZE)
|
|
|
|
self.assert_image(to_image(numpy.bool8, 1, 1), '1', TEST_IMAGE_SIZE)
|
2017-09-01 14:05:40 +03:00
|
|
|
|
2016-04-11 21:13:17 +03: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)
|
2016-04-11 21:13:17 +03:00
|
|
|
|
|
|
|
# Check non-fixed-size integer types
|
2016-06-25 16:33:26 +03:00
|
|
|
# These may fail, depending on the platform, since we have no native
|
|
|
|
# 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)
|
2016-04-11 21:13:17 +03:00
|
|
|
|
|
|
|
# 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)
|
2012-10-16 00:26:38 +04:00
|
|
|
else:
|
2016-04-19 02:58:21 +03:00
|
|
|
self.assert_image(to_image(numpy.uint16), "I;16B", TEST_IMAGE_SIZE)
|
2016-06-25 16:33:26 +03:00
|
|
|
|
|
|
|
self.assert_image(to_image(numpy.int16), "I", TEST_IMAGE_SIZE)
|
2016-04-11 21:13:17 +03:00
|
|
|
|
|
|
|
# Check 32-bit integer formats
|
2016-06-25 16:33:26 +03:00
|
|
|
self.assert_image(to_image(numpy.uint32), "I", TEST_IMAGE_SIZE)
|
|
|
|
self.assert_image(to_image(numpy.int32), "I", TEST_IMAGE_SIZE)
|
2016-04-11 21:13:17 +03:00
|
|
|
|
|
|
|
# Check 64-bit integer formats
|
2017-09-01 14:05:40 +03:00
|
|
|
self.assertRaises(TypeError, to_image, numpy.uint64)
|
|
|
|
self.assertRaises(TypeError, to_image, numpy.int64)
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2016-04-11 21:13:17 +03:00
|
|
|
# Check floating-point formats
|
2016-04-19 02:58:21 +03:00
|
|
|
self.assert_image(to_image(numpy.float), "F", TEST_IMAGE_SIZE)
|
2017-09-01 14:05:40 +03:00
|
|
|
self.assertRaises(TypeError, 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
|
|
|
|
2016-04-29 23:01:35 +03: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
|
2017-02-14 12:27:02 +03:00
|
|
|
# https://stackoverflow.com/questions/10854903/what-is-causing-dimension-dependent-attributeerror-in-pil-fromarray-function
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_3d_array(self):
|
2016-04-19 02:58:21 +03:00
|
|
|
size = (5, TEST_IMAGE_SIZE[0], TEST_IMAGE_SIZE[1])
|
2016-04-11 21:13:53 +03:00
|
|
|
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])
|
2016-04-11 21:13:53 +03:00
|
|
|
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)
|
2016-04-11 21:13:53 +03:00
|
|
|
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):
|
2016-08-18 00:25:52 +03:00
|
|
|
self.assertGreaterEqual(len(np.shape), 2)
|
2015-10-05 10:27:23 +03:00
|
|
|
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])
|
|
|
|
|
2016-08-25 13:22:20 +03:00
|
|
|
@unittest.skipIf(SKIP_NUMPY_ON_PYPY, "numpy.array(Image) is flaky on PyPy")
|
2014-06-10 13:10:47 +04:00
|
|
|
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'))
|
2014-06-03 14:02:44 +04:00
|
|
|
|
2016-08-08 01:25:43 +03:00
|
|
|
def test_1bit(self):
|
|
|
|
# Test that 1-bit arrays convert to numpy and back
|
|
|
|
# See: https://github.com/python-pillow/Pillow/issues/350
|
|
|
|
arr = numpy.array([[1, 0, 0, 1, 0], [0, 1, 0, 0, 0]], 'u1')
|
|
|
|
img = Image.fromarray(arr * 255).convert('1')
|
|
|
|
self.assertEqual(img.mode, '1')
|
|
|
|
arr_back = numpy.array(img)
|
2017-01-20 18:50:51 +03:00
|
|
|
# numpy 1.8 and earlier return this as a boolean. (trusty/precise)
|
|
|
|
if arr_back.dtype == numpy.bool:
|
|
|
|
arr_bool = numpy.array([[1, 0, 0, 1, 0], [0, 1, 0, 0, 0]], 'bool')
|
|
|
|
numpy.testing.assert_array_equal(arr_bool, arr_back)
|
|
|
|
else:
|
|
|
|
numpy.testing.assert_array_equal(arr, arr_back)
|
2016-08-08 01:25:43 +03:00
|
|
|
|
2016-04-11 22:35:16 +03:00
|
|
|
def test_save_tiff_uint16(self):
|
2016-09-03 05:17:22 +03:00
|
|
|
# Tests that we're getting the pixel value in the right byte order.
|
2016-04-11 22:35:16 +03:00
|
|
|
pixel_value = 0x1234
|
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
|
2016-08-25 22:49:40 +03:00
|
|
|
img = Image.fromarray(a)
|
|
|
|
|
|
|
|
img_px = img.load()
|
2016-09-03 05:17:22 +03:00
|
|
|
self.assertEqual(img_px[0, 0], pixel_value)
|
2016-04-11 22:35:16 +03:00
|
|
|
|
2016-08-25 13:22:20 +03:00
|
|
|
@unittest.skipIf(SKIP_NUMPY_ON_PYPY, "numpy.array(Image) is flaky on PyPy")
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_to_array(self):
|
2014-06-03 14:02:44 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def _to_array(mode, dtype):
|
2014-09-05 14:03:56 +04:00
|
|
|
img = hopper(mode)
|
2015-10-05 10:27:23 +03:00
|
|
|
|
|
|
|
# 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-03 14:02:44 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
modes = [("L", 'uint8'),
|
|
|
|
("I", 'int32'),
|
|
|
|
("F", 'float32'),
|
2016-04-29 23:01:35 +03:00
|
|
|
("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'),
|
2015-12-07 20:40:42 +03:00
|
|
|
("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
|
2013-12-11 03:47:26 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
data = list(range(256))*3
|
|
|
|
lut = numpy.array(data, dtype='uint8')
|
2014-06-03 14:02:44 +04:00
|
|
|
|
2014-09-05 14:03:56 +04:00
|
|
|
im = hopper()
|
2013-12-11 03:47:26 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
im.point(lut)
|
2013-12-11 03:47:26 +04:00
|
|
|
|
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))
|
|
|
|
|
2017-02-22 09:16:17 +03:00
|
|
|
def test_zero_size(self):
|
|
|
|
# Shouldn't cause floating point exception
|
|
|
|
# See https://github.com/python-pillow/Pillow/issues/2259
|
|
|
|
|
|
|
|
im = Image.fromarray(numpy.empty((0, 0), dtype=numpy.uint8))
|
|
|
|
|
|
|
|
self.assertEqual(im.size, (0, 0))
|
|
|
|
|
2017-08-16 21:46:27 +03:00
|
|
|
def test_bool(self):
|
|
|
|
# https://github.com/python-pillow/Pillow/issues/2044
|
2018-03-06 11:53:07 +03:00
|
|
|
a = numpy.zeros((10, 2), dtype=numpy.bool)
|
2017-09-01 14:05:40 +03:00
|
|
|
a[0][0] = True
|
2017-08-16 21:46:27 +03:00
|
|
|
|
|
|
|
im2 = Image.fromarray(a)
|
|
|
|
self.assertEqual(im2.getdata()[0], 255)
|
|
|
|
|
2015-01-01 13:57:43 +03:00
|
|
|
def test_no_resource_warning_for_numpy_array(self):
|
|
|
|
# https://github.com/python-pillow/Pillow/issues/835
|
|
|
|
# Arrange
|
|
|
|
from numpy import array
|
|
|
|
test_file = 'Tests/images/hopper.png'
|
|
|
|
im = Image.open(test_file)
|
|
|
|
|
|
|
|
# Act/Assert
|
|
|
|
self.assert_warning(None, lambda: array(im))
|
|
|
|
|
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|