color lut numpy tests

This commit is contained in:
Alexander 2018-04-14 19:17:15 +03:00
parent 76e57bbbe2
commit 63b243e1f6

View File

@ -5,6 +5,11 @@ from array import array
from PIL import Image, ImageFilter
from helper import unittest, PillowTestCase
try:
import numpy
except ImportError:
numpy = None
class TestColorLut3DCoreAPI(PillowTestCase):
def generate_identity_table(self, channels, size):
@ -279,6 +284,39 @@ class TestColorLut3DFilter(PillowTestCase):
lut = ImageFilter.Color3DLUT((2, 2, 2), [(0, 1, 2, 3)] * 8,
channels=4)
@unittest.skipIf(numpy is None, "Numpy is not installed")
def test_numpy_sources(self):
table = numpy.ones((5, 6, 7, 3), dtype=numpy.float16)
with self.assertRaisesRegexp(ValueError, "should have either channels"):
lut = ImageFilter.Color3DLUT((5, 6, 7), table)
table = numpy.ones((7, 6, 5, 3), dtype=numpy.float16)
lut = ImageFilter.Color3DLUT((5, 6, 7), table)
self.assertIsInstance(lut.table, numpy.ndarray)
self.assertEqual(lut.table.dtype, table.dtype)
self.assertEqual(lut.table.shape, (table.size,))
table = numpy.ones((7 * 6 * 5, 3), dtype=numpy.float16)
lut = ImageFilter.Color3DLUT((5, 6, 7), table)
self.assertEqual(lut.table.shape, (table.size,))
table = numpy.ones((7 * 6 * 5 * 3), dtype=numpy.float16)
lut = ImageFilter.Color3DLUT((5, 6, 7), table)
self.assertEqual(lut.table.shape, (table.size,))
# Check application
Image.new('RGB', (10, 10), 0).filter(lut)
# Check copy
table[0] = 33
self.assertEqual(lut.table[0], 1)
# Check not copy
table = numpy.ones((7 * 6 * 5 * 3), dtype=numpy.float16)
lut = ImageFilter.Color3DLUT((5, 6, 7), table, _copy_table=False)
table[0] = 33
self.assertEqual(lut.table[0], 33)
def test_repr(self):
lut = ImageFilter.Color3DLUT(2, [0, 1, 2] * 8)
self.assertEqual(repr(lut),