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:
+114
-30
@@ -21,6 +21,11 @@ from .o1key_image_catalog import (
|
||||
BANANA_RESOLUTION_OPTIONS,
|
||||
GPT_IMAGE_ASPECT_RATIO_OPTIONS,
|
||||
GPT_IMAGE_BACKGROUND_OPTIONS,
|
||||
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_COUNTS,
|
||||
@@ -44,11 +49,13 @@ from .o1key_image_catalog import (
|
||||
)
|
||||
from .http_error import format_o1key_image_error
|
||||
from .image_utils import (
|
||||
IMAGE_BATCH_MODE_ALIASES,
|
||||
IMAGE_BATCH_MODE_GROUP_TO_MODELS,
|
||||
IMAGE_BATCH_MODE_SINGLE_REFERENCES,
|
||||
IMAGE_BATCH_MODES,
|
||||
expand_image_generation_tasks,
|
||||
)
|
||||
from .gpt_image_media import animation_bytes_from_images, svg_bytes_from_image, write_bytes_atomic
|
||||
from .o1key_image_save import (
|
||||
DEFAULT_SAVE_NAMING_RULE,
|
||||
detect_image_format,
|
||||
@@ -341,7 +348,7 @@ def normalize_job_payload(payload: Any) -> dict[str, Any]:
|
||||
payload.get("resolution") or UNIFIED_IMAGE_SMART_RESOLUTION
|
||||
).strip()
|
||||
aspect_ratio = str(payload.get("aspect_ratio") or "智能").strip()
|
||||
thinking = str(payload.get("thinking_level") or "低").strip()
|
||||
thinking = str(payload.get("thinking_level") or "高").strip()
|
||||
if model not in MODELS:
|
||||
raise ValueError("模型无效")
|
||||
if model_route not in MODEL_ROUTES:
|
||||
@@ -349,7 +356,7 @@ def normalize_job_payload(payload: Any) -> dict[str, Any]:
|
||||
google_search = model in {"Nano Banana 2.1", "Nano Banana 2"} and payload.get("google_search") is True
|
||||
quality = str(payload.get("quality") or "自动").strip()
|
||||
resize_mode = str(
|
||||
payload.get("resize_mode") or ("智能缩放" if is_gpt_image_model(model) else "不缩放")
|
||||
payload.get("resize_mode") or "智能缩放"
|
||||
).strip()
|
||||
if resize_mode not in {"不缩放", "智能缩放"}:
|
||||
raise ValueError("缩放图片参数无效")
|
||||
@@ -366,13 +373,17 @@ def normalize_job_payload(payload: Any) -> dict[str, Any]:
|
||||
raise ValueError("GPT Image 分辨率无效")
|
||||
if aspect_ratio not in GPT_IMAGE_ASPECT_RATIO_OPTIONS:
|
||||
raise ValueError("GPT Image 宽高比无效")
|
||||
complexity = resolve_gpt_image_complexity(model, payload.get("complexity"))
|
||||
resolve_gpt_image_quality(model, quality)
|
||||
if output_format not in GPT_IMAGE_OUTPUT_FORMAT_OPTIONS:
|
||||
raise ValueError("GPT Image 输出格式无效")
|
||||
output_format = resolve_gpt_image_output_format(model, output_format)
|
||||
if background not in GPT_IMAGE_BACKGROUND_OPTIONS:
|
||||
raise ValueError("GPT Image 背景参数无效")
|
||||
if background == "transparent" and output_format == "jpeg":
|
||||
if background == "transparent" and output_format not in {"png", "webp"}:
|
||||
raise ValueError("GPT Image 透明背景仅支持 PNG 或 WebP 输出格式")
|
||||
frame_count = resolve_gpt_image_frame_count(output_format, payload.get("frame_count"))
|
||||
frame_duration_ms = resolve_gpt_image_frame_duration_ms(
|
||||
output_format, payload.get("frame_duration_ms")
|
||||
)
|
||||
elif is_seedream_model(model):
|
||||
output_format = str(
|
||||
payload.get("output_format") or ("png" if layer_decomposition else "jpeg")
|
||||
@@ -437,6 +448,8 @@ def normalize_job_payload(payload: Any) -> dict[str, Any]:
|
||||
batch_mode = str(
|
||||
payload.get("batch_mode") or IMAGE_BATCH_MODE_GROUP_TO_MODELS
|
||||
).strip()
|
||||
# Retired labels are still accepted so already-queued jobs keep their pairing.
|
||||
batch_mode = IMAGE_BATCH_MODE_ALIASES.get(batch_mode, batch_mode)
|
||||
if batch_enabled and batch_mode not in IMAGE_BATCH_MODES:
|
||||
raise ValueError("批量模式无效")
|
||||
if not batch_enabled:
|
||||
@@ -558,8 +571,20 @@ def normalize_job_payload(payload: Any) -> dict[str, Any]:
|
||||
"layer_decomposition": layer_decomposition,
|
||||
}
|
||||
if is_gpt_image_model(model):
|
||||
validate_gpt_image_vector_output(
|
||||
model,
|
||||
output_format,
|
||||
has_images=bool(safe_references) or bool(safe_model_references),
|
||||
has_mask=bool(safe_mask),
|
||||
)
|
||||
job["output_format"] = output_format
|
||||
job["background"] = background
|
||||
if complexity is not None:
|
||||
job["complexity"] = complexity
|
||||
if frame_count is not None:
|
||||
job["frame_count"] = frame_count
|
||||
if frame_duration_ms is not None:
|
||||
job["frame_duration_ms"] = frame_duration_ms
|
||||
elif is_seedream_model(model):
|
||||
job["output_format"] = output_format
|
||||
elif google_search:
|
||||
@@ -762,8 +787,13 @@ def _provider_result_format(image: Image.Image) -> str:
|
||||
detected = detect_image_format(raw) if isinstance(raw, bytes) else None
|
||||
if detected == "JPEG":
|
||||
return "jpg"
|
||||
if detected in {"PNG", "WEBP"}:
|
||||
if detected in {"PNG", "WEBP", "GIF", "SVG"}:
|
||||
return detected.lower()
|
||||
hinted = str(getattr(image, "_o1key_original_format", "") or getattr(image, "format", "") or "").upper()
|
||||
if hinted == "JPEG":
|
||||
return "jpg"
|
||||
if hinted in {"PNG", "WEBP", "GIF", "SVG"}:
|
||||
return hinted.lower()
|
||||
return "png"
|
||||
|
||||
|
||||
@@ -773,7 +803,7 @@ def _write_provider_result(image: Image.Image, output_path: str) -> None:
|
||||
detected = detect_image_format(raw) if isinstance(raw, bytes) else None
|
||||
temporary_path = f"{output_path}.tmp"
|
||||
try:
|
||||
if detected in {"PNG", "JPEG", "WEBP"}:
|
||||
if detected in {"PNG", "JPEG", "WEBP", "GIF", "SVG"}:
|
||||
with open(temporary_path, "wb") as handle:
|
||||
handle.write(raw)
|
||||
else:
|
||||
@@ -1066,6 +1096,7 @@ async def _execute_gpt_image_job(
|
||||
|
||||
actual_model = resolve_gpt_image_model(job["model"], job["model_route"])
|
||||
quality = resolve_gpt_image_quality(job["model"], job["quality"])
|
||||
complexity = resolve_gpt_image_complexity(job["model"], job.get("complexity"))
|
||||
client = GptImageClient()
|
||||
client.base_url = get_base_url_by_route()
|
||||
client.response_log_enabled = False
|
||||
@@ -1170,6 +1201,11 @@ async def _execute_gpt_image_job(
|
||||
started_at = time.monotonic()
|
||||
heartbeat = asyncio.create_task(_heartbeat())
|
||||
try:
|
||||
frame_kwargs = {}
|
||||
if job.get("frame_count") is not None:
|
||||
frame_kwargs["frame_count"] = job["frame_count"]
|
||||
if job.get("frame_duration_ms") is not None:
|
||||
frame_kwargs["frame_duration_ms"] = job["frame_duration_ms"]
|
||||
images = await client.generate_image_async(
|
||||
prompt=job["task_prompts"][request_index],
|
||||
model=actual_model,
|
||||
@@ -1187,6 +1223,8 @@ async def _execute_gpt_image_job(
|
||||
mask_tensor=mask_tensor,
|
||||
output_format=job["output_format"],
|
||||
background=job["background"],
|
||||
**({"complexity": complexity} if complexity is not None else {}),
|
||||
**frame_kwargs,
|
||||
resize_mode=job["resize_mode"],
|
||||
progress_callback=lambda value: _update_request_progress(
|
||||
request_index,
|
||||
@@ -1209,30 +1247,76 @@ async def _execute_gpt_image_job(
|
||||
if not images:
|
||||
raise RuntimeError(f"第 {request_index + 1} 次请求没有返回图片")
|
||||
_update_request_progress(request_index, 0.96)
|
||||
selected = images[0]
|
||||
filename = _result_filename(
|
||||
job,
|
||||
request_index,
|
||||
0,
|
||||
_provider_result_format(selected),
|
||||
)
|
||||
target = os.path.join(output_path, filename)
|
||||
await asyncio.to_thread(
|
||||
_write_provider_result,
|
||||
selected,
|
||||
target,
|
||||
)
|
||||
descriptor = {
|
||||
"filename": filename,
|
||||
"subfolder": relative_subfolder,
|
||||
"type": "temp",
|
||||
"batch_id": job["batch_id"],
|
||||
"request_index": request_index + 1,
|
||||
"result_index": 1,
|
||||
}
|
||||
_publish_partial_images(progress_callback, [descriptor])
|
||||
descriptors: list[dict[str, Any]] = []
|
||||
anim = animation_bytes_from_images(images)
|
||||
svg = svg_bytes_from_image(images[0])
|
||||
if anim is not None:
|
||||
filename = _result_filename(job, request_index, 0, "gif")
|
||||
target = os.path.join(output_path, filename)
|
||||
await asyncio.to_thread(write_bytes_atomic, target, anim)
|
||||
descriptors.append({
|
||||
"filename": filename,
|
||||
"subfolder": relative_subfolder,
|
||||
"type": "temp",
|
||||
"batch_id": job["batch_id"],
|
||||
"request_index": request_index + 1,
|
||||
"result_index": 1,
|
||||
"media": "gif",
|
||||
})
|
||||
elif svg is not None:
|
||||
svg_name = _result_filename(job, request_index, 0, "svg")
|
||||
svg_target = os.path.join(output_path, svg_name)
|
||||
await asyncio.to_thread(write_bytes_atomic, svg_target, svg)
|
||||
# Raster preview for Comfy IMAGE / native preview pipelines.
|
||||
preview = images[0]
|
||||
# Ensure preview file is PNG bytes, not SVG container.
|
||||
from io import BytesIO
|
||||
buf = BytesIO()
|
||||
preview.convert("RGBA").save(buf, format="PNG", compress_level=4)
|
||||
png_bytes = buf.getvalue()
|
||||
png_name = _result_filename(job, request_index, 1, "png")
|
||||
png_target = os.path.join(output_path, png_name)
|
||||
await asyncio.to_thread(write_bytes_atomic, png_target, png_bytes)
|
||||
descriptors.append({
|
||||
"filename": png_name,
|
||||
"subfolder": relative_subfolder,
|
||||
"type": "temp",
|
||||
"batch_id": job["batch_id"],
|
||||
"request_index": request_index + 1,
|
||||
"result_index": 1,
|
||||
"media": "image",
|
||||
"svg_filename": svg_name,
|
||||
})
|
||||
descriptors.append({
|
||||
"filename": svg_name,
|
||||
"subfolder": relative_subfolder,
|
||||
"type": "temp",
|
||||
"batch_id": job["batch_id"],
|
||||
"request_index": request_index + 1,
|
||||
"result_index": 2,
|
||||
"media": "svg",
|
||||
})
|
||||
else:
|
||||
selected = images[0]
|
||||
filename = _result_filename(
|
||||
job,
|
||||
request_index,
|
||||
0,
|
||||
_provider_result_format(selected),
|
||||
)
|
||||
target = os.path.join(output_path, filename)
|
||||
await asyncio.to_thread(_write_provider_result, selected, target)
|
||||
descriptors.append({
|
||||
"filename": filename,
|
||||
"subfolder": relative_subfolder,
|
||||
"type": "temp",
|
||||
"batch_id": job["batch_id"],
|
||||
"request_index": request_index + 1,
|
||||
"result_index": 1,
|
||||
})
|
||||
_publish_partial_images(progress_callback, descriptors)
|
||||
_update_request_progress(request_index, 1.0)
|
||||
return [descriptor]
|
||||
return descriptors
|
||||
finally:
|
||||
for image in images:
|
||||
image.close()
|
||||
|
||||
Reference in New Issue
Block a user