Restore image save previews and metadata
This commit is contained in:
+55
-9
@@ -1,6 +1,7 @@
|
|||||||
"""Generic image utilities for ComfyUI."""
|
"""Generic image utilities for ComfyUI."""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import json
|
||||||
import re
|
import re
|
||||||
import time
|
import time
|
||||||
import uuid
|
import uuid
|
||||||
@@ -51,6 +52,25 @@ def _image_to_pil(image):
|
|||||||
return Image.fromarray(array)
|
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:
|
class DumasImageCompareNode:
|
||||||
@classmethod
|
@classmethod
|
||||||
def INPUT_TYPES(cls):
|
def INPUT_TYPES(cls):
|
||||||
@@ -75,6 +95,7 @@ class DumasImageCompareNode:
|
|||||||
|
|
||||||
|
|
||||||
class DumasSaveImageNode:
|
class DumasSaveImageNode:
|
||||||
|
DESCRIPTION = "Save image batches with collision-safe names and ComfyUI previews."
|
||||||
@classmethod
|
@classmethod
|
||||||
def INPUT_TYPES(cls):
|
def INPUT_TYPES(cls):
|
||||||
return {
|
return {
|
||||||
@@ -88,27 +109,52 @@ class DumasSaveImageNode:
|
|||||||
"save_on_run": ("BOOLEAN", {"default": True}),
|
"save_on_run": ("BOOLEAN", {"default": True}),
|
||||||
},
|
},
|
||||||
"optional": {"name": ("STRING", {"default": ""}), "name_2": ("STRING", {"default": ""})},
|
"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 = ()
|
RETURN_TYPES = ()
|
||||||
FUNCTION = "save_images"
|
FUNCTION = "save_images"
|
||||||
CATEGORY = "Dumas/Image"
|
CATEGORY = "Dumas/Image"
|
||||||
OUTPUT_NODE = True
|
OUTPUT_NODE = True
|
||||||
|
|
||||||
def save_images(self, images, folder, pattern, format, quality, embed_workflow, save_on_run, name="", name_2="", **_kwargs):
|
def save_images(self, images, folder, pattern, format, quality, embed_workflow, save_on_run,
|
||||||
del embed_workflow, save_on_run
|
name="", name_2="", prompt=None, extra_pnginfo=None, **_kwargs):
|
||||||
base = Path(folder or folder_paths.get_output_directory())
|
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)
|
base.mkdir(parents=True, exist_ok=True)
|
||||||
|
ui_images = []
|
||||||
|
extension = ".jpg" if format == "jpg" else f".{format}"
|
||||||
for index, image in enumerate(images):
|
for index, image in enumerate(images):
|
||||||
width, height = int(image.shape[2]), int(image.shape[1])
|
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 = stem.replace("%width%", str(width)).replace("%height%", str(height)).replace("%batch_num%", str(index))
|
||||||
stem = re.sub(r"%counter%", f"{index + 1:03d}", stem)
|
relative = stem.replace("\\", "/") + extension
|
||||||
path = base / f"{stem}.{format}"
|
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)
|
path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
options = {"quality": quality} if format in {"jpg", "webp"} else {}
|
pil = _image_to_pil(image)
|
||||||
_image_to_pil(image).save(path, **options)
|
if format == "jpg":
|
||||||
return {"ui": {"images": []}}
|
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):
|
def _safe_token(value):
|
||||||
|
|||||||
Reference in New Issue
Block a user