OpenAI ChatGPT 图片生成API

发布时间 2023-11-16 21:45:02作者: ryan-Z

图像生成

了解如何使用 DALL·E 在 API 中。

想要在 ChatGPT 中生成图像?前往 chat.openai.com。

介绍

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

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

本指南通过有用的代码示例介绍了使用这三个 API 端点的基础知识。试试DALL·E 3,前往 ChatGPT。试试DALL·E 2、查看DALL·电子预览应用程序。

用法

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

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

您可以使用DALL·E 3(通过并行请求请求更多)或使用 DALL·带有 n 参数的 E 2。

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

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

更新的提示在数据响应对象的字段中可见。revised_prompt

示例 DALL·E 3生成

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

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

编辑 (仅限DALL· E2)

图像编辑端点也称为“修复”,允许您通过上传图像和蒙版来编辑或扩展图像,并指示应替换哪些区域。蒙版的透明区域指示应编辑图像的位置,提示应描述完整的新图像,而不仅仅是擦除的区域。此终结点可以启用类似 DALL·电子预览应用程序。

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
图像 面具 输出
提示:一个阳光明媚的室内休息区,有一个有火烈鸟的游泳池

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

变量 (仅限DALL·E2)

图像变量端点允许您生成给定图像的变体。

from openai import OpenAI
client = OpenAI()

response = client.images.create_variation(
  image=open("image_edit_original.png", "rb"),
  n=2,
  size="1024x1024"
)

image_url = response.data[0].url
图像 输出

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

内容审核

系统会根据我们的内容政策对提示和图片进行过滤,并在提示或图片被标记时返回错误。

特定于语言的提示

使用内存中图像数据

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

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,您可能会遇到一些带有图像文件参数的怪癖。下面是通过显式强制转换参数来解决类型不匹配的示例:

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();

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

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...catcherror.responseerror.message

import fs from "fs";
import OpenAI from "openai";

const openai = new OpenAI();

try {
    const response = openai.images.createVariation(
        fs.createReadStream("image.png"),
        1,
        "1024x1024"
    );
    console.log(response.data.data[0].url);
} catch (error) {
    if (error.response) {
        console.log(error.response.status);
        console.log(error.response.data);
    } else {
        console.log(error.message);
    }
}

原文引用链接

OpenAI ChatGPT生成图片API
OpenAI ChatGPT文字转语音API
OpenAI ChatGPT语音转文字API