OpenAI-ChatGPT最新官方接口《AI绘图》全网最详细中英文实用指南和教程,助你零基础快速轻松掌握全新技术(三)(附源码)

news2024/9/9 7:52:30

ChatGPT-AI绘图

  • Image generation Beta 图片生成
  • 前言
  • Introduction
  • Usage
    • Generations
    • Edits 编辑
  • Variations
  • Language-specific tips 特定语言提示
    • Python 语言
      • Using in-memory image data 使用内存中的图像数据
      • Operating on image data 操作图像数据
      • Error handling
    • Node.js 语言
      • Using in-memory image data 使用内存中的图像数据
      • Working with TypeScript 使用TypeScript
      • Error handling
  • 其它资料下载

在这里插入图片描述

Image generation Beta 图片生成

Learn how to generate or manipulate images with our DALL·E models
了解如何使用我们的DALL·E模型生成或操作图像

前言

ChatGPT不仅仅是一个强大的自然语言处理模型,其DALL·E模型还可以帮助用户快速生成多种多样的图像。用户可以根据文字快速生成图片,即可自动生成出特定的图片,大大提高了工作效率。对于多媒体行业从业者来说,DALL·E模型强大的图像处理能力,可以帮助用户节省大量时间,让用户更加专注地精心制作自己的作品,从而提高工作效率。

Introduction

The Images API provides three methods for interacting with images:
图像API提供了三种与图像交互的方法:

  1. Creating images from scratch based on a text prompt
    基于文本提示从头开始创建图像
  2. Creating edits of an existing image based on a new text prompt
    基于新文本提示创建现有图像的编辑
  3. Creating variations of an existing image
    创建现有图像的变体

This guide covers the basics of using these three API endpoints with useful code samples. To see them in action, check out our DALL·E preview app.
本指南涵盖了使用这三个API端点的基础知识和有用的代码示例。要查看它们的实际操作,请查看我们的DALL·E预览应用程序。

The Images API is in beta. During this time the API and models will evolve based on your feedback. To ensure all users can prototype comfortably, the default rate limit is 50 images per minute. You can learn more about rate limits in our rate limit guide.
图像API处于beta阶段。在此期间,API和模型将根据您的反馈进行改进。为了确保所有用户都能轻松地进行原型制作,默认速率限制为每分钟50张图像。您可以在我们的费率限制指南中了解有关费率限制的更多信息。

Usage

Generations

The image generations endpoint allows you to create an original image given a text prompt. Generated images can have a size of 256x256, 512x512, or 1024x1024 pixels. Smaller sizes are faster to generate. You can request 1-10 images at a time using the n parameter.
图像生成端点允许您在给出文本提示的情况下创建原始图像。生成的图像可以具有256 x256、512 x512或1024 x1024像素的大小。较小的尺寸生成速度更快。您可以使用n参数一次请求1-10个图像。

python代码如下:

response = openai.Image.create(
  prompt="a white siamese cat",
  n=1,
  size="1024x1024"
)
image_url = response['data'][0]['url']

node.js代码如下:

const response = await openai.createImage({
  prompt: "a white siamese cat",
  n: 1,
  size: "1024x1024",
});
image_url = response.data.data[0].url;

curl 代码如下:

curl https://api.openai.com/v1/images/generations \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
    "prompt": "a white siamese cat",
    "n": 1,
    "size": "1024x1024"
  }'

The more detailed the description, the more likely you are to get the result that you or your end user want. You can explore the examples in the DALL·E preview app for more prompting inspiration. Here’s a quick example:
描述越详细,就越有可能获得您或最终用户想要的结果。您可以在DALL·E预览应用程序中探索示例,以获得更多提示灵感。这里有一个简单的例子:

PROMPT 提示

a white siamese cat 白色暹罗猫

GENERATION 产生
在这里插入图片描述

PROMPT 提示

a close up, studio photographic portrait of a white siamese cat that looks curious, backlit ears
一个近距离,工作室摄影肖像的白色暹罗猫,看起来好奇,背光的耳朵

GENERATION 产生
在这里插入图片描述
Each image can be returned as either a URL or Base64 data, using the response_format parameter. URLs will expire after an hour.
使用response_format参数,每个图像都可以作为URL或Base64数据返回。URL将在一小时后过期。

Edits 编辑

The image edits endpoint allows you to edit and extend an image by uploading a mask. The transparent areas of the mask indicate where the image should be edited, and the prompt should describe the full new image, not just the erased area. This endpoint can enable experiences like the editor in our DALL·E preview app.
图像编辑端点允许您通过上传遮罩来编辑和扩展图像。蒙版的透明区域指示应该编辑图像的位置,提示应该描述完整的新图像,而不仅仅是擦除的区域。此端点可以实现类似于DALL·E预览应用中的编辑器的体验。

