n/programu/cloud_8769 · 2025-08-15
注册 登录
n/program · 2025-08-15 · 已编辑 · 321 次浏览
u/cloud_8769白银

免费分享【绘画指令生成器】的代码

[upvote]

import random
from dataclasses import dataclass, field
from typing import List, Dict

@dataclass
class PromptConfig:
    theme: str                                   # 必填:主题/主体
    seed: int | None = None                      # 复现实验用
    count: int = 5                               # 生成多少条
    aspect_ratio: str = "16:9"                   # 画幅
    stylize: int = 250                           # 风格强度(示意)
    min_tokens: Dict[str, int] = field(default_factory=lambda: {
        "style": 1, "artist": 1, "lighting": 1, "lens": 1, "detail": 1, "extras": 1
    })

LIB = {
    "style": [
        "cinematic", "illustrative", "photorealistic", "low-poly", "isometric",
        "watercolor", "oil painting", "flat design", "cyberpunk", "baroque"
    ],
    "artist": [
        "Hayao Miyazaki", "Studio Ghibli", "Greg Rutkowski", "Beeple",
        "Makoto Shinkai", "Gustav Klimt", "H.R. Giger", "Magritte"
    ],
    "lighting": [
        "rim light", "softbox lighting", "global illumination", "volumetric light",
        "golden hour", "neon glow", "moonlit", "backlit", "chiaroscuro"
    ],
    "lens": [
        "35mm lens", "85mm portrait", "ultra wide", "macro", "tilt-shift", "f1.4 bokeh"
    ],
    "detail": [
        "ultradetail", "8k texture", "highly detailed", "sharp focus",
        "film grain", "subsurface scattering"
    ],
    "extras": [
        "trending on artstation", "depth of field", "rule of thirds",
        "symmetry", "dramatic composition", "warm color grading", "cool color grading"
    ],
}

def weighted_sample(pool: List[str], k: int, weights: List[float] | None = None):
    # 从列表中无放回抽样,支持权重
    if k <= 0: return []
    if weights is None:
        return random.sample(pool, k=min(k, len(pool)))
    items = list(zip(pool, weights))
    chosen = []
    for _ in range(min(k, len(pool))):
        total = sum(w for _, w in items)
        r = random.random() * total
        acc = 0.0
        for i, (item, w) in enumerate(items):
            acc += w
            if r <= acc:
                chosen.append(item)
                items.pop(i)
                break
    return chosen

def build_prompt(theme: str, take: Dict[str, int], seed: int | None,
                 ar: str, stylize: int) -> str:
    parts = [theme]
    for key, n in take.items():
        parts += weighted_sample(LIB[key], n)
    # 去重并保持顺序
    seen, uniq = set(), []
    for t in parts:
        if t not in seen:
            uniq.append(t); seen.add(t)
    core = ", ".join(uniq)
    tail = f"--ar {ar} --stylize {stylize}" + (f" --seed {seed}" if seed is not None else "")
    return f"{core}, {tail}"

def generate(cfg: PromptConfig,
             weights: Dict[str, List[float]] | None = None) -> List[str]:
    if cfg.seed is not None:
        random.seed(cfg.seed)
    prompts = []
    for _ in range(cfg.count):
        take = {}
        for key, minimum in cfg.min_tokens.items():
            take[key] = minimum + random.randint(0, max(0, min(2, len(LIB[key]) - minimum)))
        prompt = build_prompt(
            theme=cfg.theme,
            take=take,
            seed=cfg.seed,
            ar=cfg.aspect_ratio,
            stylize=cfg.stylize
        )
        prompts.append(prompt)
    return prompts

if __name__ == "__main__":
    cfg = PromptConfig(
        theme="a fox reading under a street lamp at midnight",
        seed=1234, count=3, aspect_ratio="16:9", stylize=250
    )
    for p in generate(cfg):
        print(p)

[/upvote]

9
25
登录后参与讨论。
u/udc2b6nr 广告
QuicksilverVPN

快银 VPN,一键连接全球节点,线路稳定、延迟低。支持 Windows、macOS 等多平台,TUN 全局模式覆盖全部应用流量。无需复杂配置,下载即用。

umami.dnsl.top 了解更多

25 条回复

排序
  1. local 💬 钻石 2 YEARS 2025-08-15

    这是什么

    1
  2. Itachi 🤙 黄金 2 YEARS 2025-08-15

    看看

    1
  3. local 💬 钻石 2 YEARS 2025-08-15

    不建议设置成回复可见

    0
  4. James 💯 传奇 MANAGER 2025-08-15

    加了个代码框

    0
  5. nahear 钻石 2025-08-15

    看看

    0
  6. jingzhang 钻石 2025-08-15

    好东西,谢谢分享

    0
  7. 0x996 铂金 2025-08-15

    看看什么东西?

    0
  8. misawei 白银 2025-08-15

    可以玩你画我猜吗

    0
  9. akalin 青铜 2 YEARS 2025-08-15

    looklook

    0
  10. mokl 黄金 2025-08-15

    什么东西好奇

    0
  11. Jisucloud 铂金 3 YEARS 2025-08-15

    看一下了

    0
  12. xylon 铂金 端午安康 2025-08-16

    看看

    0
  13. leelokhan 黄金 2025-08-16

    看看

    0
  14. skla2580 白银 2025-08-16

    看看是啥

    0
  15. hk5rf 🍉 铂金 不离不弃 2025-08-16

    看看

    0
  16. Decoherer 黄金 2 YEARS 2025-08-16

    看看

    0
  17. xybug 钻石 2025-08-16

    看看什么情况

    0
  18. Decoherer 黄金 2 YEARS 2025-08-16

    这啥东西,这ai绘画用sd3.5不香吗

    0
  19. kaoqy 铂金 2 YEARS 2025-08-16

    看看

    0
  20. mizzle 💬 黄金 不离不弃 2025-08-16

    看看是啥

    0
  21. NoCilantro 黄金 不离不弃 2025-08-16

    回复可见,只能回复一下 :ac06:

    0
  22. lovoup 钻石 3 YEARS 2025-08-16

    看看,顶一个

    0
  23. redvb 黄金 3 YEARS 2025-08-17

    :xhj003:

    0
  24. wvspcn22 白银 2025-08-17

    谢谢分享,不错的东西

    0
  25. afternoon 黄金 2025-08-17

    谢谢分享

    0