Tests for python API

This commit is contained in:
Alexander 2018-03-29 16:37:35 +03:00
parent 461a090405
commit 506995d816
2 changed files with 62 additions and 11 deletions

View File

@ -1,7 +1,7 @@
from __future__ import division
from helper import unittest, PillowTestCase
from PIL import Image
from PIL import Image, ImageFilter
class TestColorLut3DCoreAPI(PillowTestCase):
@ -27,7 +27,7 @@ class TestColorLut3DCoreAPI(PillowTestCase):
channels, size1D, size2D, size3D,
[item for sublist in table for item in sublist])
def test_wrong_arguments(self):
def test_wrong_args(self):
im = Image.new('RGB', (10, 10), 0)
with self.assertRaisesRegexp(ValueError, "filter"):
@ -66,7 +66,7 @@ class TestColorLut3DCoreAPI(PillowTestCase):
im.im.color_lut_3d('RGB', Image.LINEAR,
3, 2, 2, 2, [0, 0, 0] * 9)
def test_correct_arguments(self):
def test_correct_args(self):
im = Image.new('RGB', (10, 10), 0)
im.im.color_lut_3d('RGB', Image.LINEAR,
@ -209,5 +209,46 @@ class TestColorLut3DCoreAPI(PillowTestCase):
self.assertEqual(transformed[205, 205], (255, 255, 0))
class TestColorLut3DFilter(PillowTestCase):
def test_wrong_args(self):
with self.assertRaisesRegexp(ValueError, "should be an integer"):
ImageFilter.Color3DLUT("small", [1])
with self.assertRaisesRegexp(ValueError, "should be an integer"):
ImageFilter.Color3DLUT((11, 11), [1])
with self.assertRaisesRegexp(ValueError, r"in \[2, 65\] range"):
ImageFilter.Color3DLUT((11, 11, 1), [1])
with self.assertRaisesRegexp(ValueError, r"in \[2, 65\] range"):
ImageFilter.Color3DLUT((11, 11, 66), [1])
with self.assertRaisesRegexp(ValueError, "table should have .+ items"):
ImageFilter.Color3DLUT((3, 3, 3), [1, 1, 1])
with self.assertRaisesRegexp(ValueError, "table should have .+ items"):
ImageFilter.Color3DLUT((3, 3, 3), [[1, 1, 1]] * 2)
with self.assertRaisesRegexp(ValueError, "should have a length of 4"):
ImageFilter.Color3DLUT((3, 3, 3), [[1, 1, 1]] * 27, channels=4)
with self.assertRaisesRegexp(ValueError, "should have a length of 3"):
ImageFilter.Color3DLUT((2, 2, 2), [[1, 1]] * 8)
def test_convert_table(self):
flt = ImageFilter.Color3DLUT(2, [0, 1, 2] * 8)
self.assertEqual(tuple(flt.size), (2, 2, 2))
self.assertEqual(flt.name, "Color 3D LUT")
flt = ImageFilter.Color3DLUT((2, 2, 2), [
(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11),
(12, 13, 14), (15, 16, 17), (18, 19, 20), (21, 22, 23)])
self.assertEqual(tuple(flt.size), (2, 2, 2))
self.assertEqual(flt.table, list(range(24)))
flt = ImageFilter.Color3DLUT((2, 2, 2), [(0, 1, 2, 3)] * 8,
channels=4)
if __name__ == '__main__':
unittest.main()

View File

@ -43,6 +43,7 @@ class Kernel(MultibandFilter):
:param offset: Offset. If given, this value is added to the result,
after it has been divided by the scale factor.
"""
name = "Kernel"
def __init__(self, size, kernel, scale=None, offset=0):
if scale is None:
@ -320,6 +321,8 @@ class Color3DLUT(MultibandFilter):
than ``channels`` channels. Default is ``None``,
which means that mode wouldn't be changed.
"""
name = "Color 3D LUT"
def __init__(self, size, table, channels=3, target_mode=None):
try:
_, _, _ = size
@ -328,24 +331,31 @@ class Color3DLUT(MultibandFilter):
"tuple of three integers.")
except TypeError:
size = (size, size, size)
size = map(int, size)
for size1D in size:
if not 2 <= size1D <= 65:
raise ValueError("Size should be in [2, 65] range.")
self.size = size
self.channels = channels
self.mode = target_mode
table = list(table)
# Convert to a flat list
if isinstance(table[0], (list, tuple)):
table = [
pixel
for pixel in table
if len(pixel) == channels
for color in pixel
]
if table and isinstance(table[0], (list, tuple)):
table, raw_table = [], table
for pixel in raw_table:
if len(pixel) != channels:
raise ValueError("The elements of the table should have "
"a length of {}.".format(channels))
for color in pixel:
table.append(color)
if len(table) != channels * size[0] * size[1] * size[2]:
raise ValueError(
"The table should have channels * size**3 float items "
"either size**3 items of channels-sized tuples with floats.")
"either size**3 items of channels-sized tuples with floats. "
"Table length: {}".format(len(table)))
self.table = table
def filter(self, image):