Skip to content

python 调用DALL·E

图像生成 API 指南

功能概述

API 提供三种主要功能:

  1. 🎨 文本生成图像 (DALL·E 3 和 DALL·E 2)
  2. ✏️ 图像编辑 (仅 DALL·E 2)
  3. 🔄 图像变体生成 (仅 DALL·E 2)

1. 图像生成

基础使用

python
from openai import OpenAI
client = OpenAI(
    base_url="https://www.anyapi.vip/v1",
    api_key=key
)

response = client.images.generate(
  model="dall-e-3",
  prompt="a white siamese cat",
  size="1024x1024",
  quality="standard",
  n=1,
)

image_url = response.data[0].url

参数说明

  • 尺寸选项:1024x1024、1024x1792、1792x1024
  • 质量选项:standard(默认)、hd(DALL·E 3专属)
  • 数量限制
    • DALL·E 3:单次1张
    • DALL·E 2:单次最多10张

2. 图像编辑 (DALL·E 2)

使用示例

python
from openai import OpenAI
client = OpenAI(
    base_url="https://www.anyapi.vip/v1",
    api_key=key
)

response = client.images.edit((
  model="dall-e-2",
  image=open("sunlit_lounge.png", "rb"),
  mask=open("mask.png", "rb"),
  prompt="A sunlit indoor lounge area with a pool containing a flamingo",
  n=1,
  size="1024x1024"
)
image_url = response.data[0].url

要求说明

  • 📝 图片和蒙版必须为PNG格式
  • 📏 必须为方形图片
  • 💾 文件大小<4MB
  • ⚖️ 图片和蒙版尺寸必须相同

3. 图像变体生成 (DALL·E 2)

使用示例

python
from openai import OpenAI
client = OpenAI(
    base_url="https://www.anyapi.vip/v1",
    api_key=key
)

response = client.images.create_variation(
  model="dall-e-2",
  image=open("corgi_and_cat_paw.png", "rb"),
  n=1,
  size="1024x1024"
)

image_url = response.data[0].url

技术要求

  • 📝 PNG格式
  • 📏 方形图片
  • 💾 文件大小<4MB

提示技巧

DALL·E 3 特性

  • 自动优化提示
  • 可通过特殊指令控制提示优化:
I NEED to test how the tool works with extremely simple prompts. 
DO NOT add any detail, just use it AS-IS:

注意事项

内容审核

  • 严格遵循内容政策
  • 违规内容将返回错误

图片URL

  • 有效期为1小时
  • 可选择Base64格式返回

AnyAPI — 专业的 AI 接口聚合服务