Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 12 additions & 34 deletions Tests/test_box_blur.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
from PIL import Image, ImageFilter

sample = Image.new("L", (7, 5))
# fmt: off
sample.putdata(sum([
[210, 50, 20, 10, 220, 230, 80],
[190, 210, 20, 180, 170, 40, 110],
[120, 210, 250, 60, 220, 0, 220],
[220, 40, 230, 80, 130, 250, 40],
[250, 0, 80, 30, 60, 20, 110],
], []))
# fmt: on
], [])) # fmt: skip


def test_imageops_box_blur() -> None:
Expand Down Expand Up @@ -85,14 +83,12 @@ def test_radius_0() -> None:
sample,
0,
[
# fmt: off
[210, 50, 20, 10, 220, 230, 80],
[190, 210, 20, 180, 170, 40, 110],
[120, 210, 250, 60, 220, 0, 220],
[220, 40, 230, 80, 130, 250, 40],
[250, 0, 80, 30, 60, 20, 110],
# fmt: on
],
], # fmt: skip
)


Expand All @@ -101,14 +97,12 @@ def test_radius_0_02() -> None:
sample,
0.02,
[
# fmt: off
[206, 55, 20, 17, 215, 223, 83],
[189, 203, 31, 171, 169, 46, 110],
[125, 206, 241, 69, 210, 13, 210],
[215, 49, 221, 82, 131, 235, 48],
[244, 7, 80, 32, 60, 27, 107],
# fmt: on
],
], # fmt: skip
delta=2,
)

Expand All @@ -118,14 +112,12 @@ def test_radius_0_05() -> None:
sample,
0.05,
[
# fmt: off
[202, 62, 22, 27, 209, 215, 88],
[188, 194, 44, 161, 168, 56, 111],
[131, 201, 229, 81, 198, 31, 198],
[209, 62, 209, 86, 133, 216, 59],
[237, 17, 80, 36, 60, 35, 103],
# fmt: on
],
], # fmt: skip
delta=2,
)

Expand All @@ -135,14 +127,12 @@ def test_radius_0_1() -> None:
sample,
0.1,
[
# fmt: off
[196, 72, 24, 40, 200, 203, 93],
[187, 183, 62, 148, 166, 68, 111],
[139, 193, 213, 96, 182, 54, 182],
[201, 78, 193, 91, 133, 191, 73],
[227, 31, 80, 42, 61, 47, 99],
# fmt: on
],
], # fmt: skip
delta=1,
)

Expand All @@ -152,14 +142,12 @@ def test_radius_0_5() -> None:
sample,
0.5,
[
# fmt: off
[176, 101, 46, 83, 163, 165, 111],
[176, 149, 108, 122, 144, 120, 117],
[164, 171, 159, 141, 134, 119, 129],
[170, 136, 133, 114, 116, 124, 109],
[184, 95, 72, 70, 69, 81, 89],
# fmt: on
],
], # fmt: skip
delta=1,
)

Expand All @@ -169,14 +157,12 @@ def test_radius_1() -> None:
sample,
1,
[
# fmt: off
[170, 109, 63, 97, 146, 153, 116],
[168, 142, 112, 128, 126, 143, 121],
[169, 166, 142, 149, 126, 131, 114],
[159, 156, 109, 127, 94, 117, 112],
[164, 128, 63, 87, 76, 89, 90],
# fmt: on
],
], # fmt: skip
delta=1,
)

Expand All @@ -186,14 +172,12 @@ def test_radius_1_5() -> None:
sample,
1.5,
[
# fmt: off
[155, 120, 105, 112, 124, 137, 130],
[160, 136, 124, 125, 127, 134, 130],
[166, 147, 130, 125, 120, 121, 119],
[168, 145, 119, 109, 103, 105, 110],
[168, 134, 96, 85, 85, 89, 97],
# fmt: on
],
], # fmt: skip
delta=1,
)

