图像生成

了解如何使用 DALL·E 生成或处理图像。

介绍

图片 API 提供了三种与图片交互的方法:source

  1. 基于文本提示从头开始创建图像 (DALL·E 3 和 DALL·E 2)
  2. 通过让模型替换预先存在的图像的某些区域,根据新的文本提示 (DALL·仅限 E 2)
  3. 创建现有图像的变体 (DALL·仅限 E 2)

本指南通过有用的生成码示例介绍了使用这三个 API 终端节点的基础知识。要试用 DALL·E 3,前往 ChatGPT。source

用法

生成

的 image generations 端点允许您在给定文本提示的情况下创建原始图像。使用 DALL·E 3 中,图像的大小可以是 1024x1024、1024x1792 或 1792x1024 像素。source

默认情况下,图像在standard质量,但在使用 DALL·E 3 您可以设置quality: "hd"以增强细节。方形、标准质量的图像生成速度最快。source

您可以使用 DALL·E 3(通过发出并行请求请求更多)或使用 DALL·E 2 和 n 参数source

生成图像
1
2
3
4
5
6
7
8
9
10
11
12
from openai import OpenAI
client = OpenAI()

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 中,模型现在接受提供的默认提示,并出于安全原因自动重写它,并添加更多细节(更详细的提示通常会导致更高质量的图像)。source

虽然目前无法禁用此功能,但您可以通过将以下内容添加到提示符中,使用提示来使输出更接近您请求的图像:I NEED to test how the tool works with extremely simple prompts. DO NOT add any detail, just use it AS-IS:.source

更新后的提示显示在revised_prompt字段。source

示例 DALL·E 3 生成

提示生成
一张白色暹罗猫的照片。

每个图像都可以使用 response_format 参数作为 URL 或 Base64 数据返回。URL 将在一小时后过期。source

编辑 (DALL·仅限 E 2)

图像编辑端点也称为“修复”,它允许您通过上传图像和蒙版来编辑或扩展图像,以指示应替换哪些区域。蒙版的透明区域指示应编辑图像的位置,提示符应描述完整的新图像,而不仅仅是擦除的区域。此终端节点可以启用 DALL·ChatGPT Plus 中的 E 图像编辑。source

编辑图像
1
2
3
4
5
6
7
8
9
10
11
12
from openai import OpenAI
client = OpenAI()

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
图像遮罩输出

source

提示:阳光充足的室内休息区,带有一个游泳池,里面有一只火烈鸟source

source

上传的图片和蒙版必须是小于 4MB 的方形 PNG 图片,并且彼此的尺寸必须相同。生成输出时不使用蒙版的非透明区域,因此它们不一定需要与上面的示例相同的原始图像匹配。source

变体 (DALL·仅限 E 2)

的 image variations 端点允许您生成给定图像的变体。source

生成图像变体
1
2
3
4
5
6
7
8
9
10
11
from openai import OpenAI
client = OpenAI()

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
图像输出

与编辑端点类似,输入图像必须是大小小于 4MB 的方形 PNG 图像。source

内容审核

提示和图像根据我们的内容策略进行筛选,在标记提示或图像时返回错误。source

特定于语言的提示

使用内存中图像数据

上面指南中的 Node.js 个示例都使用fs模块从磁盘读取图像数据。在某些情况下,您可能将图像数据保存在内存中。下面是一个使用存储在 Node.js 中的图像数据的 API 调用示例Buffer对象:source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import OpenAI from "openai";

const openai = new OpenAI();

// This is the Buffer object that contains your image data
const buffer = [your image data];

// Set a `name` that ends with .png so that the API knows it's a PNG image
buffer.name = "image.png";

async function main() {
  const image = await openai.images.createVariation({ model: "dall-e-2", image: buffer, n: 1, size: "1024x1024" });
  console.log(image.data);
}
main();

使用 TypeScript

如果你使用的是 TypeScript,你可能会遇到一些带有图像文件参数的怪癖。下面是一个通过显式强制转换参数来解决类型不匹配的示例:source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import fs from "fs";
import OpenAI from "openai";

const openai = new OpenAI();

async function main() {
  // Cast the ReadStream to `any` to appease the TypeScript compiler
  const image = await openai.images.createVariation({
    image: fs.createReadStream("image.png") as any,
  });

  console.log(image.data);
}
main();

下面是内存中图像数据的类似示例:source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import fs from "fs";
import OpenAI from "openai";

const openai = new OpenAI();

// This is the Buffer object that contains your image data
const buffer: Buffer = [your image data];

// Cast the buffer to `any` so that we can set the `name` property
const file: any = buffer;

// Set a `name` that ends with .png so that the API knows it's a PNG image
file.name = "image.png";

async function main() {
  const image = await openai.images.createVariation({
    file,
    1,
    "1024x1024"
  });
  console.log(image.data);
}
main();

错误处理

API 请求可能会因输入无效、速率限制或其他问题而返回错误。这些错误可以使用try...catch语句,错误详细信息可以在error.responseerror.message:source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import fs from "fs";
import OpenAI from "openai";

const openai = new OpenAI();

async function main() {
    try {
        const image = await openai.images.createVariation({
            image: fs.createReadStream("image.png"),
            n: 1,
            size: "1024x1024",
        });
        console.log(image.data);
    } catch (error) {
        if (error.response) {
            console.log(error.response.status);
            console.log(error.response.data);
        } else {
            console.log(error.message);
        }
    }
}

main();