python代码如下:

response = openai.Image.create_edit(
  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']

node.js代码如下:

const response = await openai.createImageEdit(
  fs.createReadStream("sunlit_lounge.png"),
  fs.createReadStream("mask.png"),
  "A sunlit indoor lounge area with a pool containing a flamingo",
  1,
  "1024x1024"
);
image_url = response.data.data[0].url;

curl 代码如下:

curl https://api.openai.com/v1/images/edits \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -F image="@sunlit_lounge.png" \
  -F mask="@mask.png" \
  -F prompt="A sunlit indoor lounge area with a pool containing a flamingo" \
  -F n=1 \
  -F size="1024x1024"

在这里插入图片描述
The uploaded image and mask must both be square PNG images less than 4MB in size, and also must have the same dimensions as each other. The non-transparent areas of the mask are not used when generating the output, so they don’t necessarily need to match the original image like the example above.
上传的图像和蒙版必须都是小于4MB的方形PNG图像,并且彼此的尺寸必须相同。生成输出时不使用蒙版的非透明区域,因此它们不一定需要像上面的示例那样匹配原始图像。

Variations

The image variations endpoint allows you to generate a variation of a given image.
图像变体端点允许您生成给定图像的变体。

python代码如下:

response = openai.Image.create_variation(
  image=open("corgi_and_cat_paw.png", "rb"),
  n=1,
  size="1024x1024"
)
image_url = response['data'][0]['url']

node.js代码如下:

const response = await openai.createImageVariation(
  fs.createReadStream("corgi_and_cat_paw.png"),
  1,
  "1024x1024"
);
image_url = response.data.data[0].url;

curl 代码如下:

curl https://api.openai.com/v1/images/variations \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -F image='@corgi_and_cat_paw.png' \
  -F n=1 \
  -F size="1024x1024"

在这里插入图片描述

Language-specific tips 特定语言提示

Python 语言

Using in-memory image data 使用内存中的图像数据

The Python examples in the guide above use the open function to read image data from disk. In some cases, you may have your image data in memory instead. Here’s an example API call that uses image data stored in a BytesIO object:
上面的Python示例使用 open 函数从磁盘读取图像数据。在某些情况下,您可能会将图像数据保存在内存中。下面是一个使用存储在 BytesIO 对象中的图像数据的示例API调用:

from io import BytesIO

# This is the BytesIO object that contains your image data
byte_stream: BytesIO = [your image data]
byte_array = byte_stream.getvalue()
response = openai.Image.create_variation(
  image=byte_array,
  n=1,
  size="1024x1024"
)

Operating on image data 操作图像数据

It may be useful to perform operations on images before passing them to the API. Here’s an example that uses PIL to resize an image:
在将图像传递给API之前对图像执行操作可能是有用的。下面是一个使用 PIL 调整图像大小的示例:

from io import BytesIO
from PIL import Image

# Read the image file from disk and resize it
image = Image.open("image.png")
width, height = 256, 256
image = image.resize((width, height))

# Convert the image to a BytesIO object
byte_stream = BytesIO()
image.save(byte_stream, format='PNG')
byte_array = byte_stream.getvalue()

response = openai.Image.create_variation(
  image=byte_array,
  n=1,
  size="1024x1024"
)

Error handling

API requests can potentially return errors due to invalid inputs, rate limits, or other issues. These errors can be handled with a try...except statement, and the error details can be found in e.error:
API请求可能由于无效输入、速率限制或其他问题而返回错误。这些错误可以用 try...except 语句处理,错误细节可以在 e.error 中找到:

try:
  openai.Image.create_variation(
    open("image.png", "rb"),
    n=1,
    size="1024x1024"
  )
  print(response['data'][0]['url'])
except openai.error.OpenAIError as e:
  print(e.http_status)
  print(e.error)

Node.js 语言

Using in-memory image data 使用内存中的图像数据

The Node.js examples in the guide above use the fs module to read image data from disk. In some cases, you may have your image data in memory instead. Here’s an example API call that uses image data stored in a Node.js Buffer object:
上述指南中的Node.js示例使用 fs 模块从磁盘读取图像数据。在某些情况下,您可能会将图像数据保存在内存中。下面是一个使用存储在Node.js Buffer 对象中的图像数据的示例API调用:

// This is the Buffer object that contains your image data 这是包含图像数据的Buffer对象
const buffer = [your image data];
// Set a `name` that ends with .png so that the API knows it's a PNG image 设置一个以.png结尾的“名称”,这样API就知道这是一个PNG图像
buffer.name = "image.png";
const response = await openai.createImageVariation(
  buffer,
  1,
  "1024x1024"
);

Working with TypeScript 使用TypeScript

If you’re using TypeScript, you may encounter some quirks with image file arguments. Here’s an example of working around the type mismatch by explicitly casting the argument:
如果你使用TypeScript,你可能会遇到一些图像文件参数的怪异。下面是一个通过显式转换参数来解决类型不匹配的示例:

// Cast the ReadStream to `any` to appease the TypeScript compiler 将ReadStream转换为' any '以便TypeScript编译器使用
const response = await openai.createImageVariation(
  fs.createReadStream("image.png") as any,
  1,
  "1024x1024"
);

And here’s a similar example for in-memory image data:
下面是内存中图像数据的类似示例:

// This is the Buffer object that contains your image data 这是包含图像数据的Buffer对象
const buffer: Buffer = [your image data];
// Cast the buffer to `any` so that we can set the `name` property 将缓冲区转换为' any ',以便我们可以设置' name '属性
const file: any = buffer;
// Set a `name` that ends with .png so that the API knows it's a PNG image 设置一个以.png结尾的“名称”,这样API就知道这是一个PNG图像
file.name = "image.png";
const response = await openai.createImageVariation(
  file,
  1,
  "1024x1024"
);

Error handling

API requests can potentially return errors due to invalid inputs, rate limits, or other issues. These errors can be handled with a try...catch statement, and the error details can be found in either error.response or error.message:
API请求可能由于无效输入、速率限制或其他问题而返回错误。这些错误可以用 try...catch 语句处理,错误详细信息可以在error.responseerror.message 中找到:

try {
  const response = await openai.createImageVariation(
    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);
  }
}