Expand All @@ -203,14 +187,12 @@ def test_radius_bigger_then_half() -> None:
sample,
3,
[
# fmt: off
[144, 145, 142, 128, 114, 115, 117],
[148, 145, 137, 122, 109, 111, 112],
[152, 145, 131, 117, 103, 107, 108],
[156, 144, 126, 111, 97, 102, 103],
[160, 144, 121, 106, 92, 98, 99],
# fmt: on
],
], # fmt: skip
delta=1,
)

Expand All @@ -225,7 +207,7 @@ def test_radius_bigger_then_width() -> None:
[159, 154, 148, 142, 136, 130, 124],
[160, 154, 148, 142, 137, 131, 125],
[160, 155, 149, 143, 137, 131, 125],
],
],
delta=0,
)

Expand All @@ -250,14 +232,12 @@ def test_two_passes() -> None:
sample,
1,
[
# fmt: off
[153, 123, 102, 109, 132, 135, 129],
[159, 138, 123, 121, 133, 131, 126],
[162, 147, 136, 124, 127, 121, 121],
[159, 140, 125, 108, 111, 106, 108],
[154, 126, 105, 87, 94, 93, 97],
# fmt: on
],
], # fmt: skip
passes=2,
delta=1,
)
Expand All @@ -268,14 +248,12 @@ def test_three_passes() -> None:
sample,
1,
[
# fmt: off
[146, 131, 116, 118, 126, 131, 130],
[151, 138, 125, 123, 126, 128, 127],
[154, 143, 129, 123, 120, 120, 119],
[152, 139, 122, 113, 108, 108, 108],
[148, 132, 112, 102, 97, 99, 100],
# fmt: on
],
], # fmt: skip
passes=3,
delta=1,
)
113 changes: 53 additions & 60 deletions Tests/test_color_lut.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,18 +258,21 @@ def test_channels_order(self) -> None:
)

# Reverse channels by splitting and using table
# fmt: off
assert_image_equal(
Image.merge('RGB', im.split()[::-1]),
im._new(im.im.color_lut_3d('RGB', Image.Resampling.BILINEAR,
3, (2, 2, 2), [
0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 1, 1,
table = [
0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 1, 1,

1, 0, 0, 1, 0, 1,
1, 1, 0, 1, 1, 1,
])))
# fmt: on
1, 0, 0, 1, 0, 1,
1, 1, 0, 1, 1, 1,
] # fmt: skip
assert_image_equal(
Image.merge("RGB", im.split()[::-1]),
im._new(
im.im.color_lut_3d(
"RGB", Image.Resampling.BILINEAR, 3, (2, 2, 2), table
)
),
)

def test_overflow(self) -> None:
g = Image.linear_gradient("L")
Expand All @@ -282,17 +285,17 @@ def test_overflow(self) -> None:
],
)

# fmt: off
transformed = im._new(im.im.color_lut_3d('RGB', Image.Resampling.BILINEAR,
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()
# fmt: on
table = [
-1, -1, -1, 2, -1, -1,
-1, 2, -1, 2, 2, -1,

-1, -1, 2, 2, -1, 2,
-1, 2, 2, 2, 2, 2,
] # fmt: skip

transformed = im._new(
im.im.color_lut_3d("RGB", Image.Resampling.BILINEAR, 3, (2, 2, 2), table)
).load()
assert transformed is not None
assert transformed[0, 0] == (0, 0, 255)
assert transformed[50, 50] == (0, 0, 255)
Expand All @@ -303,17 +306,16 @@ def test_overflow(self) -> None:
assert transformed[255, 255] == (255, 255, 0)
assert transformed[205, 205] == (255, 255, 0)

# fmt: off
transformed = im._new(im.im.color_lut_3d('RGB', Image.Resampling.BILINEAR,
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()
# fmt: on
table = [
-3, -3, -3, 5, -3, -3,
-3, 5, -3, 5, 5, -3,

-3, -3, 5, 5, -3, 5,
-3, 5, 5, 5, 5, 5,
] # fmt: skip
transformed = im._new(
im.im.color_lut_3d("RGB", Image.Resampling.BILINEAR, 3, (2, 2, 2), table)
).load()
assert transformed is not None
assert transformed[0, 0] == (0, 0, 255)
assert transformed[50, 50] == (0, 0, 255)
Expand Down Expand Up @@ -359,11 +361,10 @@ def test_convert_table(self) -> None:
assert tuple(lut.size) == (2, 2, 2)
assert lut.name == "Color 3D LUT"

# fmt: off
lut = 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)])
# fmt: on
(12, 13, 14), (15, 16, 17), (18, 19, 20), (21, 22, 23),
]) # fmt: skip
assert tuple(lut.size) == (2, 2, 2)
assert lut.table == list(range(24))

