Skip to content

Image Generation API

源力AI 支持通过 DALL-E 系列模型生成、编辑和变体图像,接口与 OpenAI Images API 完全兼容。

接口地址

功能方法地址
生成图像POSThttps://bww.letcareme.com/v1/images/generations
编辑图像POSThttps://bww.letcareme.com/v1/images/edits
图像变体POSThttps://bww.letcareme.com/v1/images/variations

生成图像

请求参数

参数类型必填说明
promptstring图像描述,最多 4000 字符(DALL-E 3)
modelstringdall-e-3(默认)或 dall-e-2
ninteger生成数量,DALL-E 3 只支持 1
sizestring图像尺寸,见下表
qualitystringstandard(默认)或 hd(仅 DALL-E 3)
stylestringvivid(默认)或 natural(仅 DALL-E 3)
response_formatstringurl(默认)或 b64_json

支持的尺寸

模型支持尺寸
DALL-E 31024x10241792x1024(横版)、1024x1792(竖版)
DALL-E 2256x256512x5121024x1024

示例

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

按量计费 · 即开即用 · 稳定可靠