From cfdad89f3e24fd9d0c782f83823f986d6e89c2aa Mon Sep 17 00:00:00 2001 From: Chris Dumas Date: Mon, 12 Oct 2026 13:03:45 +0000 Subject: [PATCH] Restore image save previews and metadata --- dumas_image_nodes.py | 64 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 55 insertions(+), 9 deletions(-) diff --git a/dumas_image_nodes.py b/dumas_image_nodes.py index a6f8ddb..2e74e1c 100644 --- a/dumas_image_nodes.py +++ b/dumas_image_nodes.py @@ -1,6 +1,7 @@ """Generic image utilities for ComfyUI.""" import os +import json import re import time import uuid @@ -51,6 +52,25 @@ def _image_to_pil(image): return Image.fromarray(array) +def _is_within_directory(parent, child): + try: + return os.path.commonpath([os.path.realpath(parent), os.path.realpath(child)]) == os.path.realpath(parent) + except ValueError: + return False + + +def _build_pnginfo(prompt=None, extra_pnginfo=None): + if not prompt and not extra_pnginfo: + return None + from PIL.PngImagePlugin import PngInfo + info = PngInfo() + if prompt is not None: + info.add_text("prompt", json.dumps(prompt, ensure_ascii=False)) + for key, value in (extra_pnginfo or {}).items(): + info.add_text(str(key), json.dumps(value, ensure_ascii=False)) + return info + + class DumasImageCompareNode: @classmethod def INPUT_TYPES(cls): @@ -75,6 +95,7 @@ class DumasImageCompareNode: class DumasSaveImageNode: + DESCRIPTION = "Save image batches with collision-safe names and ComfyUI previews." @classmethod def INPUT_TYPES(cls): return { @@ -88,27 +109,52 @@ class DumasSaveImageNode: "save_on_run": ("BOOLEAN", {"default": True}), }, "optional": {"name": ("STRING", {"default": ""}), "name_2": ("STRING", {"default": ""})}, + "hidden": {"prompt": "PROMPT", "extra_pnginfo": "EXTRA_PNGINFO"}, } + @classmethod + def IS_CHANGED(cls, **_kwargs): + return float("nan") + RETURN_TYPES = () FUNCTION = "save_images" CATEGORY = "Dumas/Image" OUTPUT_NODE = True - def save_images(self, images, folder, pattern, format, quality, embed_workflow, save_on_run, name="", name_2="", **_kwargs): - del embed_workflow, save_on_run - base = Path(folder or folder_paths.get_output_directory()) + def save_images(self, images, folder, pattern, format, quality, embed_workflow, save_on_run, + name="", name_2="", prompt=None, extra_pnginfo=None, **_kwargs): + if not save_on_run: + return {"ui": {"images": []}} + output_dir = Path(folder_paths.get_output_directory()).resolve() + base = Path(folder).expanduser().resolve() if str(folder or "").strip() else output_dir base.mkdir(parents=True, exist_ok=True) + ui_images = [] + extension = ".jpg" if format == "jpg" else f".{format}" for index, image in enumerate(images): width, height = int(image.shape[2]), int(image.shape[1]) - stem = pattern.replace("%input%", _safe_token(name)).replace("%input2%", _safe_token(name_2)) + stem = str(pattern or "image_%counter%") + stem = stem.replace("%input%", _safe_token(name)).replace("%input2%", _safe_token(name_2)) stem = stem.replace("%width%", str(width)).replace("%height%", str(height)).replace("%batch_num%", str(index)) - stem = re.sub(r"%counter%", f"{index + 1:03d}", stem) - path = base / f"{stem}.{format}" + relative = stem.replace("\\", "/") + extension + counter = _next_counter_for_relative_path(str(base), relative) + relative = relative.replace("%counter%", str(counter).zfill(3)) + path = base / Path(relative) path.parent.mkdir(parents=True, exist_ok=True) - options = {"quality": quality} if format in {"jpg", "webp"} else {} - _image_to_pil(image).save(path, **options) - return {"ui": {"images": []}} + pil = _image_to_pil(image) + if format == "jpg": + pil.convert("RGB").save(path, "JPEG", quality=max(1, min(100, int(quality)))) + elif format == "webp": + pil.save(path, "WEBP", quality=max(1, min(100, int(quality)))) + else: + pil.save(path, "PNG", pnginfo=_build_pnginfo(prompt, extra_pnginfo) if embed_workflow else None) + if _is_within_directory(str(output_dir), str(path)): + subfolder = str(path.parent.relative_to(output_dir)).replace(os.sep, "/") + ui_images.append({"filename": path.name, "subfolder": "" if subfolder == "." else subfolder, "type": "output"}) + else: + token = uuid.uuid4().hex + _SERVE_TOKENS[token] = str(path) + ui_images.append({"filename": path.name, "subfolder": str(path.parent).replace(os.sep, "/"), "type": "external", "token": token}) + return {"ui": {"images": ui_images}} def _safe_token(value):