简单使用富有创造力的DALL·E 3 图像生成器——OpenAI Images Generations API

862 阅读9分钟

OpenAI 图片生成 API 申请与使用指南

DALL-E 3 是由 OpenAI 开发的先进图像生成模型,能够依据文本描述生成高质量的图像。

本文档旨在详细讲解如何操作 OpenAI 图片生成 API,让您轻松享受官方 DALL-E 的图像生成功能。

注册试用链接

注册试用链接

申请流程

欲使用 OpenAI 图片生成 API,首先访问 OpenAI 图片生成 API 页面,点击「Acquire」按钮以获取所需凭证:

如果您尚未登录或者注册,系统将自动跳转至登录页面,邀请您进行注册与登录,登录注册成功后会返回至当前页面。

首次申请时,您会获得一定的免费额度,允许您无需费用地体验该 API。

基本使用

接下来,在界面中填写相应内容,如下图所示:

首次调用 API 时,您至少需要填写以下三个字段:一个是 authorization,可在下拉菜单中选择。第二个参数是 model,此处需选择使用 OpenAI DALL-E 的官方模型类别,我们提供的模型类型主要是 1 种,详情请参阅提供的模型信息。最后一个参数是 prompt,即您输入的生成图像提示词。

请注意,右侧会自动生成相应的调用代码,您可以便捷地复制后直接运行,或直接点击「Try」按钮进行测试。

示例 Python 调用代码:

import requests

url = "https://api.acedata.cloud/openai/images/generations"

headers = {
    "accept": "application/json",
    "authorization": "Bearer {token}",
    "content-type": "application/json"
}

payload = {
    "model": "dall-e-3",
    "prompt": "A cute baby sea otter"
}

response = requests.post(url, json=payload, headers=headers)
print(response.text)

调用成功后,您将获得如下返回结果:

{
  "created": 1721626477,
  "data": [
    {
      "revised_prompt": "A delightful image showcasing a young sea otter, who is born brown, with wide charming eyes. It is delightfully lying on its back, paddling in the calm sea waters. Its dense, velvety fur appears wet and shimmering, capturing the essence of its habitat. The small creature curiously plays with a sea shell with its small paws, looking absolutely innocent and charming in its natural environment.",
      "url": "https://dalleprodsec.blob.core.windows.net/private/images/5d98aa7c-80c6-4523-b571-fc606ad455b9/generated_00.png?se=2024-07-23T05%3A34%3A48Z&sig=GAz%2Bi3%2BkHOQwAMhxcv22tBM%2FaexrxPgT9V0DbNrL4ik%3D&ske=2024-07-23T08%3A41%3A10Z&skoid=e52d5ed7-0657-4f62-bc12-7e5dbb260a96&sks=b&skt=2024-07-16T08%3A41%3A10Z&sktid=33e01921-4d64-4f8c-a055-5bdaffd5e33d&skv=2020-10-02&sp=r&spr=https&sr=b&sv=2020-10-02"
    }
  ]
}

返回结果中包含多个字段,解释如下:

  • created:此次图像生成任务的唯一 ID。
  • data:包含图像生成的结果信息。

其中 data 包含了模型生成图片的具体信息,url 是生成图像的链接,您可以通过图像链接查看生成的图像。

图片质量参数 quality

接下来,我们将探讨如何设置图像生成结果的详细参数。其中,图片质量参数 quality 有两个选项:standard 代表生成标准图像,hd 则表示生成的图像将具有更精细的细节及更高的一致性。

以下设置图片质量参数为 standard,具体设置展示如下:

同样,右侧也会生成相应的调用代码,您可以复制并直接运行,或点击「Try」按钮进行测试。

示例 Python 调用代码:

import requests

url = "https://api.acedata.cloud/openai/images/generations"

headers = {
    "accept": "application/json",
    "authorization": "Bearer {token}",
    "content-type": "application/json"
}

payload = {
    "model": "dall-e-3",
    "prompt": "A cute baby sea otter",
    "quality": "standard"
}

response = requests.post(url, json=payload, headers=headers)
print(response.text)

调用成功后,您将获得如下返回结果:

{
  "created": 1721636023,
  "data": [
    {
      "revised_prompt": "A cute baby sea otter is lying playfully on its back in the water, with its fur looking glossy and soft. One of its tiny paws is reaching out curiously, and it has an expression of pure joy and warmth on its face as it looks up to the sky. Its body is surrounded by bubbles from its playful twirling in the water. A gentle breeze is playing with its fur making it look more charming. The scene portrays the tranquility and charm of marine life.",
      "url": "https://dalleprodsec.blob.core.windows.net/private/images/a93ee5e7-3abd-4923-8d79-dc9ef126da46/generated_00.png?se=2024-07-23T08%3A13%3A55Z&sig=wTXGYvUOwUIkaB2CxjK9ww%2FHjS8OwYUWcYInXYKwcAM%3D&ske=2024-07-23T11%3A32%3A05Z&skoid=e52d5ed7-0657-4f62-bc12-7e5dbb260a96&sks=b&skt=2024-07-16T11%3A32%3A05Z&sktid=33e01921-4d64-4f8c-a055-5bdaffd5e33d&skv=2020-10-02&sp=r&spr=https&sr=b&sv=2020-10-02"
    }
  ]
}

