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
+402
View File
@@ -0,0 +1,402 @@
# -*- coding: utf-8 -*-
"""o1key MJ图片生成节点(Midjourney v8.2)。"""
from __future__ import annotations
import time
from typing import List
from comfy_api.latest import io
from ..clients.mj_image_client import (
MJ_DEFAULT_TIMEOUT_SECONDS,
MJ_NODE_LABEL,
MjImageClient,
pil_images_to_output_tensor,
tensors_to_reference_images,
)
from ..utils.mj_prompt import (
MJ_ASPECT_RATIO_OPTIONS,
MJ_CHAOS_DEFAULT,
MJ_CHAOS_MAX,
MJ_CHAOS_MIN,
MJ_CONTENT_REF_MAX,
MJ_EXP_DEFAULT,
MJ_EXP_MAX,
MJ_EXP_MIN,
MJ_IW_DEFAULT,
MJ_IW_MAX,
MJ_IW_MIN,
MJ_QUALITY_DEFAULT,
MJ_QUALITY_MAX,
MJ_QUALITY_MIN,
MJ_STYLE_REF_MAX,
MJ_STYLIZE_DEFAULT,
MJ_STYLIZE_MAX,
MJ_STYLIZE_MIN,
MJ_SW_DEFAULT,
MJ_SW_MAX,
MJ_SW_MIN,
MJ_WEIRD_DEFAULT,
MJ_WEIRD_MAX,
MJ_WEIRD_MIN,
)
try:
from comfy.model_management import InterruptProcessingException, processing_interrupted
_INTERRUPT_AVAILABLE = True
except ImportError:
_INTERRUPT_AVAILABLE = False
def processing_interrupted():
return False
InterruptProcessingException = RuntimeError
try:
from comfy.utils import ProgressBar
_PROGRESS_BAR_AVAILABLE = True
except ImportError:
_PROGRESS_BAR_AVAILABLE = False
def _check_interrupt() -> None:
if _INTERRUPT_AVAILABLE and processing_interrupted():
raise InterruptProcessingException()
def _collect_autogrow_inputs(value) -> list:
"""收集已连接的 Autogrow 输入,并兼容单个旧值。"""
if value is None:
return []
if isinstance(value, dict):
return [item for item in value.values() if item is not None]
return [value]
def _collect_reference_images(
*,
group_value,
kwargs: dict,
max_count: int,
label: str,
numbered_prefix: str,
legacy_key: str,
) -> List:
"""从 Autogrow 组收集参考图;为空时兼容编号口与旧单口。"""
tensors = _collect_autogrow_inputs(group_value)
if not tensors:
tensors = [
kwargs[f"{numbered_prefix}{i}"]
for i in range(1, max_count + 1)
if kwargs.get(f"{numbered_prefix}{i}") is not None
]
if not tensors and kwargs.get(legacy_key) is not None:
tensors = [kwargs.get(legacy_key)]
images: List = []
for tensor in tensors:
part = tensors_to_reference_images(
tensor,
max_count=max_count,
label=label,
)
images.extend(part)
if len(images) > max_count:
raise ValueError(
f"{label}最多 {max_count} 张(当前输入为 {len(images)} 张)"
)
return images
class O1keyMJImage(io.ComfyNode):
"""独立 Midjourney 生图节点;非草稿固定 4 张,草稿模式接受任意数量。"""
@classmethod
def define_schema(cls):
content_references = io.Autogrow.Input(
"参考图组",
template=io.Autogrow.TemplateNames(
input=io.Image.Input("参考图"),
names=[f"参考图{i}" for i in range(1, MJ_CONTENT_REF_MAX + 1)],
min=0,
),
tooltip=(
f"内容/构图参考(对应图生图):约束主体姿势、构图与物体。"
f"接好一张后自动出现下一张;最多 {MJ_CONTENT_REF_MAX} 张。"
f"可与「风格参考图」同时使用;不传则为纯文生图。"
),
)
style_references = io.Autogrow.Input(
"风格参考图组",
template=io.Autogrow.TemplateNames(
input=io.Image.Input("风格参考图"),
names=[f"风格参考图{i}" for i in range(1, MJ_STYLE_REF_MAX + 1)],
min=0,
),
tooltip=(
f"风格/画风参考(写入 --sref):只学色彩、笔触、氛围,不强制同一构图。"
f"接好一张后自动出现下一张;最多 {MJ_STYLE_REF_MAX} 张。"
f"强度由「风格权重」控制。"
),
)
return io.Schema(
node_id="O1keyMJImage",
display_name="o1key MJ图片生成",
category="o1key/image",
inputs=[
io.String.Input(
"提示词",
default="",
multiline=True,
tooltip="画面描述。Midjourney 参数由下方控件写入 prompt;若手写同名参数会被控件值覆盖。",
),
content_references,
style_references,
io.Float.Input(
"参考图权重",
default=MJ_IW_DEFAULT,
min=MJ_IW_MIN,
max=MJ_IW_MAX,
step=0.1,
display_mode=io.NumberDisplay.number,
tooltip="写入 --iw;仅在有内容参考图时生效,越高越贴合参考结构。",
),
io.Int.Input(
"风格权重",
default=MJ_SW_DEFAULT,
min=MJ_SW_MIN,
max=MJ_SW_MAX,
step=1,
display_mode=io.NumberDisplay.number,
tooltip="写入 --sw;仅在有风格参考图时生效,范围 0–1000,默认 100。",
),
io.String.Input(
"排除内容",
default="",
multiline=False,
tooltip="有内容时写入 --no ...;控件优先,会覆盖提示词里手写的 --no。",
),
io.Combo.Input(
"画面比例",
options=list(MJ_ASPECT_RATIO_OPTIONS),
default="1:1",
tooltip="写入 --ar;控制比例,不承诺精确像素。",
),
io.Int.Input(
"渲染质量",
default=MJ_QUALITY_DEFAULT,
min=MJ_QUALITY_MIN,
max=MJ_QUALITY_MAX,
step=1,
display_mode=io.NumberDisplay.number,
tooltip="写入 --q;v8.2 支持 1–4。",
),
io.Boolean.Input(
"高清",
default=False,
label_on="开启",
label_off="关闭",
tooltip="开启后写入 --hd;长边约翻倍到 2048。",
),
io.Boolean.Input(
"草稿模式",
default=False,
label_on="开启",
label_off="关闭",
tooltip="开启后写入 --draft;更快更便宜,出图数量可能不是 4 张。",
),
io.Boolean.Input(
"原生模式",
default=False,
label_on="开启",
label_off="关闭",
tooltip="开启后写入 --raw;弱化默认美化滤镜。",
),
io.Int.Input(
"风格化",
default=MJ_STYLIZE_DEFAULT,
min=MJ_STYLIZE_MIN,
max=MJ_STYLIZE_MAX,
step=1,
display_mode=io.NumberDisplay.number,
tooltip="写入 --s;0–1000,默认 250,越高越艺术化。",
),
io.Int.Input(
"差异度",
default=MJ_CHAOS_DEFAULT,
min=MJ_CHAOS_MIN,
max=MJ_CHAOS_MAX,
step=1,
display_mode=io.NumberDisplay.number,
tooltip="写入 --c;仅当大于 0 时写入。越高四张候选差异越大。",
),
io.Int.Input(
"怪异度",
default=MJ_WEIRD_DEFAULT,
min=MJ_WEIRD_MIN,
max=MJ_WEIRD_MAX,
step=1,
display_mode=io.NumberDisplay.number,
tooltip="写入 --weird;仅当大于 0 时写入,范围 0–3000。",
),
io.Boolean.Input(
"无缝平铺纹理",
default=False,
label_on="开启",
label_off="关闭",
tooltip="开启后写入 --tile;适合壁纸、贴图等可无限拼接的无缝纹理。",
),
io.Int.Input(
"实验值",
default=MJ_EXP_DEFAULT,
min=MJ_EXP_MIN,
max=MJ_EXP_MAX,
step=1,
display_mode=io.NumberDisplay.number,
tooltip="写入 --exp;仅当大于 0 时写入,范围 0–100。",
),
io.Int.Input(
"种子",
default=-1,
min=-1,
max=2**31 - 1,
step=1,
display_mode=io.NumberDisplay.number,
tooltip="-1 表示不指定;≥0 时写入 --seed。",
),
io.Int.Input(
"超时秒数",
default=MJ_DEFAULT_TIMEOUT_SECONDS,
min=30,
max=900,
step=1,
display_mode=io.NumberDisplay.number,
tooltip="仅客户端轮询超时;单次任务通常 60–170 秒。",
),
],
outputs=[
io.Image.Output(display_name="IMAGE"),
],
)
@classmethod
def execute(
cls,
提示词: str,
画面比例: str = "1:1",
渲染质量: int = MJ_QUALITY_DEFAULT,
高清: bool = False,
参考图权重: float = MJ_IW_DEFAULT,
风格权重: int = MJ_SW_DEFAULT,
原生模式: bool = False,
风格化: int = MJ_STYLIZE_DEFAULT,
差异度: int = MJ_CHAOS_DEFAULT,
怪异度: int = MJ_WEIRD_DEFAULT,
无缝平铺纹理: bool = False,
实验值: int = MJ_EXP_DEFAULT,
草稿模式: bool = False,
排除内容: str = "",
种子: int = -1,
超时秒数: int = MJ_DEFAULT_TIMEOUT_SECONDS,
**kwargs,
) -> io.NodeOutput:
start_time = time.time()
_check_interrupt()
try:
reference_images = _collect_reference_images(
group_value=kwargs.get("参考图组"),
kwargs=kwargs,
max_count=MJ_CONTENT_REF_MAX,
label="参考图",
numbered_prefix="参考图",
legacy_key="参考图",
)
style_images = _collect_reference_images(
group_value=kwargs.get("风格参考图组"),
kwargs=kwargs,
max_count=MJ_STYLE_REF_MAX,
label="风格参考图",
numbered_prefix="风格参考图",
legacy_key="风格参考图",
)
except ValueError as exc:
raise ValueError(str(exc)) from None
progress_bar = ProgressBar(100) if _PROGRESS_BAR_AVAILABLE else None
last_pct = {"value": -1}
def _progress(fraction: float) -> None:
if progress_bar is None:
return
try:
pct = int(round(max(0.0, min(1.0, float(fraction))) * 100))
except (TypeError, ValueError):
return
if pct < last_pct["value"]:
return
last_pct["value"] = pct
progress_bar.update_absolute(pct, 100)
try:
client = MjImageClient()
except ValueError as exc:
if str(exc) == "未授权!":
print(f"[{MJ_NODE_LABEL}] 请联系作者授权后方可使用。")
raise ValueError("未授权!") from None
raise
opened = list(reference_images) + list(style_images)
try:
pil_images = client.generate_sync(
prompt=提示词,
aspect_ratio=画面比例,
quality=渲染质量,
hd=bool(高清),
image_weight=参考图权重,
raw=bool(原生模式),
stylize=风格化,
chaos=差异度,
seed=种子,
weird=怪异度,
tile=bool(无缝平铺纹理),
exp=实验值,
draft=bool(草稿模式),
no_text=排除内容 or "",
style_weight=风格权重,
images=reference_images,
style_images=style_images,
timeout_seconds=超时秒数,
check_interrupt=_check_interrupt,
progress_callback=_progress,
)
except InterruptProcessingException:
print(f"[{MJ_NODE_LABEL}] 用户取消")
raise
except Exception as exc:
message = str(exc).split("\n")[0]
print(f"[{MJ_NODE_LABEL}] 失败:{message}")
raise RuntimeError(message) from None
finally:
for image in opened:
try:
image.close()
except Exception:
pass
if progress_bar is not None:
progress_bar.update_absolute(100, 100)
output = pil_images_to_output_tensor(pil_images)
elapsed = time.time() - start_time
print(
f"[{MJ_NODE_LABEL}] 节点完成 | 耗时 {elapsed:.1f}s | "
f"输出 {output.shape[0]} 张 {output.shape[2]}x{output.shape[1]}"
)
return io.NodeOutput(output)
__all__ = ["O1keyMJImage"]