其它资料下载

如果大家想继续了解人工智能相关学习路线和知识体系,欢迎大家翻阅我的另外一篇博客《重磅 | 完备的人工智能AI 学习——基础知识学习路线,所有资料免关注免套路直接网盘下载》
这篇博客参考了Github知名开源平台,AI技术平台以及相关领域专家:Datawhale,ApacheCN,AI有道和黄海广博士等约有近100G相关资料,希望能帮助到所有小伙伴们。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/433052.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

手把手教你使用Python调用 ChatGPT!支持http代理

手把手教你使用Python调用 ChatGPT!支持http代理 作者:虚坏叔叔 博客:https://xuhss.com 早餐店不会开到晚上,想吃的人早就来了!😄 前段时间OpenAI 开放了两个新模型的api接口,专门为聊天而生的…

《JavaEE初阶》多线程基础

《JavaEE初阶》多线程基础 文章目录《JavaEE初阶》多线程基础前言:多线程的概念简单创建线程并运行:简述Thread中run方法与start方法的区别创建线程的几种方法:探讨串行执行与并行执行的执行时间多线程的使用场景:Thread类简单介绍:构造方法:获取线程的常见属性:线程的常用方法…

Nacos 客户端服务发现源码分析-篇六

Nacos 客户端服务发现源码分析-篇六 🕐Nacos 客户端服务注册源码分析-篇一 🕑Nacos 客户端服务注册源码分析-篇二 🕒Nacos 客户端服务注册源码分析-篇三 🕓Nacos 服务端服务注册源码分析-篇四 🕔Nacos 服务端健康…

ChatGPT神器免费使用,告别昂贵低效工具

大家好,今天我要向大家介绍一款免费的ChatGPT使用网址,它可以让你轻松地使用ChatGPT进行AI创作!而且,这个网址还是免费的,不需要担心会有额外的费用。 ChatGPT是一种非常强大的AI技术,可以用于各种领域&…

《PyTorch 深度学习实践》第11讲 卷积神经网络(高级篇)

文章目录 1 Inception Module1.1 11卷积1.2 Inception模块结构1.3 完整代码 2 残差网络(Residual Network) 该专栏内容为对该视频的学习记录:【《PyTorch深度学习实践》完结合集】 专栏的全部代码、数据集和课件全放在个人GitHub了,欢迎自取。 1 Incept…

数据库实验 | 第3关:建立和调用存储函数

