feat: PNG metadata builder (parameters + optional workflow/prompt)
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
# PNG text-chunk builder — part of ComfyUI-AI2Go-Utils. GPL-3.0.
|
||||
#
|
||||
# Always writes the A1111 `parameters` chunk (what Civitai reads). Optionally also writes ComfyUI's
|
||||
# native `prompt` + `workflow` chunks (when the node's save_workflow toggle is on) so the PNG stays
|
||||
# drag-and-drop reproducible in ComfyUI.
|
||||
import json
|
||||
|
||||
from PIL.PngImagePlugin import PngInfo
|
||||
|
||||
|
||||
def build_pnginfo(parameters, *, extra_pnginfo=None, prompt=None):
|
||||
info = PngInfo()
|
||||
info.add_text("parameters", parameters)
|
||||
if prompt is not None:
|
||||
info.add_text("prompt", json.dumps(prompt))
|
||||
if extra_pnginfo:
|
||||
for key, value in extra_pnginfo.items():
|
||||
info.add_text(key, json.dumps(value))
|
||||
return info
|
||||
@@ -0,0 +1,24 @@
|
||||
# Tests for the PNG metadata builder — part of ComfyUI-AI2Go-Utils. GPL-3.0.
|
||||
from PIL import Image
|
||||
from nodes.civitai_metadata.png_info import build_pnginfo
|
||||
|
||||
|
||||
def _roundtrip(info, tmp_path):
|
||||
p = tmp_path / "img.png"
|
||||
Image.new("RGB", (2, 2)).save(str(p), pnginfo=info)
|
||||
return Image.open(str(p)).text
|
||||
|
||||
|
||||
def test_parameters_only(tmp_path):
|
||||
text = _roundtrip(build_pnginfo("hello params"), tmp_path)
|
||||
assert text["parameters"] == "hello params"
|
||||
assert "workflow" not in text
|
||||
assert "prompt" not in text
|
||||
|
||||
|
||||
def test_with_workflow_and_prompt(tmp_path):
|
||||
info = build_pnginfo("p", extra_pnginfo={"workflow": {"nodes": []}}, prompt={"1": {"class_type": "X"}})
|
||||
text = _roundtrip(info, tmp_path)
|
||||
assert text["parameters"] == "p"
|
||||
assert '"nodes"' in text["workflow"]
|
||||
assert '"class_type"' in text["prompt"]
|
||||
Reference in New Issue
Block a user