Skip to content
Merged
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
504 changes: 503 additions & 1 deletion src/pixelflow/api/static/index.html

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions src/pixelflow/operations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
ColorSplashOperation,
ColorSplashParams,
)
from pixelflow.operations.color_wheels import (
ColorWheelsOperation,
ColorWheelsParams,
)
from pixelflow.operations.curves import (
CurvesOperation,
CurvesParams,
Expand Down Expand Up @@ -118,6 +122,10 @@
StampOperation,
StampParams,
)
from pixelflow.operations.selective_adjust import (
SelectiveAdjustOperation,
SelectiveAdjustParams,
)
from pixelflow.operations.signature import (
SignatureExtractOperation,
SignatureExtractParams,
Expand Down Expand Up @@ -158,6 +166,8 @@
"CannyParams",
"ColorSplashOperation",
"ColorSplashParams",
"ColorWheelsOperation",
"ColorWheelsParams",
"CropOperation",
"CropParams",
"CurvesOperation",
Expand Down Expand Up @@ -208,6 +218,8 @@
"ResizeParams",
"RotateOperation",
"RotateParams",
"SelectiveAdjustOperation",
"SelectiveAdjustParams",
"ShadowRemovalOperation",
"ShadowRemovalParams",
"SharpenOperation",
Expand Down
61 changes: 61 additions & 0 deletions src/pixelflow/operations/color_wheels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from __future__ import annotations

from typing import cast

import numpy as np

from pixelflow.core.image_utils import ensure_rgb, merge_alpha, split_alpha, to_uint8
from pixelflow.core.operation import ImageArray, ImageOperation, OperationParams
from pixelflow.core.registry import register_operation


class ColorWheelsParams(OperationParams):
lift: tuple[float, float, float] = (0.0, 0.0, 0.0)
gamma: tuple[float, float, float] = (1.0, 1.0, 1.0)
gain: tuple[float, float, float] = (1.0, 1.0, 1.0)
lift_master: float = 0.0
gamma_master: float = 1.0
gain_master: float = 1.0


def _compute_channel_grading(
channel: np.ndarray,
lift: float,
gamma: float,
gain: float,
) -> np.ndarray:
scaled = np.clip(channel * gain + lift, 0.0, 1.0)
safe_gamma = max(0.01, gamma)
graded = np.power(scaled, 1.0 / safe_gamma)
return np.clip(graded, 0.0, 1.0)


def _apply_color_wheels_rgb(
rgb: np.ndarray,
params: ColorWheelsParams,
) -> np.ndarray:
norm = rgb.astype(np.float32) / 255.0
out = np.empty_like(norm)
for c in range(3):
eff_lift = params.lift[c] + params.lift_master
eff_gain = max(0.0, params.gain[c] * params.gain_master)
eff_gamma = max(0.01, params.gamma[c] * params.gamma_master)
out[:, :, c] = _compute_channel_grading(
norm[:, :, c], eff_lift, eff_gamma, eff_gain
)
return np.clip(out * 255.0, 0.0, 255.0).round().astype(np.uint8)


@register_operation("color_wheels")
class ColorWheelsOperation(ImageOperation[ColorWheelsParams]):
operation_name = "color_wheels"
params_model = ColorWheelsParams

def _execute(self, image: ImageArray) -> ImageArray:
u8 = to_uint8(image)
base, alpha = split_alpha(u8)
rgb = ensure_rgb(base)
graded = _apply_color_wheels_rgb(rgb, self.params)
if alpha is not None:
return merge_alpha(graded, alpha)
return cast(ImageArray, graded)
117 changes: 117 additions & 0 deletions src/pixelflow/operations/selective_adjust.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
from __future__ import annotations

from typing import Literal, cast

import cv2
import numpy as np
from pydantic import Field

from pixelflow.core.image_utils import ensure_rgb, merge_alpha, split_alpha, to_uint8
from pixelflow.core.operation import ImageArray, ImageOperation, OperationParams
from pixelflow.core.registry import register_operation

MaskShape = Literal["radial", "linear", "box"]


class SelectiveAdjustParams(OperationParams):
shape: MaskShape = "radial"
center: tuple[float, float] = (0.5, 0.5)
size: tuple[float, float] = (0.3, 0.3)
feather: float = Field(default=0.2, ge=0.0, le=1.0)
angle: float = 0.0
invert: bool = False
exposure: float = Field(default=0.0, ge=-2.0, le=2.0)
contrast: float = Field(default=1.0, ge=0.0, le=3.0)
saturation: float = Field(default=1.0, ge=0.0, le=3.0)
warmth: float = Field(default=0.0, ge=-1.0, le=1.0)
blur_radius: int = Field(default=0, ge=0, le=50)


def _build_radial_mask(h: int, w: int, params: SelectiveAdjustParams) -> np.ndarray:
ys, xs = np.ogrid[:h, :w]
cx = params.center[0] * w
cy = params.center[1] * h
rx = max(1.0, params.size[0] * w)
ry = max(1.0, params.size[1] * h)
dist = np.sqrt(((xs - cx) / rx) ** 2 + ((ys - cy) / ry) ** 2)
feather = max(0.01, params.feather)
val = (1.0 - dist) / feather
return cast(np.ndarray, np.clip(val, 0.0, 1.0))


def _build_linear_mask(h: int, w: int, params: SelectiveAdjustParams) -> np.ndarray:
ys, xs = np.mgrid[:h, :w]
cx = params.center[0] * w
cy = params.center[1] * h
rad = np.radians(params.angle)
proj = (xs - cx) * np.sin(rad) + (ys - cy) * np.cos(rad)
span = max(1.0, params.size[1] * h)
feather = max(0.01, params.feather) * span
val = 0.5 - (proj / (span + feather))
return cast(np.ndarray, np.clip(val, 0.0, 1.0))


def _build_box_mask(h: int, w: int, params: SelectiveAdjustParams) -> np.ndarray:
ys, xs = np.ogrid[:h, :w]
cx = params.center[0] * w
cy = params.center[1] * h
rx = max(1.0, params.size[0] * w * 0.5)
ry = max(1.0, params.size[1] * h * 0.5)
dx = np.maximum(0.0, np.abs(xs - cx) - rx)
dy = np.maximum(0.0, np.abs(ys - cy) - ry)
dist = np.sqrt(dx**2 + dy**2)
feather = max(1.0, params.feather * max(rx, ry))
return cast(np.ndarray, np.clip(1.0 - (dist / feather), 0.0, 1.0))


def _generate_mask(h: int, w: int, params: SelectiveAdjustParams) -> np.ndarray:
if params.shape == "radial":
mask = _build_radial_mask(h, w, params)
elif params.shape == "linear":
mask = _build_linear_mask(h, w, params)
else:
mask = _build_box_mask(h, w, params)
if params.invert:
mask = 1.0 - mask
return mask[:, :, np.newaxis]


def _apply_tone_adjustments(
rgb: np.ndarray,
params: SelectiveAdjustParams,
) -> np.ndarray:
norm = rgb.astype(np.float32) / 255.0
if params.exposure != 0.0:
norm = norm * (2.0**params.exposure)
if params.contrast != 1.0:
norm = (norm - 0.5) * params.contrast + 0.5
if params.saturation != 1.0:
gray = norm[:, :, 0] * 0.299 + norm[:, :, 1] * 0.587 + norm[:, :, 2] * 0.114
norm = gray[:, :, np.newaxis] + (norm - gray[:, :, np.newaxis]) * params.saturation
if params.warmth != 0.0:
norm[:, :, 0] += params.warmth * 0.1
norm[:, :, 2] -= params.warmth * 0.1
clipped = np.clip(norm * 255.0, 0.0, 255.0).astype(np.uint8)
if params.blur_radius > 0:
k = params.blur_radius if params.blur_radius % 2 == 1 else params.blur_radius + 1
blurred = cv2.GaussianBlur(clipped, (k, k), 0)
return cast(np.ndarray, blurred)
return cast(np.ndarray, clipped)


@register_operation("selective_adjust")
class SelectiveAdjustOperation(ImageOperation[SelectiveAdjustParams]):
operation_name = "selective_adjust"
params_model = SelectiveAdjustParams

def _execute(self, image: ImageArray) -> ImageArray:
u8 = to_uint8(image)
base, alpha = split_alpha(u8)
rgb = ensure_rgb(base)
mask = _generate_mask(rgb.shape[0], rgb.shape[1], self.params)
adjusted = _apply_tone_adjustments(rgb, self.params).astype(np.float32)
blended = (rgb.astype(np.float32) * (1.0 - mask) + adjusted * mask)
out = np.clip(blended, 0.0, 255.0).round().astype(np.uint8)
if alpha is not None:
return merge_alpha(out, alpha)
return cast(ImageArray, out)
64 changes: 64 additions & 0 deletions tests/test_color_wheels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from __future__ import annotations

import numpy as np

from pixelflow.core.registry import default_registry
from pixelflow.operations.color_wheels import ColorWheelsOperation, ColorWheelsParams


def test_color_wheels_identity() -> None:
img = np.full((20, 20, 3), 128, dtype=np.uint8)
op = ColorWheelsOperation(ColorWheelsParams())
result = op.apply(img)
assert np.allclose(result, 128, atol=2)


def test_color_wheels_lift_brightens_shadows() -> None:
img = np.zeros((20, 20, 3), dtype=np.uint8)
params = ColorWheelsParams(lift=(0.2, 0.1, 0.0))
op = ColorWheelsOperation(params)
result = op.apply(img)
assert result[0, 0, 0] > 40
assert result[0, 0, 1] > 20
assert result[0, 0, 2] == 0


def test_color_wheels_gain_affects_highlights() -> None:
img = np.full((10, 10, 3), 200, dtype=np.uint8)
params = ColorWheelsParams(gain=(1.2, 0.8, 1.0))
op = ColorWheelsOperation(params)
result = op.apply(img)
assert result[0, 0, 0] > 200
assert result[0, 0, 1] < 200


def test_color_wheels_gamma_shifts_midtones() -> None:
img = np.full((10, 10, 3), 128, dtype=np.uint8)
params = ColorWheelsParams(gamma=(0.5, 2.0, 1.0))
op = ColorWheelsOperation(params)
result = op.apply(img)
assert result[0, 0, 0] < 128
assert result[0, 0, 1] > 128


def test_color_wheels_master_controls() -> None:
img = np.full((10, 10, 3), 100, dtype=np.uint8)
params = ColorWheelsParams(lift_master=0.1, gain_master=1.1, gamma_master=0.9)
op = ColorWheelsOperation(params)
result = op.apply(img)
assert result[0, 0, 0] > 100


def test_color_wheels_alpha_preserved() -> None:
rgba = np.full((10, 10, 4), 128, dtype=np.uint8)
rgba[:, :, 3] = 200
params = ColorWheelsParams(lift=(0.1, 0.1, 0.1))
op = ColorWheelsOperation(params)
result = op.apply(rgba)
assert result.shape == (10, 10, 4)
assert np.all(result[:, :, 3] == 200)


def test_color_wheels_registered_in_registry() -> None:
op_cls = default_registry.get("color_wheels")
assert op_cls is ColorWheelsOperation
96 changes: 96 additions & 0 deletions tests/test_selective_adjust.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
from __future__ import annotations

import numpy as np

from pixelflow.core.registry import default_registry
from pixelflow.operations.selective_adjust import (
SelectiveAdjustOperation,
SelectiveAdjustParams,
)


def test_selective_adjust_radial_brightness() -> None:
img = np.full((50, 50, 3), 100, dtype=np.uint8)
params = SelectiveAdjustParams(
shape="radial",
center=(0.5, 0.5),
size=(0.2, 0.2),
exposure=1.0,
contrast=1.0,
)
op = SelectiveAdjustOperation(params)
res = op.apply(img)
assert res[25, 25, 0] > 150
assert res[0, 0, 0] == 100


def test_selective_adjust_linear_gradient() -> None:
img = np.full((40, 40, 3), 120, dtype=np.uint8)
params = SelectiveAdjustParams(
shape="linear",
center=(0.5, 0.5),
angle=0.0,
exposure=0.5,
)
op = SelectiveAdjustOperation(params)
res = op.apply(img)
assert res.shape == (40, 40, 3)
assert res[5, 20, 0] >= res[35, 20, 0]


def test_selective_adjust_box_mask() -> None:
img = np.full((30, 30, 3), 80, dtype=np.uint8)
params = SelectiveAdjustParams(
shape="box",
center=(0.5, 0.5),
size=(0.4, 0.4),
feather=0.1,
exposure=1.0,
)
op = SelectiveAdjustOperation(params)
res = op.apply(img)
assert res[15, 15, 0] > 120
assert res[0, 0, 0] == 80


def test_selective_adjust_invert_and_warmth() -> None:
img = np.full((20, 20, 3), 120, dtype=np.uint8)
params = SelectiveAdjustParams(
shape="radial",
center=(0.5, 0.5),
size=(0.3, 0.3),
invert=True,
warmth=0.8,
saturation=1.5,
)
op = SelectiveAdjustOperation(params)
res = op.apply(img)
assert res[0, 0, 0] > res[0, 0, 2]


def test_selective_adjust_blur_radius() -> None:
img = np.random.randint(0, 255, (30, 30, 3), dtype=np.uint8)
params = SelectiveAdjustParams(
shape="radial",
center=(0.5, 0.5),
size=(0.4, 0.4),
blur_radius=5,
)
op = SelectiveAdjustOperation(params)
res = op.apply(img)
assert res.shape == (30, 30, 3)


def test_selective_adjust_alpha_preservation() -> None:
rgba = np.full((20, 20, 4), 100, dtype=np.uint8)
rgba[:, :, 3] = 220
params = SelectiveAdjustParams(exposure=0.5)
op = SelectiveAdjustOperation(params)
res = op.apply(rgba)
assert res.shape == (20, 20, 4)
assert np.all(res[:, :, 3] == 220)


def test_selective_adjust_registered() -> None:
op_cls = default_registry.get("selective_adjust")
assert op_cls is SelectiveAdjustOperation
Loading