跳至主要內容

/images/edits

LiteLLM 提供影像編輯功能,對應到 OpenAI 的 /images/edits API 端點。現在支援單張與多張影像編輯。

功能支援備註
成本追蹤適用於所有支援的模型
記錄可跨所有整合運作
終端使用者追蹤
備援可在支援的模型之間運作
負載平衡可在支援的模型之間運作
支援的操作建立影像編輯支援單張與多張影像
支援的 LiteLLM SDK 版本1.63.8+Gemini 支援需要 1.79.3+
支援的 LiteLLM Proxy 版本1.71.1+Gemini 支援需要 1.79.3+
支援的 LLM 提供者OpenAI, Gemini (Google AI Studio), Vertex AI, OpenRouter, Stability AI, AWS Bedrock (Stability), Black Forest LabsGemini 支援新的 gemini-2.5-flash-image 系列。Vertex AI 同時支援 Gemini 與 Imagen 模型。OpenRouter 透過 chat completions 路由影像編輯。Stability AI 與 Bedrock Stability 支援各種影像編輯操作。Black Forest Labs 支援 FLUX Kontext 模型。

⚡️請參閱 models.litellm.ai 以查看所有支援的模型與提供者

使用方式

LiteLLM Python SDK

基本影像編輯

OpenAI Image Edit
import litellm

# Edit an image with a prompt
response = litellm.image_edit(
model="gpt-image-1",
image=open("original_image.png", "rb"),
prompt="Add a red hat to the person in the image",
n=1,
size="1024x1024"
)

print(response)

多張影像編輯

OpenAI Multiple Images Edit
import litellm

# Edit multiple images with a prompt
response = litellm.image_edit(
model="gpt-image-1",
image=[
open("image1.png", "rb"),
open("image2.png", "rb"),
open("image3.png", "rb")
],
prompt="Apply vintage filter to all images",
n=1,
size="1024x1024"
)

print(response)

含遮罩的影像編輯

OpenAI Image Edit with Mask
import litellm

# Edit an image with a mask to specify the area to edit
response = litellm.image_edit(
model="gpt-image-1",
image=open("original_image.png", "rb"),
mask=open("mask_image.png", "rb"), # Transparent areas will be edited
prompt="Replace the background with a beach scene",
n=2,
size="512x512",
response_format="url"
)

print(response)

非同步影像編輯

Async OpenAI Image Edit
import litellm
import asyncio

async def edit_image():
response = await litellm.aimage_edit(
model="gpt-image-1",
image=open("original_image.png", "rb"),
prompt="Make the image look like a painting",
n=1,
size="1024x1024",
response_format="b64_json"
)
return response

# Run the async function
response = asyncio.run(edit_image())
print(response)

非同步多張影像編輯

Async OpenAI Multiple Images Edit
import litellm
import asyncio

async def edit_multiple_images():
response = await litellm.aimage_edit(
model="gpt-image-1",
image=[
open("portrait1.png", "rb"),
open("portrait2.png", "rb")
],
prompt="Add professional lighting to the portraits",
n=1,
size="1024x1024",
response_format="url"
)
return response

# Run the async function
response = asyncio.run(edit_multiple_images())
print(response)

含自訂參數的影像編輯

OpenAI Image Edit with Custom Parameters
import litellm

# Edit image with additional parameters
response = litellm.image_edit(
model="gpt-image-1",
image=open("portrait.png", "rb"),
prompt="Add sunglasses and a smile",
n=3,
size="1024x1024",
response_format="url",
user="user-123",
timeout=60,
extra_headers={"Custom-Header": "value"}
)

print(f"Generated {len(response.data)} image variations")
for i, image_data in enumerate(response.data):
print(f"Image {i+1}: {image_data.url}")

搭配 OpenAI SDK 的 LiteLLM Proxy

首先,將以下內容加入您的 litellm proxy config.yaml:

OpenAI Proxy Configuration
model_list:
- model_name: gpt-image-1
litellm_params:
model: gpt-image-1
api_key: os.environ/OPENAI_API_KEY

