代码示例 / Python

python 调用DALL·E

图像生成 API 指南

功能概述

API 提供三种主要功能:

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

1. 图像生成

基础使用


from openai import OpenAI

client = OpenAI(

    base_url="https://www.cokeapi.com/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

参数说明

- DALL·E 3:单次1张

- DALL·E 2:单次最多10张

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

使用示例


from openai import OpenAI

client = OpenAI(

    base_url="https://www.cokeapi.com/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

要求说明

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

使用示例


from openai import OpenAI

client = OpenAI(

    base_url="https://www.cokeapi.com/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

技术要求

提示技巧

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