返回结果与基本使用内容相符,生成的质量参数为 standard 的图片如图所示:

同样的操作下,仅需将图片质量参数设置为 hd,便可获得如下图片:

可见,hd 生成的图像具有更为精细的细节及更高一致性。

图片大小尺寸参数 size

此外,您还可以调整生成图像的尺寸大小。以下是设置图片尺寸为 1024 * 1024 的具体操作示例:

同样,右侧会自动生成调用代码,您可以轻松复制并直接运行,或点击「Try」按钮进行测试。

示例 Python 调用代码:

import requests

url = "https://api.acedata.cloud/openai/images/generations"

headers = {
    "accept": "application/json",
    "authorization": "Bearer {token}",
    "content-type": "application/json"
}

payload = {
    "model": "dall-e-3",
    "prompt": "A cute baby sea otter",
    "size": "1024x1024"
}

response = requests.post(url, json=payload, headers=headers)
print(response.text)

调用成功后,您将获得如下返回结果:

{
  "created": 1721636652,
  "data": [
    {
      "revised_prompt": "A delightful depiction of a baby sea otter. The small mammal is captured in its natural habitat in the ocean, floating on its back. It has thick brown fur that is sleek and wet from the sea water. Its eyes are closed as if it is enjoying a moment of deep relaxation. The water around it is calm, reflecting the peacefulness of the scene. The background should hint at a diverse marine ecosystem, with visible strands of kelp floating on the surface, suggesting the baby otter's preferred environment.",
      "url": "https://dalleprodsec.blob.core.windows.net/private/images/9d625ac6-fd2b-42a9-84a6-8c99eb357ccf/generated_00.png?se=2024-07-23T08%3A24%3A24Z&sig=AXtYXowEakGxfRp8LhC2DwqL%2F07LhEDW40oCP%2BdTO8s%3D&ske=2024-07-23T18%3A00%3A45Z&skoid=e52d5ed7-0657-4f62-bc12-7e5dbb260a96&sks=b&skt=2024-07-16T18%3A00%3A45Z&sktid=33e01921-4d64-4f8c-a055-5bdaffd5e33d&skv=2020-10-02&sp=r&spr=https&sr=b&sv=2020-10-02"
    }
  ]
}

返回的结果与基本使用的一致,生成的图像尺寸为 1024 * 1024 的图片如下:

通过同样的操作,只需将图片尺寸更改为 1792 * 1024,即可获得如下图所示的图片:

明显可以看出,图片的尺寸大小差异显著,此外,您还可以设置更多尺寸选项,详细信息请参考我们的官网文档。

图片风格参数 style

图片风格参数 style 涵盖两个选项:第一种 vivid 表示生成的图像更为生动,第二种 natural 则表示生成的图像更显自然。

以下是将图片风格参数设置为 vivid 的具体操作示例:

同了解,右侧也会生成调用代码供您复制,并可直接运行或点击「Try」按钮进行测试。

示例 Python 调用代码:

import requests

url = "https://api.acedata.cloud/openai/images/generations"

headers = {
    "accept": "application/json",
    "authorization": "Bearer {token}",
    "content-type": "application/json"
}

payload = {
    "model": "dall-e-3",
    "prompt": "A cute baby sea otter",
    "style": "vivid"
}

response = requests.post(url, json=payload, headers=headers)
print(response.text)

调用成功后,返回结果如下:

{
  "created": 1721637086,
  "data": [
    {
      "revised_prompt": "A baby sea otter with soft, shiny fur and sparkling eyes floating playfully on calm ocean waters. This adorable creature is trippingly frolicking amidst small, gentle waves under a bright, clear, sunny sky. The tranquility of the sea contrasts subtly with the delightful energy of this young otter. The critter gamely clings to a tiny piece of driftwood, its small paws adorably enveloping the floating object.",
      "url": "https://dalleprodsec.blob.core.windows.net/private/images/6e48f701-7fd3-4356-839e-a2f6f0fe82d9/generated_00.png?se=2024-07-23T08%3A31%3A37Z&sig=4percxqTbUR1j3BQmkhvj%2FAhHzInKI%2FqiTo1MP69coI%3D&ske=2024-07-27T10%3A39%3A55Z&skoid=e52d5ed7-0657-4f62-bc12-7e5dbb260a96&sks=b&skt=2024-07-20T10%3A39%3A55Z&sktid=33e01921-4d64-4f8c-a055-5bdaffd5e33d&skv=2020-10-02&sp=r&spr=https&sr=b&sv=2020-10-02"
    }
  ]
}

