Add MJ image node, GIF save, and Kling/image generator updates

Ship o1key MJ图片生成 with content/style refs and prompt flags, save-GIF with preview, Kling 4.0 helpers, plus GPT/unified image node polish and docs.
This commit is contained in:
Jony
2026-11-09 09:11:12 +08:00
parent ff1cf830fa
commit a181cc1768
40 changed files with 6711 additions and 675 deletions
+188
View File
@@ -0,0 +1,188 @@
"""Tests for O1keySaveGIF — output save + custom-key GIF preview + passthrough."""
from __future__ import annotations
import os
import sys
import tempfile
import unittest
from io import BytesIO
from pathlib import Path
from types import ModuleType, SimpleNamespace
from unittest.mock import patch
import torch
from PIL import Image
PLUGIN_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
CUSTOM_NODES_DIR = os.path.dirname(PLUGIN_DIR)
COMFY_ROOT = os.path.dirname(CUSTOM_NODES_DIR)
for child in ("nodes", "utils", "clients"):
package = ModuleType(f"comfyui_o1key.{child}")
package.__path__ = [os.path.join(PLUGIN_DIR, child)]
sys.modules[package.__name__] = package
plugin_package = ModuleType("comfyui_o1key")
plugin_package.__path__ = [PLUGIN_DIR]
sys.modules[plugin_package.__name__] = plugin_package
model_management = ModuleType("comfy.model_management")
model_management.processing_interrupted = lambda: False
model_management.InterruptProcessingException = RuntimeError
sys.modules[model_management.__name__] = model_management
sys.path.insert(0, COMFY_ROOT)
sys.path.insert(0, CUSTOM_NODES_DIR)
def _make_gif_bytes(colors=("red", "blue", "green"), duration=100) -> bytes:
frames = [Image.new("RGB", (6, 4), color) for color in colors]
buf = BytesIO()
frames[0].save(
buf,
format="GIF",
save_all=True,
append_images=frames[1:],
duration=duration,
loop=0,
)
return buf.getvalue()
class SaveGifHelperTests(unittest.TestCase):
def test_read_and_duration_helpers(self):
from comfyui_o1key.utils.gpt_image_media import (
encode_gif_from_pil_images,
is_gif_bytes,
read_gif_bytes_from_path,
resolve_gif_frame_duration_ms,
)
self.assertEqual(resolve_gif_frame_duration_ms(fps=5.0, frame_duration_ms=0), 200)
self.assertEqual(resolve_gif_frame_duration_ms(fps=5.0, frame_duration_ms=120), 120)
data = _make_gif_bytes()
self.assertTrue(is_gif_bytes(data))
with tempfile.TemporaryDirectory() as temp_dir:
path = os.path.join(temp_dir, "demo.gif")
Path(path).write_bytes(data)
self.assertEqual(read_gif_bytes_from_path(path), data)
self.assertIsNone(read_gif_bytes_from_path(os.path.join(temp_dir, "missing.gif")))
png = os.path.join(temp_dir, "x.png")
Image.new("RGB", (2, 2), "white").save(png)
self.assertIsNone(read_gif_bytes_from_path(png))
frames = [Image.new("RGB", (4, 4), c) for c in ("red", "blue")]
encoded = encode_gif_from_pil_images(frames, duration_ms=80)
self.assertTrue(is_gif_bytes(encoded))
class SaveGifNodeTests(unittest.TestCase):
def test_passthrough_writes_output_and_preview_payload(self):
from comfyui_o1key.nodes.save_gif import O1keySaveGIF, SavedGifPreview
gif_bytes = _make_gif_bytes()
with tempfile.TemporaryDirectory() as source_dir, tempfile.TemporaryDirectory() as output_dir:
source = os.path.join(source_dir, "provider.gif")
Path(source).write_bytes(gif_bytes)
def get_save_image_path(prefix, root, _w, _h):
return root, prefix, 1, "", prefix
folder = SimpleNamespace(
get_output_directory=lambda: output_dir,
get_save_image_path=get_save_image_path,
)
with patch("comfyui_o1key.nodes.save_gif.folder_paths", folder):
result = O1keySaveGIF.execute(文件路径=source, 文件名前缀="ComfyUI")
target = result.args[0]
ui_obj = result.ui
self.assertTrue(str(target).lower().endswith(".gif"))
self.assertTrue(Path(target).is_file())
self.assertEqual(Path(target).read_bytes(), gif_bytes)
self.assertTrue(str(target).startswith(output_dir))
self.assertIsInstance(ui_obj, SavedGifPreview)
data = ui_obj.as_dict()
# Custom key only — core must NOT see ``images`` or it will mount
# $$canvas-image-preview (static frame + "W x H") under our <img>.
self.assertNotIn("images", data)
self.assertNotIn("animated", data)
self.assertIn(SavedGifPreview.UI_KEY, data)
entry = data[SavedGifPreview.UI_KEY][0]
self.assertEqual(entry["type"], "output")
self.assertTrue(str(entry["filename"]).lower().endswith(".gif"))
self.assertEqual(entry["filename"], Path(target).name)
self.assertEqual(entry["subfolder"], "")
def test_image_metadata_passthrough_avoids_reencode(self):
from comfyui_o1key.nodes.save_gif import O1keySaveGIF, SavedGifPreview
from comfyui_o1key.utils.image_utils import pil_to_tensor
from comfyui_o1key.utils.gpt_image_media import expand_gif_frames
gif_bytes = _make_gif_bytes()
with Image.open(BytesIO(gif_bytes)) as opened:
frames = expand_gif_frames(opened, gif_bytes)
tensor = pil_to_tensor(frames)
with tempfile.TemporaryDirectory() as output_dir:
def get_save_image_path(prefix, root, _w, _h):
return root, prefix, 7, "", prefix
folder = SimpleNamespace(
get_output_directory=lambda: output_dir,
get_save_image_path=get_save_image_path,
)
with patch("comfyui_o1key.nodes.save_gif.folder_paths", folder):
result = O1keySaveGIF.execute(图像=tensor, fps=5.0, 文件名前缀="meta")
target = result.args[0]
self.assertEqual(Path(target).read_bytes(), gif_bytes)
data = result.ui.as_dict()
self.assertNotIn("images", data)
self.assertNotIn("animated", data)
self.assertTrue(
str(data[SavedGifPreview.UI_KEY][0]["filename"]).lower().endswith(".gif")
)
def test_image_batch_fallback_encodes_gif(self):
from comfyui_o1key.nodes.save_gif import O1keySaveGIF, SavedGifPreview
from comfyui_o1key.utils.image_utils import pil_to_tensor
from comfyui_o1key.utils.gpt_image_media import is_gif_bytes
frames = [Image.new("RGB", (5, 3), c) for c in ("red", "green", "blue")]
tensor = pil_to_tensor(frames)
with tempfile.TemporaryDirectory() as output_dir:
def get_save_image_path(prefix, root, _w, _h):
return root, prefix, 3, "", prefix
folder = SimpleNamespace(
get_output_directory=lambda: output_dir,
get_save_image_path=get_save_image_path,
)
with patch("comfyui_o1key.nodes.save_gif.folder_paths", folder):
result = O1keySaveGIF.execute(图像=tensor, fps=10.0, 文件名前缀="anim")
target = result.args[0]
ui_obj = result.ui
data = Path(target).read_bytes()
self.assertTrue(is_gif_bytes(data))
payload = ui_obj.as_dict()
self.assertNotIn("images", payload)
self.assertNotIn("animated", payload)
self.assertIn(SavedGifPreview.UI_KEY, payload)
self.assertIsInstance(tensor, torch.Tensor)
def test_schema_registers_chinese_labels(self):
from comfyui_o1key.nodes.save_gif import O1keySaveGIF
schema = O1keySaveGIF.define_schema()
schema.validate()
self.assertEqual(schema.node_id, "O1keySaveGIF")
self.assertEqual(schema.display_name, "保存 GIF")
ids = [item.id for item in schema.inputs]
self.assertIn("文件路径", ids)
self.assertIn("图像", ids)
self.assertIn("文件名前缀", ids)
self.assertTrue(schema.is_output_node)
if __name__ == "__main__":
unittest.main()