主题
Image Generation API
源力AI 支持通过 DALL-E 系列模型生成、编辑和变体图像,接口与 OpenAI Images API 完全兼容。
接口地址
| 功能 | 方法 | 地址 |
|---|---|---|
| 生成图像 | POST | https://bww.letcareme.com/v1/images/generations |
| 编辑图像 | POST | https://bww.letcareme.com/v1/images/edits |
| 图像变体 | POST | https://bww.letcareme.com/v1/images/variations |
生成图像
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
prompt | string | 是 | 图像描述,最多 4000 字符(DALL-E 3) |
model | string | 否 | dall-e-3(默认)或 dall-e-2 |
n | integer | 否 | 生成数量,DALL-E 3 只支持 1 |
size | string | 否 | 图像尺寸,见下表 |
quality | string | 否 | standard(默认)或 hd(仅 DALL-E 3) |
style | string | 否 | vivid(默认)或 natural(仅 DALL-E 3) |
response_format | string | 否 | url(默认)或 b64_json |
支持的尺寸
| 模型 | 支持尺寸 |
|---|---|
| DALL-E 3 | 1024x1024、1792x1024(横版)、1024x1792(竖版) |
| DALL-E 2 | 256x256、512x512、1024x1024 |
示例
python
from openai import OpenAI
client = OpenAI(
api_key="sk-xxxxxxxxxxxxxxxxxxxxxxxx",
base_url="https://bww.letcareme.com/v1"
)
response = client.images.generate(
model="dall-e-3",
prompt="一只赛博朋克风格的橘猫,坐在霓虹灯闪烁的东京街头,细节丰富,电影感光线",
size="1024x1024",
quality="hd",
style="vivid",
n=1
)
image_url = response.data[0].url
print(f"图像 URL: {image_url}")
print(f"修正后的 prompt: {response.data[0].revised_prompt}")bash
curl https://bww.letcareme.com/v1/images/generations \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-xxxxxxxxxxxxxxxxxxxxxxxx" \
-d '{
"model": "dall-e-3",
"prompt": "一只赛博朋克风格的橘猫,坐在霓虹灯闪烁的东京街头,细节丰富,电影感光线",
"size": "1024x1024",
"quality": "hd",
"style": "vivid",
"n": 1
}'响应格式
json
{
"created": 1710000000,
"data": [
{
"url": "https://oaidalleapiprodscus.blob.core.windows.net/private/xxx/img-abc.png",
"revised_prompt": "A cyberpunk-style orange tabby cat, sitting on a Tokyo street illuminated by neon lights, highly detailed, cinematic lighting..."
}
]
}保存图像到本地
python
import requests
from openai import OpenAI
client = OpenAI(
api_key="sk-xxxxxxxxxxxxxxxxxxxxxxxx",
base_url="https://bww.letcareme.com/v1"
)
response = client.images.generate(
model="dall-e-3",
prompt="极简风格的山水画,水墨风格,留白构图",
size="1024x1024",
response_format="b64_json" # 直接获取 base64 数据
)
import base64
image_data = base64.b64decode(response.data[0].b64_json)
with open("output.png", "wb") as f:
f.write(image_data)
print("图像已保存到 output.png")编辑图像(DALL-E 2)
上传原始图像和蒙版,在指定区域进行编辑:
python
from openai import OpenAI
client = OpenAI(
api_key="sk-xxxxxxxxxxxxxxxxxxxxxxxx",
base_url="https://bww.letcareme.com/v1"
)
with open("original.png", "rb") as image_file, \
open("mask.png", "rb") as mask_file:
response = client.images.edit(
model="dall-e-2",
image=image_file, # 原始图像(PNG,最大 4MB,必须为正方形)
mask=mask_file, # 蒙版(透明区域为编辑区域)
prompt="给图中的人物添加一顶红色棒球帽",
size="1024x1024",
n=1
)
print(response.data[0].url)蒙版说明
蒙版图像中,透明(alpha=0)区域会被重新生成,不透明区域保持原样。图像和蒙版必须尺寸相同,格式为 PNG。
图像变体(DALL-E 2)
基于原图生成风格相似的变体:
python
from openai import OpenAI
client = OpenAI(
api_key="sk-xxxxxxxxxxxxxxxxxxxxxxxx",
base_url="https://bww.letcareme.com/v1"
)
with open("original.png", "rb") as image_file:
response = client.images.create_variation(
model="dall-e-2",
image=image_file,
n=3, # 生成 3 个变体
size="1024x1024"
)
for i, image in enumerate(response.data):
print(f"变体 {i+1}: {image.url}")提示词技巧
好的提示词能显著提升图像质量,以下是一些有效的结构:
[主体描述] + [风格/艺术形式] + [光线/氛围] + [构图/视角] + [细节要求]示例:
"一位身穿汉服的年轻女性,水彩插画风格,柔和的自然光,正面半身构图,精致细腻的线条""未来城市夜景,赛博朋克美学,霓虹反光,鸟瞰视角,超高细节,8K 画质""简约极简主义的品牌 Logo,几何形状,蓝色系,白色背景,矢量风格"
DALL-E 3 自动优化
DALL-E 3 会自动优化你的提示词以提升图像质量,响应中的 revised_prompt 字段包含实际使用的提示词,可以参考它来改进你的描述。
注意事项
- 图像 URL 有效期通常为 1 小时,需要持久保存请及时下载到自己的存储
- 生成图像受内容安全策略限制,不得包含暴力、色情、侵权等内容
- 上传用于编辑/变体的图像必须为 PNG 格式,最大 4MB