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:
@@ -0,0 +1,264 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""Midjourney prompt helpers for o1key MJ image generation.
|
||||
|
||||
Controlled flags are appended in a stable order. Duplicate user-written copies
|
||||
of the same flags are stripped first so widget values win.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from typing import Optional, Sequence
|
||||
|
||||
MJ_MODEL_ID = "mj-v8.2"
|
||||
|
||||
MJ_ASPECT_RATIO_OPTIONS = [
|
||||
"1:1",
|
||||
"16:9",
|
||||
"9:16",
|
||||
"4:5",
|
||||
"3:2",
|
||||
"2:3",
|
||||
"4:3",
|
||||
"21:9",
|
||||
]
|
||||
|
||||
MJ_QUALITY_MIN = 1
|
||||
MJ_QUALITY_MAX = 4
|
||||
MJ_QUALITY_DEFAULT = 2
|
||||
|
||||
MJ_STYLIZE_MIN = 0
|
||||
MJ_STYLIZE_MAX = 1000
|
||||
MJ_STYLIZE_DEFAULT = 250
|
||||
|
||||
MJ_CHAOS_MIN = 0
|
||||
MJ_CHAOS_MAX = 100
|
||||
MJ_CHAOS_DEFAULT = 0
|
||||
|
||||
MJ_IW_MIN = 0.0
|
||||
MJ_IW_MAX = 3.0
|
||||
MJ_IW_DEFAULT = 1.0
|
||||
|
||||
MJ_WEIRD_MIN = 0
|
||||
MJ_WEIRD_MAX = 3000
|
||||
MJ_WEIRD_DEFAULT = 0
|
||||
|
||||
MJ_EXP_MIN = 0
|
||||
MJ_EXP_MAX = 100
|
||||
MJ_EXP_DEFAULT = 0
|
||||
|
||||
MJ_SW_MIN = 0
|
||||
MJ_SW_MAX = 1000
|
||||
MJ_SW_DEFAULT = 100
|
||||
|
||||
MJ_CONTENT_REF_MAX = 4
|
||||
MJ_STYLE_REF_MAX = 4
|
||||
|
||||
# Strip both short and long Midjourney spellings before we append ours.
|
||||
_STRIP_PATTERNS = (
|
||||
re.compile(r"(?i)(?:^|\s)--ar(?:atio)?\s+\S+"),
|
||||
re.compile(r"(?i)(?:^|\s)--q(?:uality)?\s+\S+"),
|
||||
re.compile(r"(?i)(?:^|\s)--hd\b"),
|
||||
re.compile(r"(?i)(?:^|\s)--iw\s+\S+"),
|
||||
re.compile(r"(?i)(?:^|\s)--raw\b"),
|
||||
re.compile(r"(?i)(?:^|\s)--s(?:tylize)?\s+\S+"),
|
||||
re.compile(r"(?i)(?:^|\s)--c(?:haos)?\s+\S+"),
|
||||
re.compile(r"(?i)(?:^|\s)--seed\s+\S+"),
|
||||
re.compile(r"(?i)(?:^|\s)--weird\s+\S+"),
|
||||
re.compile(r"(?i)(?:^|\s)--tile\b"),
|
||||
re.compile(r"(?i)(?:^|\s)--exp\s+\S+"),
|
||||
re.compile(r"(?i)(?:^|\s)--draft\b"),
|
||||
re.compile(r"(?i)(?:^|\s)--no(?:\s+(?!--)\S+)+"),
|
||||
re.compile(r"(?i)(?:^|\s)--sref(?:\s+(?!--)\S+)+"),
|
||||
re.compile(r"(?i)(?:^|\s)--sw\s+\S+"),
|
||||
)
|
||||
|
||||
_SPACE_RE = re.compile(r"[ \t]{2,}")
|
||||
|
||||
|
||||
def strip_controlled_mj_flags(prompt: str) -> str:
|
||||
"""Remove controlled Midjourney flags from free-form prompt text."""
|
||||
text = str(prompt or "")
|
||||
for pattern in _STRIP_PATTERNS:
|
||||
text = pattern.sub(" ", text)
|
||||
text = _SPACE_RE.sub(" ", text)
|
||||
return text.strip()
|
||||
|
||||
|
||||
def _format_iw(value: float) -> str:
|
||||
number = float(value)
|
||||
if number.is_integer():
|
||||
return str(int(number))
|
||||
text = f"{number:.3f}".rstrip("0").rstrip(".")
|
||||
return text or "0"
|
||||
|
||||
|
||||
def _normalize_https_urls(urls: Optional[Sequence[str]], *, label: str) -> list[str]:
|
||||
cleaned: list[str] = []
|
||||
for raw in urls or ():
|
||||
url = str(raw or "").strip()
|
||||
if not url:
|
||||
continue
|
||||
if not url.startswith("https://"):
|
||||
raise ValueError(f"{label}必须使用临时素材 HTTPS URL")
|
||||
cleaned.append(url)
|
||||
return cleaned
|
||||
|
||||
|
||||
def _prefix_content_ref_urls(base: str, urls: Sequence[str]) -> str:
|
||||
"""Prepend content-reference URLs that are not already present in the text."""
|
||||
extras: list[str] = []
|
||||
for url in urls:
|
||||
if url and url not in base:
|
||||
extras.append(url)
|
||||
if not extras:
|
||||
return base
|
||||
if not base:
|
||||
return " ".join(extras)
|
||||
return " ".join(extras + [base])
|
||||
|
||||
|
||||
def build_mj_prompt(
|
||||
prompt: str,
|
||||
*,
|
||||
aspect_ratio: str = "1:1",
|
||||
quality: int = MJ_QUALITY_DEFAULT,
|
||||
hd: bool = False,
|
||||
image_weight: Optional[float] = None,
|
||||
raw: bool = False,
|
||||
stylize: int = MJ_STYLIZE_DEFAULT,
|
||||
chaos: int = MJ_CHAOS_DEFAULT,
|
||||
seed: int = -1,
|
||||
has_reference_image: bool = False,
|
||||
content_ref_urls: Optional[Sequence[str]] = None,
|
||||
style_ref_urls: Optional[Sequence[str]] = None,
|
||||
style_weight: int = MJ_SW_DEFAULT,
|
||||
weird: int = MJ_WEIRD_DEFAULT,
|
||||
tile: bool = False,
|
||||
exp: int = MJ_EXP_DEFAULT,
|
||||
draft: bool = False,
|
||||
no_text: str = "",
|
||||
) -> str:
|
||||
"""Assemble the final Midjourney prompt with controlled trailing flags.
|
||||
|
||||
Stable order:
|
||||
``[content_ref_urls...] [base_prompt] [--no ...] --ar --q [--hd] [--iw]
|
||||
[--sref ...] [--sw] [--raw] --s [--c] [--weird] [--tile] [--exp]
|
||||
[--draft] [--seed]``
|
||||
"""
|
||||
base = strip_controlled_mj_flags(prompt)
|
||||
if not base:
|
||||
raise ValueError("提示词不能为空")
|
||||
|
||||
ratio = str(aspect_ratio or "").strip()
|
||||
if ratio not in MJ_ASPECT_RATIO_OPTIONS:
|
||||
raise ValueError(f"画面比例无效:{aspect_ratio}")
|
||||
|
||||
quality_value = int(quality)
|
||||
if quality_value < MJ_QUALITY_MIN or quality_value > MJ_QUALITY_MAX:
|
||||
raise ValueError(f"渲染质量须为 {MJ_QUALITY_MIN}–{MJ_QUALITY_MAX}")
|
||||
|
||||
stylize_value = int(stylize)
|
||||
if stylize_value < MJ_STYLIZE_MIN or stylize_value > MJ_STYLIZE_MAX:
|
||||
raise ValueError(f"风格化须为 {MJ_STYLIZE_MIN}–{MJ_STYLIZE_MAX}")
|
||||
|
||||
chaos_value = int(chaos)
|
||||
if chaos_value < MJ_CHAOS_MIN or chaos_value > MJ_CHAOS_MAX:
|
||||
raise ValueError(f"差异度须为 {MJ_CHAOS_MIN}–{MJ_CHAOS_MAX}")
|
||||
|
||||
weird_value = int(weird)
|
||||
if weird_value < MJ_WEIRD_MIN or weird_value > MJ_WEIRD_MAX:
|
||||
raise ValueError(f"怪异度须为 {MJ_WEIRD_MIN}–{MJ_WEIRD_MAX}")
|
||||
|
||||
exp_value = int(exp)
|
||||
if exp_value < MJ_EXP_MIN or exp_value > MJ_EXP_MAX:
|
||||
raise ValueError(f"实验值须为 {MJ_EXP_MIN}–{MJ_EXP_MAX}")
|
||||
|
||||
prompt_content_urls = _normalize_https_urls(
|
||||
content_ref_urls, label="内容参考图"
|
||||
)
|
||||
style_urls = _normalize_https_urls(style_ref_urls, label="风格参考图")
|
||||
|
||||
text_body = _prefix_content_ref_urls(base, prompt_content_urls)
|
||||
|
||||
no_value = str(no_text or "").strip()
|
||||
parts: list[str] = [text_body]
|
||||
if no_value:
|
||||
parts.append(f"--no {no_value}")
|
||||
|
||||
parts.extend([f"--ar {ratio}", f"--q {quality_value}"])
|
||||
if hd:
|
||||
parts.append("--hd")
|
||||
|
||||
if has_reference_image or prompt_content_urls:
|
||||
if image_weight is None:
|
||||
iw_value = MJ_IW_DEFAULT
|
||||
else:
|
||||
iw_value = float(image_weight)
|
||||
if iw_value < MJ_IW_MIN or iw_value > MJ_IW_MAX:
|
||||
raise ValueError(f"参考图权重须为 {MJ_IW_MIN:g}–{MJ_IW_MAX:g}")
|
||||
parts.append(f"--iw {_format_iw(iw_value)}")
|
||||
|
||||
if style_urls:
|
||||
parts.append("--sref " + " ".join(style_urls))
|
||||
sw_value = int(style_weight)
|
||||
if sw_value < MJ_SW_MIN or sw_value > MJ_SW_MAX:
|
||||
raise ValueError(f"风格权重须为 {MJ_SW_MIN}–{MJ_SW_MAX}")
|
||||
parts.append(f"--sw {sw_value}")
|
||||
|
||||
if raw:
|
||||
parts.append("--raw")
|
||||
|
||||
parts.append(f"--s {stylize_value}")
|
||||
|
||||
if chaos_value > 0:
|
||||
parts.append(f"--c {chaos_value}")
|
||||
|
||||
if weird_value > 0:
|
||||
parts.append(f"--weird {weird_value}")
|
||||
|
||||
if tile:
|
||||
parts.append("--tile")
|
||||
|
||||
if exp_value > 0:
|
||||
parts.append(f"--exp {exp_value}")
|
||||
|
||||
if draft:
|
||||
parts.append("--draft")
|
||||
|
||||
seed_value = int(seed)
|
||||
if seed_value >= 0:
|
||||
parts.append(f"--seed {seed_value}")
|
||||
|
||||
return " ".join(parts)
|
||||
|
||||
|
||||
__all__ = [
|
||||
"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_MODEL_ID",
|
||||
"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",
|
||||
"build_mj_prompt",
|
||||
"strip_controlled_mj_flags",
|
||||
]
|
||||
Reference in New Issue
Block a user