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
+113 -14
View File
@@ -1,6 +1,6 @@
"""
o1key GPT Image 节点
支持 GPT Image 2 / 2.5 系列的文生图、图生图和带蒙版图像编辑
支持 GPT Image 2 / 2.5 / 2.6 系列的文生图、图生图和带蒙版图像编辑
"""
import os
@@ -20,12 +20,29 @@ from ..utils.image_utils import parse_batch_prompts, pil_to_tensor, tensor_to_pi
from ..utils.config import get_base_url_by_route
from ..utils.o1key_image_catalog import (
GPT_IMAGE_BACKGROUND_OPTIONS,
GPT_IMAGE_COMPLEXITY_OPTIONS,
GPT_IMAGE_26_OUTPUT_FORMAT_OPTIONS,
GPT_IMAGE_GIF_FRAME_COUNT_DEFAULT,
GPT_IMAGE_GIF_FRAME_COUNT_MAX,
GPT_IMAGE_GIF_FRAME_COUNT_MIN,
GPT_IMAGE_GIF_FRAME_DURATION_DEFAULT,
GPT_IMAGE_GIF_FRAME_DURATION_MAX,
GPT_IMAGE_GIF_FRAME_DURATION_MIN,
resolve_gpt_image_complexity,
resolve_gpt_image_frame_count,
resolve_gpt_image_frame_duration_ms,
resolve_gpt_image_output_format,
validate_gpt_image_vector_output,
GPT_IMAGE_EXACT_SIZE_OPTIONS,
GPT_IMAGE_OUTPUT_FORMAT_OPTIONS,
GPT_IMAGE_25_QUALITY_OPTIONS,
resolve_gpt_image_quality,
resolve_gpt_image_size,
)
from ..utils.gpt_image_media import (
animation_bytes_from_images,
svg_bytes_from_image,
write_bytes_atomic,
)
from ..utils.file_utils import (
ImageInfo,
generate_timestamp_filename,
@@ -142,13 +159,17 @@ class O1keyGPTImage(io.ComfyNode):
io.Combo.Input(
"模型",
options=GPT_IMAGE_MODEL_OPTIONS,
default="gpt-image-2.5-sunburst",
default="gpt-image-2.6-sunburst",
),
io.Combo.Input(
"模型线路",
options=GPT_IMAGE_ROUTE_OPTIONS,
default="畅速",
),
io.Combo.Input(
"复杂度", options=GPT_IMAGE_COMPLEXITY_OPTIONS, default="高",
tooltip="仅 GPT Image 2.6 生效:低 / 中 / 高;越高规划越充分、耗时越长。",
),
io.Combo.Input(
"分辨率",
options=GPT_IMAGE_EXACT_SIZE_OPTIONS,
@@ -168,13 +189,13 @@ class O1keyGPTImage(io.ComfyNode):
"质量",
options=GPT_IMAGE_25_QUALITY_OPTIONS,
default="自动",
tooltip="GPT Image 2 支持高/中/低/自动;GPT Image 2.5 另支持超高=xhigh、最高=max。",
tooltip="GPT Image 2 支持高/中/低/自动;GPT Image 2.5 / 2.6 另支持超高=xhigh、最高=max。",
),
io.Combo.Input(
"输出格式",
options=["png", "jpeg", "webp"],
options=GPT_IMAGE_26_OUTPUT_FORMAT_OPTIONS,
default="png",
tooltip="Generated image output format",
tooltip="2.6 另支持 svg / gif;svg/gif 仅纯文生图可用。",
),
io.Combo.Input(
"背景",
@@ -204,8 +225,29 @@ class O1keyGPTImage(io.ComfyNode):
control_after_generate=io.ControlAfterGenerate.randomize,
tooltip="Random seed (0 = not specified)",
),
io.Int.Input(
"帧数",
default=GPT_IMAGE_GIF_FRAME_COUNT_DEFAULT,
min=GPT_IMAGE_GIF_FRAME_COUNT_MIN,
max=GPT_IMAGE_GIF_FRAME_COUNT_MAX,
step=1,
display_mode=io.NumberDisplay.number,
tooltip="仅输出格式为 gif 时生效:2–64,默认 9。",
),
io.Int.Input(
"帧时长ms",
default=GPT_IMAGE_GIF_FRAME_DURATION_DEFAULT,
min=GPT_IMAGE_GIF_FRAME_DURATION_MIN,
max=GPT_IMAGE_GIF_FRAME_DURATION_MAX,
step=10,
display_mode=io.NumberDisplay.number,
tooltip="仅输出格式为 gif 时生效:20–2000ms,默认 200,按 10ms 取整。",
),
],
outputs=[
io.Image.Output(display_name="IMAGE"),
io.String.Output("FILE", display_name="文件路径"),
],
outputs=[io.Image.Output(display_name="IMAGE")],
# 兼容 Autogrow 改造前保存的参考图1~参考图9端口。
accept_all_inputs=True,
)
@@ -214,7 +256,7 @@ class O1keyGPTImage(io.ComfyNode):
def execute(
cls,
prompt: str,
模型: str = "gpt-image-2.5-sunburst",
模型: str = "gpt-image-2.6-sunburst",
模型线路: str = "畅速",
分辨率: str = "智能",
质量: str = "自动",
@@ -224,6 +266,9 @@ class O1keyGPTImage(io.ComfyNode):
遮罩=None,
缩放图片: str = "智能缩放",
背景: str = "auto",
复杂度: str = "高",
帧数: int = GPT_IMAGE_GIF_FRAME_COUNT_DEFAULT,
帧时长ms: int = GPT_IMAGE_GIF_FRAME_DURATION_DEFAULT,
**kwargs,
) -> io.NodeOutput:
result = cls.generate(
@@ -238,15 +283,21 @@ class O1keyGPTImage(io.ComfyNode):
遮罩=遮罩,
缩放图片=缩放图片,
背景=背景,
复杂度=复杂度,
帧数=帧数,
帧时长ms=帧时长ms,
**kwargs,
)
return io.NodeOutput(*result)
tensor, file_path, preview = result
if preview is not None:
return io.NodeOutput(tensor, file_path, ui=preview)
return io.NodeOutput(tensor, file_path)
@classmethod
def generate(
cls,
prompt: str,
模型: str = "gpt-image-2.5-sunburst",
模型: str = "gpt-image-2.6-sunburst",
模型线路: str = "畅速",
分辨率: str = "智能",
质量: str = "自动",
@@ -256,6 +307,9 @@ class O1keyGPTImage(io.ComfyNode):
遮罩=None,
缩放图片: str = "智能缩放",
背景: str = "auto",
复杂度: str = "高",
帧数: int = GPT_IMAGE_GIF_FRAME_COUNT_DEFAULT,
帧时长ms: int = GPT_IMAGE_GIF_FRAME_DURATION_DEFAULT,
**kwargs,
):
"""
@@ -285,12 +339,19 @@ class O1keyGPTImage(io.ComfyNode):
raise ValueError("提供了遮罩但未提供图片,请同时提供图片和遮罩")
if 缩放图片 not in {"不缩放", "智能缩放"}:
raise ValueError("缩放图片参数无效")
if 输出格式 not in GPT_IMAGE_OUTPUT_FORMAT_OPTIONS:
raise ValueError("GPT Image 输出格式无效")
if 背景 not in GPT_IMAGE_BACKGROUND_OPTIONS:
raise ValueError("GPT Image 背景参数无效")
if 背景 == "transparent" and 输出格式 == "jpeg":
输出格式 = resolve_gpt_image_output_format(模型, 输出格式)
if 背景 == "transparent" and 输出格式 not in {"png", "webp"}:
raise ValueError("GPT Image 透明背景仅支持 PNG 或 WebP 输出格式")
validate_gpt_image_vector_output(
模型,
输出格式,
has_images=bool(图片),
has_mask=遮罩 is not None,
)
frame_count = resolve_gpt_image_frame_count(输出格式, 帧数)
frame_duration_ms = resolve_gpt_image_frame_duration_ms(输出格式, 帧时长ms)
# ── 2. 解析分辨率显示值 → API 参数值 ──────────────────────────────────
size = _resolve_async_size(分辨率)
@@ -300,6 +361,7 @@ class O1keyGPTImage(io.ComfyNode):
# ── 2c. 解析质量显示值 → API 参数值 ───────────────────────────────────
quality = resolve_gpt_image_quality(模型, 质量)
complexity = resolve_gpt_image_complexity(模型, 复杂度)
# ── 3. 创建客户端 ─────────────────────────────────────────────────────
try:
@@ -348,6 +410,9 @@ class O1keyGPTImage(io.ComfyNode):
mask_tensor=遮罩,
output_format=输出格式,
background=背景,
**({"complexity": complexity} if complexity is not None else {}),
**({"frame_count": frame_count} if frame_count is not None else {}),
**({"frame_duration_ms": frame_duration_ms} if frame_duration_ms is not None else {}),
progress_callback=_make_node_progress_callback(progress_bar, idx, total),
special_price_parallel=True,
task_submitted_callback=_submitted,
@@ -381,6 +446,9 @@ class O1keyGPTImage(io.ComfyNode):
mask_tensor=遮罩,
output_format=输出格式,
background=背景,
**({"complexity": complexity} if complexity is not None else {}),
**({"frame_count": frame_count} if frame_count is not None else {}),
**({"frame_duration_ms": frame_duration_ms} if frame_duration_ms is not None else {}),
progress_callback=_make_node_progress_callback(progress_bar, 1, 1),
special_price_parallel=True,
task_submitted_callback=_submitted,
@@ -402,6 +470,37 @@ class O1keyGPTImage(io.ComfyNode):
raise RuntimeError("所有提示词均生成失败,无可用图像输出")
# ── 7. PIL → tensor ───────────────────────────────────────────────
file_path = ""
preview = None
anim = animation_bytes_from_images(all_pil_images)
svg = svg_bytes_from_image(all_pil_images[0]) if all_pil_images else None
if _FOLDER_PATHS_AVAILABLE and (anim is not None or svg is not None):
media_root = (
folder_paths.get_temp_directory()
if anim is not None
else folder_paths.get_output_directory()
)
os.makedirs(media_root, exist_ok=True)
target = generate_timestamp_filename(
output_folder=media_root,
prefix="gpt_image",
extension=".gif" if anim is not None else ".svg",
)
if anim is not None:
# Durable copy lives in output via the dedicated「保存 GIF」node.
# Temp here only backs the「文件路径」output — no in-node preview
# (user request: gpt image itself should not preview GIFs).
write_bytes_atomic(target, anim)
file_path = target
preview = None
print(
f"[o1key GPT Image] GIF 已写入 temp(持久保存请接「保存 GIF」): {file_path}"
)
else:
write_bytes_atomic(target, svg)
file_path = target
print(f"[o1key GPT Image] SVG 已保存: {file_path}")
output_tensor = GptImageClient._pil_list_to_tensor(all_pil_images)
# ── 8. 完成日志 ───────────────────────────────────────────────────
@@ -412,7 +511,7 @@ class O1keyGPTImage(io.ComfyNode):
f"{output_tensor.shape[2]}×{output_tensor.shape[1]}"
)
return (output_tensor,)
return (output_tensor, file_path, preview)
finally:
cls._print_balance(client)