> ## Documentation Index
> Fetch the complete documentation index at: https://docs.comfy.org/llms.txt
> Use this file to discover all available pages before exploring further.

# Recraft Replace Background - ComfyUI 原生节点文档

> 自动识别前景主体并替换背景的 Recraft 合作伙伴节点

<img src="https://mintcdn.com/dripart/5003JSxULDwNImme/images/built-in-nodes/api_nodes/recraft/recraft-replace-background.jpg?fit=max&auto=format&n=5003JSxULDwNImme&q=85&s=798d76cef5bcc224546b066e8a9b2db2" alt="ComfyUI 原生Recraft Replace Background节点" width="1625" height="1158" data-path="images/built-in-nodes/api_nodes/recraft/recraft-replace-background.jpg" />

Recraft Replace Background 节点能够通过 Recraft 的 API 来智能识别图像中的主体对象，并根据文本描述生成新的背景场景。

## 参数说明

### 基本参数

| 参数     | 类型  | 默认值 | 说明                        |
| ------ | --- | --- | ------------------------- |
| image  | 图像  | -   | 包含需要保留主体的输入图像             |
| prompt | 字符串 | ""  | 用于图像生成的提示词                |
| n      | 整数  | 1   | 要生成的图像数量(1-6)             |
| seed   | 整数  | 0   | 确定节点是否应该重新运行的种子；实际结果与种子无关 |

### 可选参数

| 参数               | 类型            | 说明                 |
| ---------------- | ------------- | ------------------ |
| recraft\_style   | Recraft Style | 设置生成背景的风格          |
| negative\_prompt | 字符串           | 图像中不希望出现的元素的可选文本描述 |

### 输出

| 输出    | 类型 | 说明         |
| ----- | -- | ---------- |
| IMAGE | 图像 | 替换背景后的完整图像 |

## 源码参考

\[节点源码 (更新于2025-05-03)]

```python theme={null}

class RecraftReplaceBackgroundNode:
    """
    Replace background on image, based on provided prompt.
    """

    RETURN_TYPES = (IO.IMAGE,)
    DESCRIPTION = cleandoc(__doc__ or "")  # Handle potential None value
    FUNCTION = "api_call"
    API_NODE = True
    CATEGORY = "api node/image/Recraft"

    @classmethod
    def INPUT_TYPES(s):
        return {
            "required": {
                "image": (IO.IMAGE, ),
                "prompt": (
                    IO.STRING,
                    {
                        "multiline": True,
                        "default": "",
                        "tooltip": "Prompt for the image generation.",
                    },
                ),
                "n": (
                    IO.INT,
                    {
                        "default": 1,
                        "min": 1,
                        "max": 6,
                        "tooltip": "The number of images to generate.",
                    },
                ),
                "seed": (
                    IO.INT,
                    {
                        "default": 0,
                        "min": 0,
                        "max": 0xFFFFFFFFFFFFFFFF,
                        "control_after_generate": True,
                        "tooltip": "Seed to determine if node should re-run; actual results are nondeterministic regardless of seed.",
                    },
                ),
            },
            "optional": {
                "recraft_style": (RecraftIO.STYLEV3,),
                "negative_prompt": (
                    IO.STRING,
                    {
                        "default": "",
                        "forceInput": True,
                        "tooltip": "An optional text description of undesired elements on an image.",
                    },
                ),
            },
            "hidden": {
                "auth_token": "AUTH_TOKEN_COMFY_ORG",
            },
        }

    def api_call(
        self,
        image: torch.Tensor,
        prompt: str,
        n: int,
        seed,
        auth_token=None,
        recraft_style: RecraftStyle = None,
        negative_prompt: str = None,
        **kwargs,
    ):
        default_style = RecraftStyle(RecraftStyleV3.realistic_image)
        if recraft_style is None:
            recraft_style = default_style

        if not negative_prompt:
            negative_prompt = None

        request = RecraftImageGenerationRequest(
            prompt=prompt,
            negative_prompt=negative_prompt,
            model=RecraftModel.recraftv3,
            n=n,
            style=recraft_style.style,
            substyle=recraft_style.substyle,
            style_id=recraft_style.style_id,
        )

        images = []
        total = image.shape[0]
        pbar = ProgressBar(total)
        for i in range(total):
            sub_bytes = handle_recraft_file_request(
                image=image[i],
                path="/proxy/recraft/images/replaceBackground",
                request=request,
                auth_token=auth_token,
            )
            images.append(torch.cat([bytesio_to_image_tensor(x) for x in sub_bytes], dim=0))
            pbar.update(1)

        images_tensor = torch.cat(images, dim=0)
        return (images_tensor, )

```