啟動 LiteLLM proxy server:

Start LiteLLM Proxy Server
litellm --config /path/to/config.yaml

# RUNNING on http://0.0.0.0:4000

透過 Proxy 進行基本影像編輯

OpenAI Proxy Image Edit
from openai import OpenAI

# Initialize client with your proxy URL
client = OpenAI(
base_url="http://localhost:4000", # Your proxy URL
api_key="your-api-key" # Your proxy API key
)

# Edit an image
response = client.images.edit(
model="gpt-image-1",
image=open("original_image.png", "rb"),
prompt="Add a red hat to the person in the image",
n=1,
size="1024x1024"
)

print(response)

cURL 範例

cURL Image Edit Request
curl -X POST "http://localhost:4000/v1/images/edits" \
-H "Authorization: Bearer your-api-key" \
-F "model=gpt-image-1" \
-F "image=@original_image.png" \
-F "mask=@mask_image.png" \
-F "prompt=Add a beautiful sunset in the background" \
-F "n=1" \
-F "size=1024x1024" \
-F "response_format=url"

cURL 多張影像範例

cURL Multiple Images Edit Request
curl -X POST "http://localhost:4000/v1/images/edits" \
-H "Authorization: Bearer your-api-key" \
-F "model=gpt-image-1" \
-F "image=@image1.png" \
-F "image=@image2.png" \
-F "image=@image3.png" \
-F "prompt=Apply artistic filter to all images" \
-F "n=1" \
-F "size=1024x1024" \
-F "response_format=url"

</TabItem>

<TabItem value="gemini" label="Gemini">

1. Add the Gemini image edit model to your `config.yaml`:
```yaml showLineNumbers title="Gemini Proxy Configuration"
model_list:
- model_name: gemini-image-edit
litellm_params:
model: gemini/gemini-2.5-flash-image
api_key: os.environ/GEMINI_API_KEY
  1. 啟動 LiteLLM proxy server:
Start LiteLLM Proxy Server
litellm --config /path/to/config.yaml
  1. 發送影像編輯請求(Gemini 回應僅限 base64):
Gemini Proxy Image Edit
curl -X POST "http://0.0.0.0:4000/v1/images/edits" \
-H "Authorization: Bearer <YOUR-LITELLM-KEY>" \
-F "model=gemini-image-edit" \
-F "image=@original_image.png" \
-F "prompt=Add a warm golden-hour glow to the scene" \
-F "size=1024x1024"

支援的影像編輯參數

參數類型描述必填
imageFileTypes要編輯的影像。必須是有效的 PNG 檔案,小於 4MB,且為正方形。
promptstr所需影像編輯的文字描述。
modelstr用於影像編輯的模型選填(預設為 dall-e-2
maskstr另一張影像,其完全透明的區域表示原始影像應被編輯的位置。必須是有效的 PNG 檔案,小於 4MB,且尺寸與 image 相同。選填
nint要產生的影像數量。必須介於 1 到 10 之間。選填(預設為 1)
sizestr生成影像的尺寸。必須為 256x256512x5121024x1024 之一。選填(預設為 1024x1024
response_formatstr回傳生成影像的格式。必須為 urlb64_json 之一。選填(預設為 url
userstr代表您的終端使用者的唯一識別碼。選填

回應格式

回應遵循 OpenAI Images API 格式:

Image Edit Response Structure
{
"created": 1677649800,
"data": [
{
"url": "https://example.com/edited_image_1.png"
},
{
"url": "https://example.com/edited_image_2.png"
}
]
}

對於 b64_json 格式:

Base64 Response Structure
{
"created": 1677649800,
"data": [
{
"b64_json": "iVBORw0KGgoAAAANSUhEUgAA..."
}
]
}
🚅
LiteLLM Enterprise
為正式環境打造的 SSO/SAML、稽核記錄、支出追蹤、多團隊管理與防護欄。
深入瞭解 →