Skip to content

Image Pipeline Playground

feat(backend/nodes): add blank image node with optional size ref

Mohammad Rajaeicb6650755ccf
diff --git a/backend/app/nodes/__init__.py b/backend/app/nodes/__init__.py
index 34fc0cd..e5ca45c 100644
--- a/backend/app/nodes/__init__.py
+++ b/backend/app/nodes/__init__.py
@@ -36,7 +36,7 @@ from app.nodes.filters import (
ThresholdNode,
)
from app.nodes.geometry import CropNode, FlipNode, ResizeNode, RotateNode
-from app.nodes.io import LoadImageNode, PreviewNode, SaveImageNode
+from app.nodes.io import BlankImageNode, LoadImageNode, PreviewNode, SaveImageNode
from app.nodes.morphology import DilateNode, ErodeNode, MorphologyExNode
from app.nodes.stochastic import GaussianNoiseNode, RandomBrightnessContrastNode
from app.nodes.structure import (
@@ -58,6 +58,7 @@ def register_builtin_nodes() -> None:
return
for node in (
LoadImageNode(),
+ BlankImageNode(),
SaveImageNode(),
PreviewNode(),
ToGrayNode(),
diff --git a/backend/app/nodes/io.py b/backend/app/nodes/io.py
index b2ccea6..9616112 100644
--- a/backend/app/nodes/io.py
+++ b/backend/app/nodes/io.py
@@ -14,7 +14,9 @@ from app.nodes.common import (
file_param,
image_in,
image_out,
+ int_param,
require_image,
+ select_param,
string_param,
)
@@ -164,3 +166,83 @@ class PreviewNode(BaseNode):
output_vars: dict[str, str],
) -> list[str]:
return [f"{output_vars['image']} = {input_vars['image']} # preview"]
+
+
+class BlankImageNode(BaseNode):
+ type = "blank_image"
+ label = "Blank Image"
+ category = "io"
+ description = (
+ "Create a blank image with custom size. "
+ "Optionally match size from a connected reference image."
+ )
+ ports = [
+ image_in("size_ref", "Size Ref", optional=True),
+ image_out(),
+ ]
+ params = [
+ int_param("width", "Width", 256, minimum=1, maximum=8192),
+ int_param("height", "Height", 256, minimum=1, maximum=8192),
+ select_param("channels", "Channels", "bgr", ["gray", "bgr", "bgra"]),
+ int_param("fill", "Fill", 0, minimum=0, maximum=255),
+ ]
+
+ def execute(
+ self,
+ inputs: dict[str, np.ndarray | list[np.ndarray] | None],
+ params: dict[str, Any],
+ seed: int = 0,
+ ) -> dict[str, np.ndarray | list[np.ndarray]]:
+ width = max(1, int(params["width"]))
+ height = max(1, int(params["height"]))
+ fill = int(params["fill"])
+ channels = str(params["channels"])
+
+ size_ref = inputs.get("size_ref")
+ if size_ref is not None:
+ if isinstance(size_ref, list):
+ if not size_ref:
+ raise ValueError("Empty image list for optional size_ref input")
+ ref = size_ref[0]
+ else:
+ ref = size_ref
+ height, width = int(ref.shape[0]), int(ref.shape[1])
+
+ if channels == "gray":
+ image = np.full((height, width), fill, dtype=np.uint8)
+ elif channels == "bgra":
+ image = np.full((height, width, 4), fill, dtype=np.uint8)
+ else:
+ image = np.full((height, width, 3), fill, dtype=np.uint8)
+ return {"image": image}
+
+ def emit_python(
+ self,
+ node_id: str,
+ params: dict[str, Any],
+ input_vars: dict[str, str],
+ output_vars: dict[str, str],
+ ) -> list[str]:
+ dst = output_vars["image"]
+ width = max(1, int(params["width"]))
+ height = max(1, int(params["height"]))
+ fill = int(params["fill"])
+ channels = str(params["channels"])
+ lines: list[str] = [
+ f"_w, _h, _fill = {width}, {height}, {fill}",
+ ]
+ if "size_ref" in input_vars:
+ ref = input_vars["size_ref"]
+ lines.extend(
+ [
+ f"_ref = {ref}[0] if isinstance({ref}, list) else {ref}",
+ "_h, _w = int(_ref.shape[0]), int(_ref.shape[1])",
+ ]
+ )
+ if channels == "gray":
+ lines.append(f"{dst} = np.full((_h, _w), _fill, dtype='uint8')")
+ elif channels == "bgra":
+ lines.append(f"{dst} = np.full((_h, _w, 4), _fill, dtype='uint8')")
+ else:
+ lines.append(f"{dst} = np.full((_h, _w, 3), _fill, dtype='uint8')")
+ return lines
diff --git a/backend/tests/test_io_blank_image.py b/backend/tests/test_io_blank_image.py
new file mode 100644
index 0000000..8dbdd59
--- /dev/null
+++ b/backend/tests/test_io_blank_image.py
@@ -0,0 +1,50 @@
+"""Tests for blank-image I/O node."""
+
+from __future__ import annotations
+
+import numpy as np
+
+from app.engine.registry import registry
+from app.nodes import register_builtin_nodes
+
+
+def test_blank_image_registered() -> None:
+ register_builtin_nodes()
+ meta = next(m for m in registry.list_metadata() if m.type == "blank_image")
+ size_ref = next(port for port in meta.ports if port.id == "size_ref")
+ assert size_ref.optional is True
+ assert size_ref.name == "Size Ref"
+
+
+def test_blank_image_custom_size() -> None:
+ register_builtin_nodes()
+ result = registry.get("blank_image").execute(
+ {},
+ {"width": 120, "height": 80, "channels": "bgr", "fill": 0},
+ seed=0,
+ )["image"]
+ assert result.shape == (80, 120, 3)
+ assert int(result.sum()) == 0
+
+
+def test_blank_image_uses_optional_size_ref() -> None:
+ register_builtin_nodes()
+ ref = np.zeros((45, 90), dtype=np.uint8)
+ result = registry.get("blank_image").execute(
+ {"size_ref": ref},
+ {"width": 10, "height": 10, "channels": "gray", "fill": 128},
+ seed=0,
+ )["image"]
+ assert result.shape == (45, 90)
+ assert int(result[0, 0]) == 128
+
+
+def test_blank_image_without_size_ref() -> None:
+ register_builtin_nodes()
+ result = registry.get("blank_image").execute(
+ {"size_ref": None},
+ {"width": 16, "height": 24, "channels": "bgra", "fill": 255},
+ seed=0,
+ )["image"]
+ assert result.shape == (24, 16, 4)
+ assert int(result[0, 0, 0]) == 255
diff --git a/backend/tests/test_nodes.py b/backend/tests/test_nodes.py
index c2ad4bd..69c1576 100644
--- a/backend/tests/test_nodes.py
+++ b/backend/tests/test_nodes.py
@@ -12,6 +12,7 @@ def test_builtin_nodes_registered() -> None:
register_builtin_nodes()
expected = {
"load_image",
+ "blank_image",
"save_image",
"preview",
"to_gray",