多角色定位
在图像中定位多个角色,每个角色有独立的提示词和位置。
WARNING
多角色定位仅适用于 V4+ 模型。
基本用法
typescript
import { NovelAI } from 'novelai-sdk-unofficial';
const client = new NovelAI({ apiKey: 'your-api-key' });
const images = await client.image.generate({
prompt: 'two people standing in a park',
model: 'nai-diffusion-4-5-full',
characters: [
{
prompt: '1girl, red hair, blue eyes, wearing a dress',
position: [0.25, 0.5],
enabled: true,
},
{
prompt: '1boy, black hair, green eyes, wearing a suit',
position: [0.75, 0.5],
enabled: true,
},
],
});角色参数
| 参数 | 类型 | 默认值 | 描述 |
|---|---|---|---|
prompt | string | 必填 | 角色描述 |
negativePrompt | string | '' | 该角色要避免的内容 |
position | Position | [0.5, 0.5] | 图像中的位置 |
enabled | boolean | true | 是否包含该角色 |
位置系统
坐标系统
位置指定为 [x, y],其中:
x:水平位置(0 = 左,1 = 右)y:垂直位置(0 = 上,1 = 下)
typescript
// 左侧
position: [0.2, 0.5]
// 中心
position: [0.5, 0.5]
// 右侧
position: [0.8, 0.5]
// 左上角
position: [0.2, 0.2]位置预设
你也可以使用预设位置(A1-E5 网格):
typescript
import { positionPresetToCoords } from 'novelai-sdk-unofficial';
// 使用预设
characters: [
{
prompt: '1girl',
position: 'B2',
},
]
// 将预设转换为坐标
const coords = positionPresetToCoords('B2'); // [0.3, 0.3]网格布局:
text
A B C D E
1 [0.1, 0.1, 0.3, 0.5, 0.7, 0.9]
2 [0.1, 0.3, ...]
3 [0.1, 0.5, ...]
4 [0.1, 0.7, ...]
5 [0.1, 0.9, ...]示例
两个角色并排
typescript
const images = await client.image.generate({
prompt: 'two friends talking',
model: 'nai-diffusion-4-5-full',
characters: [
{
prompt: '1girl, blonde hair, smiling',
position: [0.3, 0.5],
},
{
prompt: '1girl, brown hair, laughing',
position: [0.7, 0.5],
},
],
});三个角色
typescript
const images = await client.image.generate({
prompt: 'group photo',
model: 'nai-diffusion-4-5-full',
characters: [
{
prompt: '1girl, red dress',
position: [0.2, 0.5],
},
{
prompt: '1boy, blue suit',
position: [0.5, 0.5],
},
{
prompt: '1girl, green dress',
position: [0.8, 0.5],
},
],
});带独立负面提示词
typescript
characters: [
{
prompt: '1girl, detailed face',
negativePrompt: 'bad anatomy',
position: [0.3, 0.5],
},
{
prompt: '1boy, detailed face',
negativePrompt: 'bad anatomy, blurry',
position: [0.7, 0.5],
},
]禁用角色
typescript
characters: [
{
prompt: '1girl',
position: [0.3, 0.5],
enabled: true,
},
{
prompt: '1boy',
position: [0.7, 0.5],
enabled: false, // 该角色不会出现
},
]提示
主提示词很重要 - 主
prompt应描述整体场景和构图。具体描述 - 每个角色的提示词应清楚描述其独特特征。
考虑重叠 - 位置太近的角色可能会混合或重叠。
测试位置 - 从分离良好的位置开始,根据需要调整。
与角色参考结合
你可以将角色参考与多角色定位结合使用(仅 V4.5):
typescript
const images = await client.image.generate({
prompt: 'two people in a cafe',
model: 'nai-diffusion-4-5-full',
characterReferences: [
{
image: referenceImage,
fidelity: 0.75,
},
],
characters: [
{
prompt: '1girl, sitting',
position: [0.3, 0.5],
},
{
prompt: '1boy, standing',
position: [0.7, 0.5],
},
],
});