feat(fx): 添加像素排序 (Pixel Sort) 滤镜

- 新增 '像素排序' 滤镜,实现类似 Photoshop Pixel Sort 效果
- 支持水平/垂直方向排序
- 支持多种排序依据:亮度、色相、红/绿/蓝通道
- 支持自动阈值计算(使用图像亮度中位数)
- 支持自定义遮罩阈值
- 支持反向排序
- 支持块大小参数
- 添加相关单元测试
This commit is contained in:
2026-04-08 14:20:46 +08:00
parent c2161635a8
commit cd010afc24
3 changed files with 200 additions and 0 deletions

View File

@ -86,3 +86,67 @@ def test_prase_input_args_parses_resize_second_argument_as_float():
assert len(filters) == 1
assert filters[0].name == "缩放"
assert filters[0].args == [2.0, 3.0]
def test_apply_pixel_sort_keeps_image_mode_and_size():
"""测试 Pixel Sort 保持图像的 mode 和 size"""
image = Image.new("RGBA", (10, 10), (255, 0, 0, 128))
result = ImageFilterImplement.apply_pixel_sort(image)
assert result.size == image.size
assert result.mode == "RGBA"
def test_apply_pixel_sort_horizontal():
"""测试水平方向的 Pixel Sort"""
# 创建一个简单的渐变图像
image = Image.new("RGB", (5, 3))
# 第一行:红到蓝渐变
image.putpixel((0, 0), (255, 0, 0))
image.putpixel((1, 0), (200, 0, 0))
image.putpixel((2, 0), (100, 0, 0))
image.putpixel((3, 0), (50, 0, 0))
image.putpixel((4, 0), (0, 0, 255))
# 填充其他行
for y in range(1, 3):
for x in range(5):
image.putpixel((x, y), (128, 128, 128))
result = ImageFilterImplement.apply_pixel_sort(
image, direction="horizontal", auto_threshold=False, mask_threshold=10
)
assert result.size == image.size
assert result.mode == "RGBA"
def test_apply_pixel_sort_vertical():
"""测试垂直方向的 Pixel Sort"""
image = Image.new("RGB", (3, 5))
# 第一列:绿到红渐变
image.putpixel((0, 0), (0, 255, 0))
image.putpixel((0, 1), (0, 200, 0))
image.putpixel((0, 2), (0, 100, 0))
image.putpixel((0, 3), (0, 50, 0))
image.putpixel((0, 4), (255, 0, 0))
# 填充其他列
for y in range(5):
for x in range(1, 3):
image.putpixel((x, y), (128, 128, 128))
result = ImageFilterImplement.apply_pixel_sort(
image, direction="vertical", auto_threshold=False, mask_threshold=10
)
assert result.size == image.size
assert result.mode == "RGBA"
def test_prase_input_args_parses_pixel_sort_arguments():
"""测试解析 Pixel Sort 参数"""
filters = prase_input_args("像素排序 horizontal 0 false brightness 128 false 1")
assert len(filters) == 1
assert filters[0].name == "像素排序"
assert filters[0].args == ["horizontal", 0.0, False, "brightness", 128.0, False, 1]