Skip to content

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 对象

参数类型默认值描述
imagesControlNetImage[]必填ControlNet 图像数组
strengthnumber1.0整体 ControlNet 强度 (0-1)

ControlNetImage 对象

参数类型默认值描述
imageImageInput必填参考图像
infoExtractednumber0.7信息提取级别 (0.01-1)
strengthnumber0.6单个图像强度 (0.01-1)
controlnetModelImageModel当前模型编码用模型

理解参数

信息提取

控制从参考中提取多少结构信息:

  • 较低值 (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,
  },
});

提示

  1. 从默认值开始 - 从 infoExtracted: 0.7strength: 0.6 开始,然后调整。

  2. 使用清晰的参考 - 构图和结构清晰的图像效果最好。

  3. 与提示词平衡 - 你的提示词应该补充而非与参考矛盾。

  4. 多尝试 - 不同的 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,
      },
    ],
  },
});

基于 MIT 许可证发布