ControlNet(风格迁移)
ControlNet 允许你使用参考图像控制生成图像的构图、姿势和风格。
基本用法
typescript
import { NovelAI } from 'novelai-sdk-unofficial';
import { readFileSync, writeFileSync } from 'fs';
const client = new NovelAI({ apiKey: 'your-api-key' });
const poseReference = readFileSync('pose.png');
const images = await client.image.generate({
prompt: '1girl, standing',
model: 'nai-diffusion-4-5-full',
controlnet: {
images: [
{
image: poseReference,
infoExtracted: 0.7,
strength: 0.6,
},
],
strength: 1.0,
},
});
writeFileSync('output.png', images[0]);参数说明
ControlNet 对象
| 参数 | 类型 | 默认值 | 描述 |
|---|---|---|---|
images | ControlNetImage[] | 必填 | ControlNet 图像数组 |
strength | number | 1.0 | 整体 ControlNet 强度 (0-1) |
ControlNetImage 对象
| 参数 | 类型 | 默认值 | 描述 |
|---|---|---|---|
image | ImageInput | 必填 | 参考图像 |
infoExtracted | number | 0.7 | 信息提取级别 (0.01-1) |
strength | number | 0.6 | 单个图像强度 (0.01-1) |
controlnetModel | ImageModel | 当前模型 | 编码用模型 |
理解参数
信息提取
控制从参考中提取多少结构信息:
- 较低值 (0.1-0.3):宽松解释,更多创作自由
- 中等值 (0.4-0.7):平衡提取
- 较高值 (0.8-1.0):严格遵循结构
强度
控制提取的信息对输出的影响程度:
- 较低值 (0.1-0.3):微妙影响
- 中等值 (0.4-0.6):适度影响
- 较高值 (0.7-1.0):强烈影响
示例
姿势迁移
typescript
const images = await client.image.generate({
prompt: '1girl, dancing',
controlnet: {
images: [
{
image: poseImage,
infoExtracted: 0.8,
strength: 0.7,
},
],
},
});风格/构图迁移
typescript
const images = await client.image.generate({
prompt: 'landscape, mountains',
controlnet: {
images: [
{
image: compositionReference,
infoExtracted: 0.5,
strength: 0.5,
},
],
},
});多个参考
typescript
const images = await client.image.generate({
prompt: '1girl, detailed background',
controlnet: {
images: [
{
image: poseReference,
infoExtracted: 0.8,
strength: 0.6,
},
{
image: styleReference,
infoExtracted: 0.4,
strength: 0.4,
},
],
strength: 1.0,
},
});提示
从默认值开始 - 从
infoExtracted: 0.7和strength: 0.6开始,然后调整。使用清晰的参考 - 构图和结构清晰的图像效果最好。
与提示词平衡 - 你的提示词应该补充而非与参考矛盾。
多尝试 - 不同的 infoExtracted 和 strength 组合会产生不同的结果。
与其他功能结合
与角色参考结合(V4.5)
typescript
const images = await client.image.generate({
prompt: '1girl, standing',
model: 'nai-diffusion-4-5-full',
characterReferences: [
{
image: characterRef,
fidelity: 0.75,
},
],
controlnet: {
images: [
{
image: poseRef,
strength: 0.6,
},
],
},
});与图生图结合
typescript
const images = await client.image.generate({
prompt: 'enhanced version',
i2i: {
image: baseImage,
strength: 0.5,
},
controlnet: {
images: [
{
image: styleRef,
strength: 0.4,
},
],
},
});