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

# Qwen Image 編輯

> Qwen Image 圖片編輯欄位、模型限制與請求示例。

通過包含文本塊與圖片塊的單條 user 訊息，使用 Qwen Image 編輯參考圖片。

`POST https://api.mixroute.ai/v1/images/generations`

## 支援模型

| 模型 ID                  | 輸入                 |
| ---------------------- | ------------------ |
| `qwen-image-edit`      | 一個文本塊及 1-3 個參考圖片塊。 |
| `qwen-image-edit-plus` | 一個文本塊及 1-3 個參考圖片塊。 |
| `qwen-image-edit-max`  | 一個文本塊及 1-3 個參考圖片塊。 |

qwen-image、qwen-image-plus、qwen-image-2.0、qwen-image-2.0-pro、qwen-image-max 的生成請求見 [Qwen Image](/zh-hant/api-reference/endpoint/qwen-image)。

## 請求參數

| 欄位                                 | 型別      | 必填   | 說明                                                   |
| ---------------------------------- | ------- | ---- | ---------------------------------------------------- |
| `model`                            | string  | 是    | 對應模型表中的完整模型 ID。                                      |
| `input`                            | object  | 是    | 輸入物件。                                                |
| `input.messages`                   | array   | 是    | 有且只有一條 user 訊息。                                      |
| `input.messages[].role`            | string  | 是    | 固定為 `user`。                                          |
| `input.messages[].content`         | array   | 是    | 包含一個文本塊及 1-3 個圖片塊，每個塊只使用 text 或 image，不能同時包含兩者。      |
| `input.messages[].content[].text`  | string  | 按內容塊 | 文本塊中的提示詞。                                            |
| `input.messages[].content[].image` | string  | 按內容塊 | 參考圖片的公網 HTTPS URL，由上游服務下載；請求包含 1-3 個圖片塊。             |
| `parameters`                       | object  | 否    | 可選生成設定。                                              |
| `parameters.size`                  | string  | 否    | 尺寸採用 `寬*高` 格式，例如 `1024*1024` 或 `1664*928`，使用星號而不是 x。 |
| `parameters.n`                     | integer | 否    | 單次請求僅支援 `1` 張；需要多張時分別呼叫。                             |
| `parameters.seed`                  | integer | 否    | 隨機種子。同提示詞和同種子可產生近似結果，不保證完全相同。                        |
| `parameters.watermark`             | boolean | 否    | 是否新增水印。                                              |
| `parameters.negative_prompt`       | string  | 否    | 希望圖片中避免出現的元素。                                        |
| `parameters.prompt_extend`         | boolean | 否    | 是否由上游服務擴展提示詞、補充細節。                                   |

<Info>
  text 和 image 必須分別放在不同內容塊中。參考圖使用上游可訪問的公網 HTTPS URL。請求採用 input.messages 而不是 input.prompt；不傳頂層 prompt、multipart 檔案、Base64 data URI 或 metadata 包裝。
</Info>

## 請求示例

設定 `MIXROUTE_API_KEY`，並將參考圖佔位值替換為公網 HTTPS 圖片 URL。

```bash theme={null}
curl --request POST "https://api.mixroute.ai/v1/images/generations" \
  --header "Authorization: Bearer $MIXROUTE_API_KEY" \
  --header "Content-Type: application/json" \
  --data '{
  "model": "qwen-image-edit",
  "input": {
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "text": "Change the mug to blue while preserving the background."
          },
          {
            "image": "https://example.com/reference.png"
          }
        ]
      }
    ]
  },
  "parameters": {
    "size": "1024*1024",
    "n": 1,
    "seed": 42
  }
}'
```

### Python

```python theme={null}
import json
import os
import requests

payload = json.loads(r'''
{
  "model": "qwen-image-edit",
  "input": {
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "text": "Change the mug to blue while preserving the background."
          },
          {
            "image": "https://example.com/reference.png"
          }
        ]
      }
    ]
  },
  "parameters": {
    "size": "1024*1024",
    "n": 1,
    "seed": 42
  }
}
''')
response = requests.post(
    "https://api.mixroute.ai/v1/images/generations",
    headers={"Authorization": "Bearer " + os.environ["MIXROUTE_API_KEY"]},
    json=payload,
    timeout=180,
)
response.raise_for_status()
result = response.json()
output = result.get("output") or {}
image_urls = [
    part["image"]
    for choice in output.get("choices", [])
    for part in choice.get("message", {}).get("content", [])
    if part.get("image")
]
if not image_urls:
    raise RuntimeError(result.get("message") or result)
for url in image_urls:
    print(url)
```

## 響應

從 output.choices\[].message.content\[].image 讀取圖片 URL。usage.image\_count、usage.height、usage.width 描述生成結果，不使用基礎文生圖的 output.results 結構。

```json theme={null}
{
  "output": {
    "choices": [
      {
        "message": {
          "role": "assistant",
          "content": [
            {
              "image": "https://example.com/generated.png"
            }
          ]
        }
      }
    ]
  },
  "usage": {
    "image_count": 1,
    "height": 1024,
    "width": 1024
  }
}
```

## 錯誤處理

HTTP 400 可能表示缺少參考圖、模型名稱無效或尺寸不支援。HTTP 429 表示頻次或併發超限，應降低呼叫頻率後重試。