Expand Down Expand Up @@ -484,24 +485,21 @@ def test_3_channels(self) -> None:
lut = ImageFilter.Color3DLUT.generate(5, lambda r, g, b: (r, g, b))
assert tuple(lut.size) == (5, 5, 5)
assert lut.name == "Color 3D LUT"
# fmt: off
assert lut.table[:24] == [
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]
# fmt: on
1.0, 0.0, 0.0, 0.0, 0.25, 0.0, 0.25, 0.25, 0.0, 0.5, 0.25, 0.0,
] # fmt: skip

def test_4_channels(self) -> None:
lut = ImageFilter.Color3DLUT.generate(
5, channels=4, callback=lambda r, g, b: (b, r, g, (r + g + b) / 2)
)
assert tuple(lut.size) == (5, 5, 5)
assert lut.name == "Color 3D LUT"
# fmt: off
assert lut.table[:24] == [
0.0, 0.0, 0.0, 0.0, 0.0, 0.25, 0.0, 0.125, 0.0, 0.5, 0.0, 0.25,
0.0, 0.75, 0.0, 0.375, 0.0, 1.0, 0.0, 0.5, 0.0, 0.0, 0.25, 0.125
]
# fmt: on
] # fmt: skip

def test_apply(self) -> None:
lut = ImageFilter.Color3DLUT.generate(5, lambda r, g, b: (r, g, b))
Expand Down Expand Up @@ -559,11 +557,10 @@ def test_3_to_4_channels(self) -> None:
assert tuple(lut.size) == tuple(source.size)
assert len(lut.table) != len(source.table)
assert lut.table != source.table
# fmt: off
assert lut.table[: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]
# fmt: on
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,
] # fmt: skip

def test_4_to_3_channels(self) -> None:
source = ImageFilter.Color3DLUT.generate(
Expand All @@ -575,11 +572,10 @@ def test_4_to_3_channels(self) -> None:
assert tuple(lut.size) == tuple(source.size)
assert len(lut.table) != len(source.table)
assert lut.table != source.table
# fmt: off
assert lut.table[: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]
# fmt: on
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,
] # fmt: skip

def test_4_to_4_channels(self) -> None:
source = ImageFilter.Color3DLUT.generate(
Expand All @@ -589,11 +585,10 @@ def test_4_to_4_channels(self) -> None:
assert tuple(lut.size) == tuple(source.size)
assert len(lut.table) == len(source.table)
assert lut.table != source.table
# fmt: off
assert lut.table[: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]
# fmt: on
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,
] # fmt: skip

def test_with_normals_3_channels(self) -> None:
source = ImageFilter.Color3DLUT.generate(
Expand All @@ -605,11 +600,10 @@ def test_with_normals_3_channels(self) -> None:
assert tuple(lut.size) == tuple(source.size)
assert len(lut.table) == len(source.table)
assert lut.table != source.table
# fmt: off
assert lut.table[: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]
# fmt: on
0.24, 0.0, 0.0, 0.8 - (0.8**2), 0, 0, 0, 0, 0,
] # fmt: skip

def test_with_normals_4_channels(self) -> None:
source = ImageFilter.Color3DLUT.generate(
Expand All @@ -622,8 +616,7 @@ def test_with_normals_4_channels(self) -> None:
assert tuple(lut.size) == tuple(source.size)
assert len(lut.table) == len(source.table)
assert lut.table != source.table
# fmt: off
assert lut.table[: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]
# fmt: on
0.0, 0.0, 0.0, 0.5, 0.0, 0.16, 0.0, 0.5,
] # fmt: skip
Loading
Loading