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

# Flux 图片编辑

> 使用 Flux 2 编辑图片，支持单图改图、多图融合、input_image 参数和 width/height 尺寸控制

## 模型简介

Flux 图片编辑和文生图共用 `/v1/images/generations`。不传 `input_image` 时是文生图；传入 `input_image` 时进入图片编辑；继续传 `input_image_2` 到 `input_image_8` 时进入多图融合编辑。

<Note>
  **关键结构**

  Flux 2 改图请求是 `application/json`，参考图字段是字符串：`input_image`、`input_image_2`、`input_image_3` ... `input_image_8`。字段值可以是公网图片 URL，也可以是 `data:image/png;base64,...` 形式的 data URL。
</Note>

## 模型和价格

| 模型               | 模型 ID              | 计费类型        | 当前价格       | 说明               |
| ---------------- | ------------------ | ----------- | ---------- | ---------------- |
| Flux 2 Pro       | `flux-2-pro`       | 按次付费 - Chat | \$0.0300/次 | 推荐默认模型，适合单图和多图编辑 |
| Flux 2 Flex      | `flux-2-flex`      | 按次付费 - Chat | \$0.0600/次 | 更细控制场景           |
| Flux 2 Max       | `flux-2-max`       | 按次付费 - Chat | \$0.0700/次 | 最高质量编辑           |
| Flux Kontext Pro | `flux-kontext-pro` | 按次付费 - Chat | \$0.0350/次 | 旧版 Kontext 兼容    |
| Flux Kontext Max | `flux-kontext-max` | 按次付费 - Chat | \$0.0700/次 | 旧版 Kontext 高质量模型 |

<Tip>
  实际可用模型和价格以控制台为准。多图融合不会因为参考图数量改变接口写法，仍按一次生成请求返回一张结果图。
</Tip>

## 快速开始

### 单图改图 cURL

```bash theme={null}
curl -X POST "https://api2.laozhang.ai/v1/images/generations" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "flux-2-pro",
    "prompt": "保留主体，换成干净的浅蓝色摄影棚背景，保持原始光照和主体细节",
    "input_image": "https://example.com/source.png",
    "width": 1792,
    "height": 1024,
    "output_format": "png"
  }'
```

### 多图改图 cURL

```bash theme={null}
curl -X POST "https://api2.laozhang.ai/v1/images/generations" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "flux-2-pro",
    "prompt": "结合三张参考图创作一张新图：使用 image 1 的红色机器人，image 2 的温室背景，image 3 的黄色滑板，合成一个机器人在温室前骑滑板的完整场景。保留白色围巾、橙色门、紫色轮子和黑色星星贴纸。",
    "input_image": "https://example.com/source_1_red_robot.png",
    "input_image_2": "https://example.com/source_2_glass_greenhouse.png",
    "input_image_3": "https://example.com/source_3_yellow_skateboard.png",
    "width": 1792,
    "height": 1024,
    "output_format": "png"
  }'
```

### 本地图片多图改图 cURL

本地图片不能用 `-F "image=@..."` 直接上传。先把本地图片转成 data URL，再放进 JSON 的 `input_image`、`input_image_2`、`input_image_3` 字段。

```bash theme={null}
cd ~/Downloads
export LAOZHANG_API_KEY="YOUR_API_KEY"

IMG1=$(base64 < source_1_red_robot.png | tr -d '\n')
IMG2=$(base64 < source_2_glass_greenhouse.png | tr -d '\n')
IMG3=$(base64 < source_3_yellow_skateboard.png | tr -d '\n')

curl -X POST "https://api2.laozhang.ai/v1/images/generations" \
  -H "Authorization: Bearer $LAOZHANG_API_KEY" \
  -H "Content-Type: application/json" \
  --data-binary @- \
  -o flux_local_multi_response.json <<EOF
{
  "model": "flux-2-pro",
  "prompt": "Combine the three provided reference images into one coherent new image. Use image 1 as the red robot, image 2 as the turquoise glass greenhouse background, and image 3 as the yellow skateboard. The final image must clearly include details from all three references, not a collage or split-screen.",
  "input_image": "data:image/png;base64,$IMG1",
  "input_image_2": "data:image/png;base64,$IMG2",
  "input_image_3": "data:image/png;base64,$IMG3",
  "width": 1792,
  "height": 1024,
  "output_format": "png"
}
EOF
```

返回结果里的 URL 有效期较短，建议立即下载：

```bash theme={null}
python3 - <<'PY'
import json
import urllib.request

with open("flux_local_multi_response.json") as f:
    url = json.load(f)["data"][0]["url"]

urllib.request.urlretrieve(url, "flux_local_multi_edit.png")
print("saved: flux_local_multi_edit.png")
PY
```

<Warning>
  不要用 `/v1/images/edits` 或 multipart `-F "image=@..."` 来调用 Flux 2 多图编辑。Flux 2 多图编辑使用 `/v1/images/generations` + JSON `input_image` 字段。