任务描述 本关任务: 销售数据库有顾客、销售单数据表 顾客gk数据表有会员号hyh、姓名name、性别sex、电话tel、部门dept字段 销售单xsd数据表有销售单号xsdh、会员号hyh、雇员号gyh、销售日期xsrq、应付款yfk、实际付款sjfk字段 任务要求 建立存储过程 gkjb(nf …

Obsidian中如何创作思维导图Mind-map

使用插件 obsidian-mind-map 1.直接在社区下载安装 设置快捷键或者在左侧竖形打开命令面板搜索关键字“mind”, 或者为了便于使用,设置快捷键,在设置-第三方插件中-选择快捷键 然后按下你想设置的快捷键就可以 我这里设置成了CtrlAltM ,M是…

小程序的组件化开发

目录: 1 小程序组件化思想 2 自定义组件的过程 3 组件样式实现细节 4 组件使用过程通信 5 组件插槽定义使用 6 Component构造器 在小程序里面需要创建组件的话需要在最外层建component包,然后在使用新建component来创建类似page的4个文件&#xff…

知乎版ChatGPT「知海图AI」加入国产大模型乱斗,称效果与GPT-4持平

“2023知乎发现大会”上,知乎创始人、董事长兼CEO周源和知乎合作人、CTO李大海共同宣布了知乎与面壁智能联合发布“知海图AI”中文大模型。 周源据介绍,知乎与面壁智能达成深度合作,共同开发中文大模型产品并推进应用落地。目前,知…

AUTOSAR存储服务之FEE换页策略介绍

概述 如下图是AUTOSAR Memory Stack的架构图,对于Memory Stack的介绍请参考AUTOSAR MemoryStack详细介绍_钢琴上的汽车软件的博客-CSDN博客 随着现在MCU携带的内置flash空间越来越大,从成本节省以及方便使用等方面考虑,在产品设计和开发过程中常用Flash EEPROM Emulation技…

CTFHub | eval执行

0x00 前言 CTFHub 专注网络安全、信息安全、白帽子技术的在线学习,实训平台。提供优质的赛事及学习服务,拥有完善的题目环境及配套 writeup ,降低 CTF 学习入门门槛,快速帮助选手成长,跟随主流比赛潮流。 0x01 题目描述…

手推广告论文(二)Wide Deep 推荐系统算法Wide Deep Learning for Recommender Systems

Wide & Deep Learning for Recommender Systems 论文地址https://arxiv.org/pdf/1606.07792.pdf 摘要 广义线性模型结合非线性特征转换,在处理具有大规模稀疏输入的回归和分类问题中已被广泛应用。通过一系列交叉积特征转换来记忆特征交互既有效又具有解释性…

【分布式事务 本地部署Seata服务】分布式事务框架Seata本地部署详细讲解

前言 这篇文章我会从0到1详细搭建分布式事务框架seata的使用,那么我们首先要先了解一下什么是分布式事务? 本篇文章是本地启动seata服务并且注册到nacos中,在SpringCloud中整合seata框架请转移下方连接 点我跳转SpringCloud整合seata教程&…

VMware ESXi 8.0U1 Unlocker OEM BIOS 集成网卡驱动和 NVMe 驱动 (集成驱动版)

发布 ESXi 8.0U1 集成驱动版,在个人电脑上运行企业级工作负载 请访问原文链接:https://sysin.org/blog/vmware-esxi-8-u1-sysin/,查看最新版。原创作品,转载请保留出处。 作者主页:sysin.org 2023-04-18, VMware vSp…

【真北直播笔记】董越:简明DevOps学习地图

缘起 真北敏捷社区的宗旨是:求知、连接。求知就是学习,家里没矿的话,学习是一个人最重要的动力之源。连接就是把人拉在一起,我们相信人与人的互动会带来美好的变化。今天的直播是把大家拉在一起学习,就是求知、连接。 …

【万人推荐】黑客成长技术清单

最近两天,在reddit安全板块和Twitter上有个GitHub项目很火,叫“Awesome Hacking”。 “Awesome Hacking”在reddit上有超过四百个赞,但管理员后来认为不适合该板块(Awesome类项目没有新的内容),给了“rejec…

十大排序算法之插入排序、希尔排序、选择排序

个人主页:平行线也会相交 欢迎 点赞👍 收藏✨ 留言✉ 加关注💓本文由 平行线也会相交 原创 收录于专栏【数据结构初阶(C实现)】 本篇主要讲解八大排序算法中的三种排序,分别是:插入排序、希尔排…

家用洗地机有什么优缺点?实用的洗地机分享

随着科技的不断发展,家庭清洁设备也在不断更新换代。现在市场上最常见的家用清洁设备包括洗地机、扫地机器人和吸尘器。这些设备各有优缺点,但在清洁效果、清洁范围和清洁方式等方面存在差异。洗地机是一种专业的清洁设备,它能够深度清洁地面…

SpringCloud之Gateway组件简介

网关的理解 网关类似于海关或者大门,出入都需要经过这个网关。别人不经过这个网关,永远也看不到里面的东西。可以在网关进行条件过滤,比如大门只有对应的钥匙才能入内。网关和大门一样,永远暴露在最外面 不使用网关 前端需要记住每…

Javascript进阶专题总结(函数、异步编程、设计模式)

函数式编程什么时候用 编程方法:函数式(js),面向对象(java,c),命令式 函数式(工具式操作) 优点:JavaScript种函数是一等公民,便于拆分组合可扩展性好,方便tree-shaking 缺点&…