Skip to content

Image Pipeline Playground

feat(backend/nodes): load images as an array from file or folder

Mohammad Rajaeidad6d84df919
diff --git a/backend/app/nodes/common.py b/backend/app/nodes/common.py
index 290b830..6d0e3f5 100644
--- a/backend/app/nodes/common.py
+++ b/backend/app/nodes/common.py
@@ -13,8 +13,19 @@ def image_in(port_id: str = "image", name: str = "Image") -> PortSpec:
return PortSpec(id=port_id, name=name, direction=PortDirection.INPUT, data_type="image")
-def image_out(port_id: str = "image", name: str = "Image") -> PortSpec:
- return PortSpec(id=port_id, name=name, direction=PortDirection.OUTPUT, data_type="image")
+def image_out(
+ port_id: str = "image",
+ name: str = "Image",
+ *,
+ multiple: bool = False,
+) -> PortSpec:
+ return PortSpec(
+ id=port_id,
+ name=name,
+ direction=PortDirection.OUTPUT,
+ data_type="image",
+ multiple=multiple,
+ )
def require_image(
diff --git a/backend/app/nodes/io.py b/backend/app/nodes/io.py
index ed9a9e6..b2ccea6 100644
--- a/backend/app/nodes/io.py
+++ b/backend/app/nodes/io.py
@@ -51,17 +51,17 @@ def read_image(path: Path) -> np.ndarray:
class LoadImageNode(BaseNode):
type = "load_image"
- label = "Load Image"
+ label = "Load Images"
category = "io"
- description = "Load an image file, or a folder of images (one per sample)."
- ports = [image_out()]
+ description = "Load one or more images from files or a folder (output is an image array)."
+ ports = [image_out(multiple=True)]
params = [
file_param(
"path",
- "Image / Folder",
+ "Images / Folder",
"",
accept=IMAGE_EXTENSIONS,
- description="Select an image file or a folder of images",
+ description="Select multiple images or a folder of images",
)
]
@@ -73,11 +73,11 @@ class LoadImageNode(BaseNode):
) -> dict[str, np.ndarray | list[np.ndarray]]:
path_value = str(params["path"])
if not path_value:
- raise ValueError("Load Image requires a file or folder selection")
+ raise ValueError("Load Images requires a file or folder selection")
path = Path(path_value)
files = list_image_files(path)
- selected = files[seed % len(files)]
- return {"image": read_image(selected)}
+ images = [read_image(file_path) for file_path in files]
+ return {"image": images}
def emit_python(
self,
@@ -93,14 +93,16 @@ class LoadImageNode(BaseNode):
"_exts = {'.png', '.jpg', '.jpeg', '.bmp', '.tif', '.tiff', '.webp', '.gif'}",
"if _path.is_dir():",
" _files = sorted(p for p in _path.iterdir() if p.suffix.lower() in _exts)",
- " if not _files:",
- f" raise FileNotFoundError({path!r})",
- " _selected = _files[seed % len(_files)]",
"else:",
- " _selected = _path",
- f"{dst} = cv2.imread(str(_selected), cv2.IMREAD_UNCHANGED)",
- f"if {dst} is None:",
- " raise FileNotFoundError(str(_selected))",
+ " _files = [_path]",
+ "if not _files:",
+ f" raise FileNotFoundError({path!r})",
+ f"{dst} = []",
+ "for _file in _files:",
+ " _img = cv2.imread(str(_file), cv2.IMREAD_UNCHANGED)",
+ " if _img is None:",
+ " raise FileNotFoundError(str(_file))",
+ f" {dst}.append(_img)",
]
diff --git a/backend/tests/test_load_image.py b/backend/tests/test_load_image.py
index 132ed73..89a86c1 100644
--- a/backend/tests/test_load_image.py
+++ b/backend/tests/test_load_image.py
@@ -25,16 +25,24 @@ def test_list_image_files_filters_extensions(tmp_path: Path) -> None:
assert [path.name for path in files] == ["a.png", "b.jpg"]
-def test_load_image_from_folder_uses_seed(tmp_path: Path) -> None:
+def test_load_image_returns_array_from_folder(tmp_path: Path) -> None:
register_builtin_nodes()
_write(tmp_path / "a.png", 10)
_write(tmp_path / "b.png", 40)
node = registry.get("load_image")
- first = node.execute({}, {"path": str(tmp_path)}, seed=0)["image"]
- second = node.execute({}, {"path": str(tmp_path)}, seed=1)["image"]
- assert isinstance(first, np.ndarray)
- assert isinstance(second, np.ndarray)
- assert int(first.mean()) != int(second.mean())
+ loaded = node.execute({}, {"path": str(tmp_path)}, seed=0)["image"]
+ assert isinstance(loaded, list)
+ assert len(loaded) == 2
+ assert int(loaded[0].mean()) != int(loaded[1].mean())
+
+
+def test_load_image_single_file_still_returns_list(tmp_path: Path) -> None:
+ register_builtin_nodes()
+ path = tmp_path / "only.png"
+ _write(path, 12)
+ loaded = registry.get("load_image").execute({}, {"path": str(path)}, seed=0)["image"]
+ assert isinstance(loaded, list)
+ assert len(loaded) == 1
def test_load_image_metadata_exposes_file_accept() -> None:
@@ -44,3 +52,4 @@ def test_load_image_metadata_exposes_file_accept() -> None:
assert field.type == "file"
assert field.accept is not None
assert ".png" in field.accept
+ assert meta.ports[0].multiple is True
diff --git a/backend/tests/test_nodes.py b/backend/tests/test_nodes.py
index 30ff9ea..8012256 100644
--- a/backend/tests/test_nodes.py
+++ b/backend/tests/test_nodes.py
@@ -43,8 +43,9 @@ def test_filter_pipeline_deterministic(tmp_path) -> None:
cv2.imwrite(str(path), image)
loaded = registry.get("load_image").execute({}, {"path": str(path)}, seed=0)["image"]
+ assert isinstance(loaded, list)
blurred = registry.get("gaussian_blur").execute(
- {"image": loaded},
+ {"image": loaded[0]},
{"ksize": 3, "sigma": 0},
seed=0,
)["image"]