返回的结果与基本使用的内容一致,生成风格参数为 vivid 的图片如下所示:

同样,将图片风格参数更改为 natural,可获得如下图像:

显然,vivid 参数生成的图像更生动逼真。

图片链接格式参数 response_format

最后,图片链接格式参数 response_format 有两个选项:b64_json 对图片链接进行 Base64 编码,而 url 则为普通图片链接,可直接查看图片。

以下是将图片链接格式参数设置为 url 的具体操作示例:

同样,右侧会生成相应调用代码,您可以轻松复制并直接运行,或点击「Try」按钮进行测试。

示例 Python 调用代码:

import requests

url = "https://api.acedata.cloud/openai/images/generations"

headers = {
    "accept": "application/json",
    "authorization": "Bearer {token}",
    "content-type": "application/json"
}

payload = {
    "model": "dall-e-3",
    "prompt": "A cute baby sea otter",
    "response_format": "url"
}

response = requests.post(url, json=payload, headers=headers)
print(response.text)

调用成功后,您将获得以下返回结果:

{
  "created": 1721637575,
  "data": [
    {
      "revised_prompt": "A charming depiction of a baby sea otter. The otter is seen resting serenely on its back amidst the gentle, blue ocean waves. The baby otter's fur is an endearing mix of soft greyish brown shades, glinting subtly in the muted sunlight. Its small paws are touching, lifted slightly towards the sky as if playing with an unseen object. Its round, expressive eyes are wide in curiosity, sparking with life and innocence. Use a realistic style to evoke the otter's natural habitat and its adorably fluffy exterior.",
      "url": "https://dalleprodsec.blob.core.windows.net/private/images/87792c5f-8b6d-412e-81dd-f1a1baa19bd2/generated_00.png?se=2024-07-23T08%3A39%3A47Z&sig=zzRAn30TqIKHdLVqZPUUuSJdjCYpoJdaGU6BeoA76Jo%3D&ske=2024-07-23T13%3A32%3A13Z&skoid=e52d5ed7-0657-4f62-bc12-7e5dbb260a96&sks=b&skt=2024-07-16T13%3A32%3A13Z&sktid=33e01921-4d64-4f8c-a055-5bdaffd5e33d&skv=2020-10-02&sp=r&spr=https&sr=b&sv=2020-10-02"
    }
  ]
}

返回的结果与基本使用相符,当图片链接格式参数为 url 时生成的图片链接为 图片 URL,您可以直接访问,生成的图片显示如下:

类似的操作,如果将图片链接的格式参数设为 b64_json,你将得到 Base64 编码后的图片链接,具体结果显示如下:

{
  "created": 1721638071,
  "data": [
    {
      "b64_json": "iVBORw0..............v//AQEAAP4AAAD+AAADAQAAAwEEA/4D//8Q/Pbw64mKbVTFoQAAAABJRU5ErkJggg==",
      "revised_prompt": "A charming image of a young baby sea otter. The otter is gently floating on a calm blue sea, basking in the warm, golden rays of sunlight streaming down from a clear sky above. The otter's fur is a rich chocolate brown, and it looks incredibly soft and fluffy. The otter's eyes are bright and expressive, filled with childlike curiosity and joy. It has small, pricked ears and a button-like nose which adds to its overall cuteness. In the sea around it, twinkling droplets of water can be seen, pepped up by the sunlight, the sight is certainly a delightful one."
    }
  ]
}

错误处理

在调用 API 的过程中,若遇到错误,API 将返回相应的错误代码及信息。例如:

  • 400 token_mismatched:错误请求,可能由于缺少或无效的参数。
  • 400 api_not_implemented:错误请求,可能由于缺少或无效的参数。
  • 401 invalid_token:未授权,存在无效或缺失的授权令牌。
  • 429 too_many_requests:请求过多,您已超出速率限制。
  • 500 api_error:内部服务器错误,服务器出现问题。

错误响应示例

{
  "success": false,
  "error": {
    "code": "api_error",
    "message": "fetch failed"
  },
  "trace_id": "2cf86e86-22a4-46e1-ac2f-032c0f2a4e89"
}

结论

通过阅读本文档,您已了解如何轻松使用 OpenAI 图片生成 API 来接入官方 DALL-E 图像生成功能。希望本指南能够帮助您更好地使用并整合该 API。如若您有任何疑问,欢迎随时联系我们的技术支持团队。