SDXL 提示词样式器 - 菜单自定义
本文档说明如何使用外部配置文件自定义 SDXL 提示词样式器 的菜单选项。这种方法允许您轻松更新、添加或修改菜单分组,而无需编辑核心 Python 代码。
概述
脚本(`sdxl_prompt_styler.py`)定义了多个用于样式化提示词的菜单组。最初,这些菜单是硬编码在 Python 脚本中的。现在,菜单定义可以通过名为 styler_config.json 的 JSON 文件进行外部配置。这使得即使没有编程经验的用户也能轻松进行自定义。
文件结构
以下是一个示例文件夹结构,展示脚本如何指向不同的数据文件夹,每个文件夹对应 styler_config.json 中定义的菜单项:
project_directory/
|— sdxl_prompt_styler.py # 用于提示词样式化的主 Python 脚本
|— styler_config.json # 用于菜单自定义的外部配置文件
|— data/ # 包含所有样式类别的文件夹
|— camera/ # “camera” 样式的文件夹
|— composition/ # “composition” 样式的文件夹
|— depth/ # “depth” 样式的文件夹
|— environment/ # “environment” 样式的文件夹
|— filter/ # “filter” 样式的文件夹
|— focus/ # “focus” 样式的文件夹
|— lighting/ # “lighting” 样式的文件夹
|— mood/ # “mood” 样式的文件夹
|— subject/ # “subject” 样式的文件夹
|— theme/ # “theme” 样式的文件夹
|— timeofday/ # “timeofday” 样式的文件夹
data 目录中的每个文件夹名称必须与 styler_config.json 中菜单项列出的名称一致。此结构确保 JSON 文件中的每个菜单项都能直接链接到包含相应样式数据的文件夹。
此功能相关的文件如下:
project_directory/
|— sdxl_prompt_styler.py # 用于提示词样式化的主 Python 脚本
|— styler_config.json # 用于菜单自定义的外部配置文件
|— other_files/ # 其他相关文件和目录
外部配置文件(styler_config.json)
JSON 文件中每个菜单项的值必须与 data 目录中对应文件夹的名称完全匹配。只有当项名称与现有文件夹名称一致时,脚本才能正确链接到对应的样式数据。
styler_config.json 文件包含菜单定义。此文件必须放置在与主 Python 脚本(`sdxl_prompt_styler.py`)相同的目录中。
示例 styler_config.json
以下是 styler_config.json 文件的示例格式:
{
"Perfection Styler": [
"camera", "composition", "depth", "environment", "filter",
"focus", "lighting", "mood", "subject", "theme", "timeofday"
],
"Enhanced Styler": [
"camera", "composition", "depth", "environment", "filter",
"focus", "lighting", "mood", "subject", "theme", "timeofday"
]
}
- 每个菜单名称是一个键(例如 "Perfection Styler"),每个菜单下的项目以数组形式定义。
- 列表中的每个项目必须与
data 目录中相应文件夹的名称完全一致,才能正常工作。
- 用户可根据需要添加新菜单、修改现有菜单或删除菜单。
如何自定义菜单
1. 用文本编辑器打开 styler_config.json:使用您喜欢的任何文本编辑器(例如 Notepad、VSCode、Sublime Text)。
2. 修改菜单:
- 添加新菜单:只需在 JSON 文件中添加一个新的键值对。
- 编辑现有菜单:修改现有键下的项目列表。
- 删除菜单:删除对应的键值对即可。
添加新菜单
要添加一个名为 "Creative Styler" 的新菜单,请按如下方式修改 JSON:
{
"Perfection Styler": [
"camera", "composition", "depth", "environment", "filter",
"focus", "lighting", "mood", "subject", "theme", "timeofday"
],
"Enhanced Styler": [
"camera", "composition", "depth", "environment", "filter",
"focus", "lighting", "mood", "subject", "theme", "timeofday"
],
"Creative Styler": [
"abstract", "vivid colors", "dynamic", "motion", "unique perspective"
]
}
编辑现有菜单
要修改现有菜单(例如 "Enhanced Styler"),可按需更改其项目列表。例如,为 Enhanced Styler 添加一个新项 mystical:
{
"Perfection Styler": [
"camera", "composition", "depth", "environment", "filter",
"focus", "lighting", "mood", "subject", "theme", "timeofday"
],
"Enhanced Styler": [
"camera", "composition", "depth", "environment", "filter",
"focus", "lighting", "mood", "subject", "theme", "timeofday", "mystical"
]
}
删除菜单
如果您不再需要某个菜单(例如 "Perfection Styler"),只需将其键值对从 JSON 中删除即可:
{
"Enhanced Styler": [
"camera", "composition", "depth", "environment", "filter",
"focus", "lighting", "mood", "subject", "theme", "timeofday"
],
"Creative Styler": [
"abstract", "vivid colors", "dynamic", "motion", "unique perspective"
]
}
运行脚本
修改配置文件后,像往常一样运行 Python 脚本即可:
python sdxl_prompt_styler.py
脚本将自动从 styler_config.json 加载菜单定义。如果配置文件缺失或格式错误,脚本将回退到使用默认硬编码版本以确保功能正常运行。
故障排除
- 配置文件未找到:如果脚本找不到
styler_config.json,将显示错误信息并使用默认设置。
- JSON 格式无效:请确保
styler_config.json 是格式正确的 JSON 文件。您可使用在线 JSON 验证工具(如 jsonlint.com)验证其正确性。
总结
新的配置功能使管理与自定义 SDXL 提示词样式器菜单变得极为简便:
- 无需编辑 Python 代码。
- 使用简单的 JSON 文件实现灵活配置。
请自由添加或修改菜单,打造更符合您个人需求的提示词样式体验!