</Warning>

## Python 示例

### URL 方式

```python theme={null}
import requests

API_KEY = "YOUR_API_KEY"
BASE_URL = "https://api2.laozhang.ai/v1"

def flux_edit(prompt, image_urls, width=1024, height=1024, model="flux-2-pro"):
    if not image_urls:
        raise ValueError("image_urls 至少需要 1 张图")
    if len(image_urls) > 8:
        raise ValueError("Flux 2 最多支持 8 张参考图")

    payload = {
        "model": model,
        "prompt": prompt,
        "input_image": image_urls[0],
        "width": width,
        "height": height,
        "output_format": "png",
    }
    for index, image_url in enumerate(image_urls[1:], start=2):
        payload[f"input_image_{index}"] = image_url

    response = requests.post(
        f"{BASE_URL}/images/generations",
        headers={
            "Authorization": f"Bearer {API_KEY}",
            "Content-Type": "application/json",
        },
        json=payload,
        timeout=240,
    )
    response.raise_for_status()
    return response.json()["data"][0]["url"]

result_url = flux_edit(
    prompt="Use image 1 as the subject, image 2 as the background, and image 3 as the prop. Fuse them into one coherent scene.",
    image_urls=[
        "https://example.com/image1.png",
        "https://example.com/image2.png",
        "https://example.com/image3.png",
    ],
    width=1792,
    height=1024,
)
print(result_url)
```

### 本地文件方式

本地图片需要先转成 data URL，再放到 `input_image` 字段里。

```python theme={null}
import base64
import mimetypes
from pathlib import Path

def file_to_data_url(path):
    path = Path(path)
    mime_type = mimetypes.guess_type(path.name)[0] or "image/png"
    encoded = base64.b64encode(path.read_bytes()).decode("utf-8")
    return f"data:{mime_type};base64,{encoded}"

result_url = flux_edit(
    prompt="结合三张图创作一张新图：使用 image 1 的红色机器人、image 2 的温室背景、image 3 的黄色滑板。",
    image_urls=[
        file_to_data_url("source_1_red_robot.png"),
        file_to_data_url("source_2_glass_greenhouse.png"),
        file_to_data_url("source_3_yellow_skateboard.png"),
    ],
    width=1792,
    height=1024,
)
print(result_url)
```

## 参数说明

| 参数                                 | 类型      | 必填 | 说明                                                    |
| ---------------------------------- | ------- | -- | ----------------------------------------------------- |
| `model`                            | string  | 是  | 推荐 `flux-2-pro`；也可使用 `flux-2-max` / `flux-2-flex`     |
| `prompt`                           | string  | 是  | 编辑或融合指令；多图场景建议写明 image 1、image 2、image 3 的用途          |
| `input_image`                      | string  | 是  | 第 1 张参考图，公网 URL 或 data URL                            |
| `input_image_2` \~ `input_image_8` | string  | 否  | 第 2 到第 8 张参考图                                         |
| `width`                            | integer | 否  | 输出宽度，建议 16 的倍数                                        |
| `height`                           | integer | 否  | 输出高度，建议 16 的倍数                                        |
| `size`                             | string  | 否  | OpenAI 风格尺寸字符串，如 `1792x1024`；与 `width` / `height` 二选一 |
| `output_format`                    | string  | 否  | `jpeg` / `png`                                        |
| `seed`                             | integer | 否  | 固定随机种子，便于复现                                           |
| `safety_tolerance`                 | integer | 否  | 内容安全级别，默认 2                                           |

## 多图提示词建议

* 明确编号：用 `image 1`、`image 2`、`image 3` 指代不同参考图
* 明确保留元素：写出颜色、物体、背景、贴纸、文字等关键细节
* 明确合成目标：说明要生成“一张新图”，不是简单拼接
* 明确不要什么：例如不要边框、不要多栏排版、不要文字标签

```text theme={null}
Use image 1 as the character, image 2 as the background, and image 3 as the product.
Create one coherent commercial poster.
Preserve the character's clothing, the background lighting, and the product logo.
Do not create a collage or split-screen layout.
```

## 注意事项

1. **端点固定**：Flux 文生图、单图改图、多图改图都使用 `/v1/images/generations`
2. **请求格式**：使用 JSON，不使用 multipart 表单
3. **多图上限**：Flux 2 Pro / Max / Flex 最多 8 张参考图
4. **图片来源**：推荐公网 URL；本地文件请转为 data URL
5. **结果下载**：返回的 `data[0].url` 只有约 10 分钟有效，请立即下载

## 相关资源

* [Flux 图像生成 API](/api-capabilities/flux-image-generation) - 文生图参数和模型说明
* [价格对比计算器](https://api2.laozhang.ai/account/pricing) - 实时价格查询
* [在线体验 Demo](https://yingtu.ai) - 测试 Flux 效果
