Pillow/Tests/test_color_lut.py

562 lines
22 KiB
Python
Raw Normal View History

import unittest
2018-04-12 11:54:54 +03:00
from array import array
2018-03-29 16:37:35 +03:00
from PIL import Image, ImageFilter
from .helper import PillowTestCase, assert_image_equal
2018-03-26 19:30:00 +03:00
2018-04-14 19:17:15 +03:00
try:
import numpy
except ImportError:
numpy = None
2018-03-26 19:30:00 +03:00
class TestColorLut3DCoreAPI(PillowTestCase):
2018-04-11 17:05:48 +03:00
def generate_identity_table(self, channels, size):
2018-03-26 19:30:00 +03:00
if isinstance(size, tuple):
size1D, size2D, size3D = size
else:
size1D, size2D, size3D = (size, size, size)
table = [
[
r / (size1D - 1) if size1D != 1 else 0,
g / (size2D - 1) if size2D != 1 else 0,
b / (size3D - 1) if size3D != 1 else 0,
r / (size1D - 1) if size1D != 1 else 0,
g / (size2D - 1) if size2D != 1 else 0,
2018-03-26 19:30:00 +03:00
][:channels]
for b in range(size3D)
for g in range(size2D)
for r in range(size1D)
2018-03-26 19:30:00 +03:00
]
return (
2019-06-13 18:53:42 +03:00
channels,
size1D,
size2D,
size3D,
[item for sublist in table for item in sublist],
)
2018-03-26 19:30:00 +03:00
2018-03-29 16:37:35 +03:00
def test_wrong_args(self):
2019-06-13 18:53:42 +03:00
im = Image.new("RGB", (10, 10), 0)
2018-03-26 19:30:00 +03:00
with self.assertRaisesRegex(ValueError, "filter"):
2019-06-13 18:53:42 +03:00
im.im.color_lut_3d("RGB", Image.CUBIC, *self.generate_identity_table(3, 3))
2018-03-26 19:30:00 +03:00
with self.assertRaisesRegex(ValueError, "image mode"):
2019-06-13 18:53:42 +03:00
im.im.color_lut_3d(
"wrong", Image.LINEAR, *self.generate_identity_table(3, 3)
)
2018-03-26 19:30:00 +03:00
with self.assertRaisesRegex(ValueError, "table_channels"):
2019-06-13 18:53:42 +03:00
im.im.color_lut_3d("RGB", Image.LINEAR, *self.generate_identity_table(5, 3))
2018-03-26 19:30:00 +03:00
with self.assertRaisesRegex(ValueError, "table_channels"):
2019-06-13 18:53:42 +03:00
im.im.color_lut_3d("RGB", Image.LINEAR, *self.generate_identity_table(1, 3))
2018-03-26 19:30:00 +03:00
with self.assertRaisesRegex(ValueError, "table_channels"):
2019-06-13 18:53:42 +03:00
im.im.color_lut_3d("RGB", Image.LINEAR, *self.generate_identity_table(2, 3))
2018-03-26 19:30:00 +03:00
with self.assertRaisesRegex(ValueError, "Table size"):
2019-06-13 18:53:42 +03:00
im.im.color_lut_3d(
"RGB", Image.LINEAR, *self.generate_identity_table(3, (1, 3, 3))
)
2018-03-26 19:30:00 +03:00
with self.assertRaisesRegex(ValueError, "Table size"):
2019-06-13 18:53:42 +03:00
im.im.color_lut_3d(
"RGB", Image.LINEAR, *self.generate_identity_table(3, (66, 3, 3))
)
2018-03-26 19:30:00 +03:00
with self.assertRaisesRegex(ValueError, r"size1D \* size2D \* size3D"):
2019-06-13 18:53:42 +03:00
im.im.color_lut_3d("RGB", Image.LINEAR, 3, 2, 2, 2, [0, 0, 0] * 7)
2018-03-26 22:29:50 +03:00
with self.assertRaisesRegex(ValueError, r"size1D \* size2D \* size3D"):
2019-06-13 18:53:42 +03:00
im.im.color_lut_3d("RGB", Image.LINEAR, 3, 2, 2, 2, [0, 0, 0] * 9)
2018-03-26 22:29:50 +03:00
2018-04-14 17:22:21 +03:00
with self.assertRaises(TypeError):
2019-06-13 18:53:42 +03:00
im.im.color_lut_3d("RGB", Image.LINEAR, 3, 2, 2, 2, [0, 0, "0"] * 8)
2018-04-14 17:47:53 +03:00
with self.assertRaises(TypeError):
2019-06-13 18:53:42 +03:00
im.im.color_lut_3d("RGB", Image.LINEAR, 3, 2, 2, 2, 16)
2018-04-14 17:47:53 +03:00
2018-03-29 16:37:35 +03:00
def test_correct_args(self):
2019-06-13 18:53:42 +03:00
im = Image.new("RGB", (10, 10), 0)
2018-03-26 19:30:00 +03:00
2019-06-13 18:53:42 +03:00
im.im.color_lut_3d("RGB", Image.LINEAR, *self.generate_identity_table(3, 3))
2018-03-26 19:30:00 +03:00
2019-06-13 18:53:42 +03:00
im.im.color_lut_3d("CMYK", Image.LINEAR, *self.generate_identity_table(4, 3))
2018-03-26 19:30:00 +03:00
2019-06-13 18:53:42 +03:00
im.im.color_lut_3d(
"RGB", Image.LINEAR, *self.generate_identity_table(3, (2, 3, 3))
)
2018-03-26 19:30:00 +03:00
2019-06-13 18:53:42 +03:00
im.im.color_lut_3d(
"RGB", Image.LINEAR, *self.generate_identity_table(3, (65, 3, 3))
)
2018-03-26 19:30:00 +03:00
2019-06-13 18:53:42 +03:00
im.im.color_lut_3d(
"RGB", Image.LINEAR, *self.generate_identity_table(3, (3, 65, 3))
)
2018-03-26 19:30:00 +03:00
2019-06-13 18:53:42 +03:00
im.im.color_lut_3d(
"RGB", Image.LINEAR, *self.generate_identity_table(3, (3, 3, 65))
)
2018-03-26 19:30:00 +03:00
def test_wrong_mode(self):
with self.assertRaisesRegex(ValueError, "wrong mode"):
2019-06-13 18:53:42 +03:00
im = Image.new("L", (10, 10), 0)
im.im.color_lut_3d("RGB", Image.LINEAR, *self.generate_identity_table(3, 3))
2018-03-26 19:30:00 +03:00
with self.assertRaisesRegex(ValueError, "wrong mode"):
2019-06-13 18:53:42 +03:00
im = Image.new("RGB", (10, 10), 0)
im.im.color_lut_3d("L", Image.LINEAR, *self.generate_identity_table(3, 3))
2018-03-26 19:30:00 +03:00
with self.assertRaisesRegex(ValueError, "wrong mode"):
2019-06-13 18:53:42 +03:00
im = Image.new("L", (10, 10), 0)
im.im.color_lut_3d("L", Image.LINEAR, *self.generate_identity_table(3, 3))
2018-03-26 19:30:00 +03:00
with self.assertRaisesRegex(ValueError, "wrong mode"):
2019-06-13 18:53:42 +03:00
im = Image.new("RGB", (10, 10), 0)
im.im.color_lut_3d(
"RGBA", Image.LINEAR, *self.generate_identity_table(3, 3)
)
2018-03-26 19:30:00 +03:00
with self.assertRaisesRegex(ValueError, "wrong mode"):
2019-06-13 18:53:42 +03:00
im = Image.new("RGB", (10, 10), 0)
im.im.color_lut_3d("RGB", Image.LINEAR, *self.generate_identity_table(4, 3))
2018-03-26 19:30:00 +03:00
def test_correct_mode(self):
2019-06-13 18:53:42 +03:00
im = Image.new("RGBA", (10, 10), 0)
im.im.color_lut_3d("RGBA", Image.LINEAR, *self.generate_identity_table(3, 3))
2018-03-26 19:30:00 +03:00
2019-06-13 18:53:42 +03:00
im = Image.new("RGBA", (10, 10), 0)
im.im.color_lut_3d("RGBA", Image.LINEAR, *self.generate_identity_table(4, 3))
2018-03-26 19:30:00 +03:00
2019-06-13 18:53:42 +03:00
im = Image.new("RGB", (10, 10), 0)
im.im.color_lut_3d("HSV", Image.LINEAR, *self.generate_identity_table(3, 3))
2018-03-26 19:30:00 +03:00
2019-06-13 18:53:42 +03:00
im = Image.new("RGB", (10, 10), 0)
im.im.color_lut_3d("RGBA", Image.LINEAR, *self.generate_identity_table(4, 3))
2018-03-26 19:30:00 +03:00
2018-04-11 17:05:48 +03:00
def test_identities(self):
2019-06-13 18:53:42 +03:00
g = Image.linear_gradient("L")
im = Image.merge(
"RGB", [g, g.transpose(Image.ROTATE_90), g.transpose(Image.ROTATE_180)]
)
2018-03-26 22:29:50 +03:00
# Fast test with small cubes
for size in [2, 3, 5, 7, 11, 16, 17]:
assert_image_equal(
2019-06-13 18:53:42 +03:00
im,
im._new(
im.im.color_lut_3d(
"RGB", Image.LINEAR, *self.generate_identity_table(3, size)
)
),
)
2018-03-26 22:29:50 +03:00
# Not so fast
assert_image_equal(
2019-06-13 18:53:42 +03:00
im,
im._new(
im.im.color_lut_3d(
"RGB", Image.LINEAR, *self.generate_identity_table(3, (2, 2, 65))
)
),
)
2018-03-26 22:29:50 +03:00
2018-04-11 17:05:48 +03:00
def test_identities_4_channels(self):
2019-06-13 18:53:42 +03:00
g = Image.linear_gradient("L")
im = Image.merge(
"RGB", [g, g.transpose(Image.ROTATE_90), g.transpose(Image.ROTATE_180)]
)
# Red channel copied to alpha
assert_image_equal(
2019-06-13 18:53:42 +03:00
Image.merge("RGBA", (im.split() * 2)[:4]),
im._new(
im.im.color_lut_3d(
"RGBA", Image.LINEAR, *self.generate_identity_table(4, 17)
)
),
)
def test_copy_alpha_channel(self):
2019-06-13 18:53:42 +03:00
g = Image.linear_gradient("L")
im = Image.merge(
"RGBA",
[
g,
g.transpose(Image.ROTATE_90),
g.transpose(Image.ROTATE_180),
g.transpose(Image.ROTATE_270),
],
)
assert_image_equal(
2019-06-13 18:53:42 +03:00
im,
im._new(
im.im.color_lut_3d(
"RGBA", Image.LINEAR, *self.generate_identity_table(3, 17)
)
),
)
2018-03-26 22:29:50 +03:00
def test_channels_order(self):
2019-06-13 18:53:42 +03:00
g = Image.linear_gradient("L")
im = Image.merge(
"RGB", [g, g.transpose(Image.ROTATE_90), g.transpose(Image.ROTATE_180)]
)
2018-03-26 22:29:50 +03:00
# Reverse channels by splitting and using table
2019-06-13 18:53:42 +03:00
# fmt: off
assert_image_equal(
2018-03-26 22:29:50 +03:00
Image.merge('RGB', im.split()[::-1]),
im._new(im.im.color_lut_3d('RGB', Image.LINEAR,
3, 2, 2, 2, [
0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 1, 1,
2018-03-26 22:29:50 +03:00
1, 0, 0, 1, 0, 1,
1, 1, 0, 1, 1, 1,
])))
2019-06-13 18:53:42 +03:00
# fmt: on
2018-03-26 22:29:50 +03:00
def test_overflow(self):
2019-06-13 18:53:42 +03:00
g = Image.linear_gradient("L")
im = Image.merge(
"RGB", [g, g.transpose(Image.ROTATE_90), g.transpose(Image.ROTATE_180)]
)
2018-03-26 22:29:50 +03:00
2019-06-13 18:53:42 +03:00
# fmt: off
2018-03-26 22:29:50 +03:00
transformed = im._new(im.im.color_lut_3d('RGB', Image.LINEAR,
3, 2, 2, 2,
[
-1, -1, -1, 2, -1, -1,
-1, 2, -1, 2, 2, -1,
-1, -1, 2, 2, -1, 2,
-1, 2, 2, 2, 2, 2,
])).load()
2019-06-13 18:53:42 +03:00
# fmt: on
2018-03-29 23:56:51 +03:00
self.assertEqual(transformed[0, 0], (0, 0, 255))
self.assertEqual(transformed[50, 50], (0, 0, 255))
self.assertEqual(transformed[255, 0], (0, 255, 255))
self.assertEqual(transformed[205, 50], (0, 255, 255))
self.assertEqual(transformed[0, 255], (255, 0, 0))
self.assertEqual(transformed[50, 205], (255, 0, 0))
self.assertEqual(transformed[255, 255], (255, 255, 0))
self.assertEqual(transformed[205, 205], (255, 255, 0))
2018-03-26 22:29:50 +03:00
2019-06-13 18:53:42 +03:00
# fmt: off
2018-03-29 23:56:51 +03:00
transformed = im._new(im.im.color_lut_3d('RGB', Image.LINEAR,
3, 2, 2, 2,
[
-3, -3, -3, 5, -3, -3,
-3, 5, -3, 5, 5, -3,
-3, -3, 5, 5, -3, 5,
-3, 5, 5, 5, 5, 5,
])).load()
2019-06-13 18:53:42 +03:00
# fmt: on
2018-03-26 22:29:50 +03:00
self.assertEqual(transformed[0, 0], (0, 0, 255))
self.assertEqual(transformed[50, 50], (0, 0, 255))
self.assertEqual(transformed[255, 0], (0, 255, 255))
self.assertEqual(transformed[205, 50], (0, 255, 255))
self.assertEqual(transformed[0, 255], (255, 0, 0))
self.assertEqual(transformed[50, 205], (255, 0, 0))
self.assertEqual(transformed[255, 255], (255, 255, 0))
self.assertEqual(transformed[205, 205], (255, 255, 0))
2018-03-26 19:30:00 +03:00
2018-03-29 16:37:35 +03:00
class TestColorLut3DFilter(PillowTestCase):
def test_wrong_args(self):
with self.assertRaisesRegex(ValueError, "should be either an integer"):
2018-03-29 16:37:35 +03:00
ImageFilter.Color3DLUT("small", [1])
with self.assertRaisesRegex(ValueError, "should be either an integer"):
2018-03-29 16:37:35 +03:00
ImageFilter.Color3DLUT((11, 11), [1])
with self.assertRaisesRegex(ValueError, r"in \[2, 65\] range"):
2018-03-29 16:37:35 +03:00
ImageFilter.Color3DLUT((11, 11, 1), [1])
with self.assertRaisesRegex(ValueError, r"in \[2, 65\] range"):
2018-03-29 16:37:35 +03:00
ImageFilter.Color3DLUT((11, 11, 66), [1])
with self.assertRaisesRegex(ValueError, "table should have .+ items"):
2018-03-29 16:37:35 +03:00
ImageFilter.Color3DLUT((3, 3, 3), [1, 1, 1])
with self.assertRaisesRegex(ValueError, "table should have .+ items"):
2018-03-29 16:37:35 +03:00
ImageFilter.Color3DLUT((3, 3, 3), [[1, 1, 1]] * 2)
with self.assertRaisesRegex(ValueError, "should have a length of 4"):
2018-03-29 16:37:35 +03:00
ImageFilter.Color3DLUT((3, 3, 3), [[1, 1, 1]] * 27, channels=4)
with self.assertRaisesRegex(ValueError, "should have a length of 3"):
2018-03-29 16:37:35 +03:00
ImageFilter.Color3DLUT((2, 2, 2), [[1, 1]] * 8)
with self.assertRaisesRegex(ValueError, "Only 3 or 4 output"):
ImageFilter.Color3DLUT((2, 2, 2), [[1, 1]] * 8, channels=2)
2018-03-29 16:37:35 +03:00
def test_convert_table(self):
2018-03-30 02:02:37 +03:00
lut = ImageFilter.Color3DLUT(2, [0, 1, 2] * 8)
self.assertEqual(tuple(lut.size), (2, 2, 2))
self.assertEqual(lut.name, "Color 3D LUT")
2018-03-29 16:37:35 +03:00
2019-06-13 18:53:42 +03:00
# fmt: off
2018-03-30 02:02:37 +03:00
lut = ImageFilter.Color3DLUT((2, 2, 2), [
2018-03-29 16:37:35 +03:00
(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11),
(12, 13, 14), (15, 16, 17), (18, 19, 20), (21, 22, 23)])
2019-06-13 18:53:42 +03:00
# fmt: on
2018-03-30 02:02:37 +03:00
self.assertEqual(tuple(lut.size), (2, 2, 2))
self.assertEqual(lut.table, list(range(24)))
2018-03-29 16:37:35 +03:00
2019-06-13 18:53:42 +03:00
lut = ImageFilter.Color3DLUT((2, 2, 2), [(0, 1, 2, 3)] * 8, channels=4)
self.assertEqual(tuple(lut.size), (2, 2, 2))
self.assertEqual(lut.table, list(range(4)) * 8)
2018-03-29 16:37:35 +03:00
2018-04-14 19:17:15 +03:00
@unittest.skipIf(numpy is None, "Numpy is not installed")
def test_numpy_sources(self):
table = numpy.ones((5, 6, 7, 3), dtype=numpy.float16)
2018-04-15 23:35:41 +03:00
with self.assertRaisesRegex(ValueError, "should have either channels"):
2018-04-14 19:17:15 +03:00
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
2019-06-13 18:53:42 +03:00
Image.new("RGB", (10, 10), 0).filter(lut)
2018-04-14 19:17:15 +03:00
# 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)
2018-04-15 00:33:15 +03:00
@unittest.skipIf(numpy is None, "Numpy is not installed")
def test_numpy_formats(self):
2019-06-13 18:53:42 +03:00
g = Image.linear_gradient("L")
im = Image.merge(
"RGB", [g, g.transpose(Image.ROTATE_90), g.transpose(Image.ROTATE_180)]
)
2018-04-15 00:33:15 +03:00
2019-06-13 18:53:42 +03:00
lut = ImageFilter.Color3DLUT.generate((7, 9, 11), lambda r, g, b: (r, g, b))
2018-04-15 00:33:15 +03:00
lut.table = numpy.array(lut.table, dtype=numpy.float32)[:-1]
2018-04-15 23:35:41 +03:00
with self.assertRaisesRegex(ValueError, "should have table_channels"):
2018-04-15 00:33:15 +03:00
im.filter(lut)
2019-06-13 18:53:42 +03:00
lut = ImageFilter.Color3DLUT.generate((7, 9, 11), lambda r, g, b: (r, g, b))
lut.table = numpy.array(lut.table, dtype=numpy.float32).reshape((7 * 9 * 11), 3)
2018-04-15 23:35:41 +03:00
with self.assertRaisesRegex(ValueError, "should have table_channels"):
2018-04-15 00:33:15 +03:00
im.filter(lut)
2019-06-13 18:53:42 +03:00
lut = ImageFilter.Color3DLUT.generate((7, 9, 11), lambda r, g, b: (r, g, b))
2018-04-15 00:33:15 +03:00
lut.table = numpy.array(lut.table, dtype=numpy.float16)
assert_image_equal(im, im.filter(lut))
2018-04-15 00:33:15 +03:00
2019-06-13 18:53:42 +03:00
lut = ImageFilter.Color3DLUT.generate((7, 9, 11), lambda r, g, b: (r, g, b))
2018-04-15 00:33:15 +03:00
lut.table = numpy.array(lut.table, dtype=numpy.float32)
assert_image_equal(im, im.filter(lut))
2018-04-15 00:33:15 +03:00
2019-06-13 18:53:42 +03:00
lut = ImageFilter.Color3DLUT.generate((7, 9, 11), lambda r, g, b: (r, g, b))
2018-04-15 00:33:15 +03:00
lut.table = numpy.array(lut.table, dtype=numpy.float64)
assert_image_equal(im, im.filter(lut))
2018-04-15 00:33:15 +03:00
2019-06-13 18:53:42 +03:00
lut = ImageFilter.Color3DLUT.generate((7, 9, 11), lambda r, g, b: (r, g, b))
2018-04-15 00:33:15 +03:00
lut.table = numpy.array(lut.table, dtype=numpy.int32)
im.filter(lut)
lut.table = numpy.array(lut.table, dtype=numpy.int8)
im.filter(lut)
2018-04-12 11:54:54 +03:00
def test_repr(self):
lut = ImageFilter.Color3DLUT(2, [0, 1, 2] * 8)
2019-06-13 18:53:42 +03:00
self.assertEqual(repr(lut), "<Color3DLUT from list size=2x2x2 channels=3>")
2018-04-12 11:54:54 +03:00
lut = ImageFilter.Color3DLUT(
2019-06-13 18:53:42 +03:00
(3, 4, 5),
array("f", [0, 0, 0, 0] * (3 * 4 * 5)),
channels=4,
target_mode="YCbCr",
_copy_table=False,
)
self.assertEqual(
2019-06-13 18:53:42 +03:00
repr(lut), "<Color3DLUT from array size=3x4x5 channels=4 target_mode=YCbCr>"
)
2018-04-11 17:05:48 +03:00
2018-04-11 17:05:48 +03:00
class TestGenerateColorLut3D(PillowTestCase):
def test_wrong_channels_count(self):
with self.assertRaisesRegex(ValueError, "3 or 4 output channels"):
ImageFilter.Color3DLUT.generate(
2019-06-13 18:53:42 +03:00
5, channels=2, callback=lambda r, g, b: (r, g, b)
)
2018-03-29 16:37:35 +03:00
with self.assertRaisesRegex(ValueError, "should have either channels"):
2018-04-11 17:05:48 +03:00
ImageFilter.Color3DLUT.generate(5, lambda r, g, b: (r, g, b, r))
with self.assertRaisesRegex(ValueError, "should have either channels"):
ImageFilter.Color3DLUT.generate(
2019-06-13 18:53:42 +03:00
5, channels=4, callback=lambda r, g, b: (r, g, b)
)
2018-04-11 17:05:48 +03:00
def test_3_channels(self):
2018-03-30 02:02:37 +03:00
lut = ImageFilter.Color3DLUT.generate(5, lambda r, g, b: (r, g, b))
self.assertEqual(tuple(lut.size), (5, 5, 5))
self.assertEqual(lut.name, "Color 3D LUT")
2019-06-13 18:53:42 +03:00
# fmt: off
2018-03-30 02:02:37 +03:00
self.assertEqual(lut.table[:24], [
2018-03-29 17:26:21 +03:00
0.0, 0.0, 0.0, 0.25, 0.0, 0.0, 0.5, 0.0, 0.0, 0.75, 0.0, 0.0,
1.0, 0.0, 0.0, 0.0, 0.25, 0.0, 0.25, 0.25, 0.0, 0.5, 0.25, 0.0])
2019-06-13 18:53:42 +03:00
# fmt: on
2018-03-29 17:26:21 +03:00
2018-04-11 17:05:48 +03:00
def test_4_channels(self):
lut = ImageFilter.Color3DLUT.generate(
2019-06-13 18:53:42 +03:00
5, channels=4, callback=lambda r, g, b: (b, r, g, (r + g + b) / 2)
)
2018-03-30 02:02:37 +03:00
self.assertEqual(tuple(lut.size), (5, 5, 5))
self.assertEqual(lut.name, "Color 3D LUT")
2019-06-13 18:53:42 +03:00
# fmt: off
2018-03-30 02:02:37 +03:00
self.assertEqual(lut.table[:24], [
2018-03-29 17:26:21 +03:00
0.0, 0.0, 0.0, 0.0, 0.0, 0.25, 0.0, 0.125, 0.0, 0.5, 0.0, 0.25,
2018-06-24 15:32:25 +03:00
0.0, 0.75, 0.0, 0.375, 0.0, 1.0, 0.0, 0.5, 0.0, 0.0, 0.25, 0.125
])
2019-06-13 18:53:42 +03:00
# fmt: on
2018-03-29 17:26:21 +03:00
2018-04-11 17:05:48 +03:00
def test_apply(self):
lut = ImageFilter.Color3DLUT.generate(5, lambda r, g, b: (r, g, b))
2018-03-29 17:26:21 +03:00
2019-06-13 18:53:42 +03:00
g = Image.linear_gradient("L")
im = Image.merge(
"RGB", [g, g.transpose(Image.ROTATE_90), g.transpose(Image.ROTATE_180)]
)
2018-04-11 17:05:48 +03:00
self.assertEqual(im, im.filter(lut))
class TestTransformColorLut3D(PillowTestCase):
def test_wrong_args(self):
source = ImageFilter.Color3DLUT.generate(5, lambda r, g, b: (r, g, b))
with self.assertRaisesRegex(ValueError, "Only 3 or 4 output"):
2018-04-11 17:05:48 +03:00
source.transform(lambda r, g, b: (r, g, b), channels=8)
with self.assertRaisesRegex(ValueError, "should have either channels"):
2018-04-11 17:05:48 +03:00
source.transform(lambda r, g, b: (r, g, b), channels=4)
2018-03-29 17:26:21 +03:00
with self.assertRaisesRegex(ValueError, "should have either channels"):
2018-04-11 17:05:48 +03:00
source.transform(lambda r, g, b: (r, g, b, 1))
2018-04-11 17:31:41 +03:00
with self.assertRaises(TypeError):
2018-04-11 17:05:48 +03:00
source.transform(lambda r, g, b, a: (r, g, b))
def test_target_mode(self):
source = ImageFilter.Color3DLUT.generate(
2019-06-13 18:53:42 +03:00
2, lambda r, g, b: (r, g, b), target_mode="HSV"
)
2018-04-11 17:05:48 +03:00
lut = source.transform(lambda r, g, b: (r, g, b))
2019-06-13 18:53:42 +03:00
self.assertEqual(lut.mode, "HSV")
2018-04-11 17:05:48 +03:00
2019-06-13 18:53:42 +03:00
lut = source.transform(lambda r, g, b: (r, g, b), target_mode="RGB")
self.assertEqual(lut.mode, "RGB")
2018-04-11 17:05:48 +03:00
def test_3_to_3_channels(self):
2019-06-13 18:53:42 +03:00
source = ImageFilter.Color3DLUT.generate((3, 4, 5), lambda r, g, b: (r, g, b))
lut = source.transform(lambda r, g, b: (r * r, g * g, b * b))
2018-04-11 17:05:48 +03:00
self.assertEqual(tuple(lut.size), tuple(source.size))
self.assertEqual(len(lut.table), len(source.table))
self.assertNotEqual(lut.table, source.table)
2019-06-13 18:53:42 +03:00
self.assertEqual(
lut.table[0:10], [0.0, 0.0, 0.0, 0.25, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0]
)
2018-04-11 17:05:48 +03:00
def test_3_to_4_channels(self):
2019-06-13 18:53:42 +03:00
source = ImageFilter.Color3DLUT.generate((6, 5, 4), lambda r, g, b: (r, g, b))
lut = source.transform(lambda r, g, b: (r * r, g * g, b * b, 1), channels=4)
2018-04-11 17:05:48 +03:00
self.assertEqual(tuple(lut.size), tuple(source.size))
self.assertNotEqual(len(lut.table), len(source.table))
self.assertNotEqual(lut.table, source.table)
2019-06-13 18:53:42 +03:00
# fmt: off
2018-04-11 17:05:48 +03:00
self.assertEqual(lut.table[0:16], [
0.0, 0.0, 0.0, 1, 0.2**2, 0.0, 0.0, 1,
0.4**2, 0.0, 0.0, 1, 0.6**2, 0.0, 0.0, 1])
2019-06-13 18:53:42 +03:00
# fmt: on
2018-04-11 17:05:48 +03:00
def test_4_to_3_channels(self):
source = ImageFilter.Color3DLUT.generate(
2019-06-13 18:53:42 +03:00
(3, 6, 5), lambda r, g, b: (r, g, b, 1), channels=4
)
lut = source.transform(
lambda r, g, b, a: (a - r * r, a - g * g, a - b * b), channels=3
)
2018-04-11 17:05:48 +03:00
self.assertEqual(tuple(lut.size), tuple(source.size))
self.assertNotEqual(len(lut.table), len(source.table))
self.assertNotEqual(lut.table, source.table)
2019-06-13 18:53:42 +03:00
# fmt: off
2018-04-11 17:05:48 +03:00
self.assertEqual(lut.table[0:18], [
1.0, 1.0, 1.0, 0.75, 1.0, 1.0, 0.0, 1.0, 1.0,
1.0, 0.96, 1.0, 0.75, 0.96, 1.0, 0.0, 0.96, 1.0])
2019-06-13 18:53:42 +03:00
# fmt: on
2018-04-11 17:05:48 +03:00
def test_4_to_4_channels(self):
source = ImageFilter.Color3DLUT.generate(
2019-06-13 18:53:42 +03:00
(6, 5, 4), lambda r, g, b: (r, g, b, 1), channels=4
)
lut = source.transform(lambda r, g, b, a: (r * r, g * g, b * b, a - 0.5))
2018-04-11 17:05:48 +03:00
self.assertEqual(tuple(lut.size), tuple(source.size))
self.assertEqual(len(lut.table), len(source.table))
self.assertNotEqual(lut.table, source.table)
2019-06-13 18:53:42 +03:00
# fmt: off
2018-04-11 17:05:48 +03:00
self.assertEqual(lut.table[0:16], [
0.0, 0.0, 0.0, 0.5, 0.2**2, 0.0, 0.0, 0.5,
0.4**2, 0.0, 0.0, 0.5, 0.6**2, 0.0, 0.0, 0.5])
2019-06-13 18:53:42 +03:00
# fmt: on
2018-04-11 17:05:48 +03:00
def test_with_normals_3_channels(self):
source = ImageFilter.Color3DLUT.generate(
2019-06-13 18:53:42 +03:00
(6, 5, 4), lambda r, g, b: (r * r, g * g, b * b)
)
2018-04-11 17:05:48 +03:00
lut = source.transform(
2019-06-13 18:53:42 +03:00
lambda nr, ng, nb, r, g, b: (nr - r, ng - g, nb - b), with_normals=True
)
2018-04-11 17:05:48 +03:00
self.assertEqual(tuple(lut.size), tuple(source.size))
self.assertEqual(len(lut.table), len(source.table))
self.assertNotEqual(lut.table, source.table)
2019-06-13 18:53:42 +03:00
# fmt: off
2018-04-11 17:05:48 +03:00
self.assertEqual(lut.table[0:18], [
0.0, 0.0, 0.0, 0.16, 0.0, 0.0, 0.24, 0.0, 0.0,
0.24, 0.0, 0.0, 0.8 - (0.8**2), 0, 0, 0, 0, 0])
2019-06-13 18:53:42 +03:00
# fmt: on
2018-04-11 17:05:48 +03:00
def test_with_normals_4_channels(self):
source = ImageFilter.Color3DLUT.generate(
2019-06-13 18:53:42 +03:00
(3, 6, 5), lambda r, g, b: (r * r, g * g, b * b, 1), channels=4
)
2018-04-11 17:05:48 +03:00
lut = source.transform(
2019-06-13 18:53:42 +03:00
lambda nr, ng, nb, r, g, b, a: (nr - r, ng - g, nb - b, a - 0.5),
with_normals=True,
)
2018-04-11 17:05:48 +03:00
self.assertEqual(tuple(lut.size), tuple(source.size))
self.assertEqual(len(lut.table), len(source.table))
self.assertNotEqual(lut.table, source.table)
2019-06-13 18:53:42 +03:00
# fmt: off
2018-04-11 17:05:48 +03:00
self.assertEqual(lut.table[0:16], [
0.0, 0.0, 0.0, 0.5, 0.25, 0.0, 0.0, 0.5,
0.0, 0.0, 0.0, 0.5, 0.0, 0.16, 0.0, 0.5])
2019-06-13 18:53:42 +03:00
# fmt: on