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:
@@ -24,7 +24,8 @@ from PIL.PngImagePlugin import PngInfo
|
||||
SAVE_FORMAT_OPTIONS = ("原始", "png", "jpg", "webp")
|
||||
SAVE_NAMING_RULE_OPTIONS = ("和主图一致", "自然数字", "自定义前缀")
|
||||
DEFAULT_SAVE_NAMING_RULE = "自定义前缀"
|
||||
_FORMAT_EXTENSIONS = {"PNG": "png", "JPEG": "jpg", "WEBP": "webp"}
|
||||
_FORMAT_EXTENSIONS = {"PNG": "png", "JPEG": "jpg", "WEBP": "webp", "GIF": "gif"}
|
||||
_TEMP_RESULT_FORMATS = frozenset(_FORMAT_EXTENSIONS) # GIF kept as original bytes when 原始
|
||||
_PNG_SIGNATURE = b"\x89PNG\r\n\x1a\n"
|
||||
_SAVE_LOCK = threading.Lock()
|
||||
_UNSAFE_FILENAME_CHARS = re.compile(r'[<>:"/\\|?*\x00-\x1f]')
|
||||
@@ -107,6 +108,11 @@ def detect_image_format(data: bytes) -> str | None:
|
||||
return "JPEG"
|
||||
if len(data) >= 12 and data[:4] == b"RIFF" and data[8:12] == b"WEBP":
|
||||
return "WEBP"
|
||||
if data.startswith(b"GIF87a") or data.startswith(b"GIF89a"):
|
||||
return "GIF"
|
||||
head = data.lstrip()[:256].lower() if data else b""
|
||||
if head.startswith(b"<svg") or (head.startswith(b"<?xml") and b"<svg" in head):
|
||||
return "SVG"
|
||||
return None
|
||||
|
||||
|
||||
@@ -458,12 +464,20 @@ def _save_sources(
|
||||
"jpg": "JPEG",
|
||||
"webp": "WEBP",
|
||||
}[selected]
|
||||
# Never re-encode GIF into itself via Pillow; keep provider bytes.
|
||||
if selected == "原始" and source_format == "GIF" and isinstance(raw, bytes) and detect_image_format(raw) == "GIF":
|
||||
target_format = "GIF"
|
||||
if target_format not in _FORMAT_EXTENSIONS:
|
||||
target_format = "PNG"
|
||||
if target_format == "JPEG" and _has_workflow_metadata(extra_pnginfo):
|
||||
target_format = "PNG"
|
||||
# GIF has no workflow metadata container in this pack; keep bytes intact.
|
||||
if target_format == "GIF" and (not isinstance(raw, bytes) or detect_image_format(raw) != "GIF"):
|
||||
raise ValueError("GIF 结果缺少原始字节,无法原样保存")
|
||||
extension = _FORMAT_EXTENSIONS[target_format]
|
||||
if selected == "原始" and raw is not None and detect_image_format(raw) == target_format:
|
||||
if target_format == "GIF":
|
||||
encoded = raw
|
||||
elif selected == "原始" and raw is not None and detect_image_format(raw) == target_format:
|
||||
encoded = inject_workflow_metadata(
|
||||
raw,
|
||||
target_format,
|
||||
@@ -502,11 +516,13 @@ def _save_sources(
|
||||
except FileExistsError:
|
||||
continue
|
||||
if external_location:
|
||||
results.append(
|
||||
_external_preview_descriptor(encoded, saved_name, folder_paths_module)
|
||||
)
|
||||
descriptor = _external_preview_descriptor(encoded, saved_name, folder_paths_module)
|
||||
else:
|
||||
results.append({"filename": saved_name, "subfolder": subfolder, "type": "output"})
|
||||
descriptor = {"filename": saved_name, "subfolder": subfolder, "type": "output"}
|
||||
if target_format == "GIF" or saved_name.lower().endswith(".gif"):
|
||||
descriptor["media"] = "gif"
|
||||
descriptor["animated"] = True
|
||||
results.append(descriptor)
|
||||
return results
|
||||
|
||||
|
||||
@@ -564,6 +580,7 @@ def save_temp_images(
|
||||
image_format = detect_image_format(raw)
|
||||
if image_format not in _FORMAT_EXTENSIONS:
|
||||
raise ValueError(f"临时结果不是受支持的图像:{os.path.basename(source_path)}")
|
||||
# GIF is accepted and preserved byte-for-byte when save format is 原始.
|
||||
with Image.open(BytesIO(raw)) as opened:
|
||||
opened.load()
|
||||
image = opened.copy()
|
||||
|
||||
Reference in New Issue
Block a user