图像#
给定提示和/或输入图像,模型将生成一个新图像。
1/0
## 让后面的代码不执行
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
Cell In[1], line 1
----> 1 1/0
2 ## 让后面的代码不执行
ZeroDivisionError: division by zero
创建映像#
https://api.openai.com/v1/images/generations
创建给定提示的图像。
import matplotlib.pyplot as plt
from imageio import imread
import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
result=openai.Image.create(
prompt="一个可爱的幼儿园小屁孩和他的狗狗",
n=2,
size="1024x1024"
)
url=result["data"][0]["url"]
img = imread(url)
plt.imshow(img)
plt.show()
C:\Users\user\AppData\Local\Temp\ipykernel_12884\2795090091.py:13: DeprecationWarning: Starting with ImageIO v3 the behavior of this function will switch to that of iio.v3.imread. To keep the current behavior (and make this warning dissapear) use `import imageio.v2 as imageio` or call `imageio.v2.imread` directly.
img = imread(url)
参数名称 |
类型 |
是否必须 |
默认值 |
描述 |
|---|---|---|---|---|
prompt |
string |
Required |
- |
期望图像的文本描述。最大长度为 1000 个字符。 |
n |
integer |
Optional |
1 |
要生成的图像数量。必须在 1 到 10 之间。 |
size |
string |
Optional |
1024x1024 |
生成图像的大小。必须是以下之一: 256x256、512x512 或 1024x1024。 |
response_format |
string |
Optional |
url |
返回生成图像的格式。必须是 url 或 b64_json。 |
user |
string |
Optional |
- |
表示您的最终用户的唯一标识符,可帮助 OpenAI 监控和检测滥用。 |
创建图像编辑#
https://api.openai.com/v1/images/edits
在给定原始图像和提示的情况下创建编辑或扩展的图像。
注意: image 要编辑的图像。必须是有效的 PNG 文件,小于 4MB,并且是正方形。如果未提供mask,则图像必须具有透明度的地方,该透明度的地方将用作mask。
mask 一个附加图像,其完全透明的区域(例如,alpha为零)指示应编辑的位置。必须是有效的 PNG 文件,小于 4MB,并且尺寸与image 相同。
response = openai.Image.create_edit(
image=open("image_edit_original1.png", "rb"),
mask=open("image_edit_mask1.png", "rb"),
prompt="A sunlit indoor lounge area with a pool containing a flamingo",
n=1,
size="1024x1024"
)
image_url = response['data'][0]['url']
image_url
'https://oaidalleapiprodscus.blob.core.windows.net/private/org-hkvxFr6AdoRUTccMVIZwwmYp/user-U0me5j49F33OHhOju2dVPwOH/img-jUujwCwiagUkq4QagJVefPTu.png?st=2023-04-20T05%3A40%3A26Z&se=2023-04-20T07%3A40%3A26Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-04-20T04%3A47%3A14Z&ske=2023-04-21T04%3A47%3A14Z&sks=b&skv=2021-08-06&sig=ANkXpLofT6MF7JY5Q9MZ6lY45BBd6odBzRGpPTi5YuY%3D'
参数名称 |
类型 |
是否必须 |
默认值 |
描述 |
|---|---|---|---|---|
image |
string |
Required |
- |
要编辑的图像。必须是有效的 PNG 文件,小于 4MB,且为正方形。如果未提供 mask,则图像必须具有透明度,该透明度将用作 mask。 |
mask |
string |
Optional |
- |
一个附加图像,其完全透明区域(例如 alpha 为零处)表示应编辑的位置。必须是有效的 PNG 文件,小于 4MB,并且与 image 具有相同的尺寸。 |
prompt |
string |
Required |
- |
期望图像的文本描述。最大长度为 1000 个字符。 |
n |
integer |
Optional |
1 |
要生成的图像数量。必须在 1 到 10 之间。 |
size |
string |
Optional |
1024x1024 |
生成图像的大小。必须是以下之一: 256x256、512x512 或 1024x1024。 |
response_format |
string |
Optional |
url |
返回生成图像的格式。必须是 url 或 b64_json。 |
user |
string |
Optional |
- |
表示您的最终用户的唯一标识符,可帮助 OpenAI 监控和检测滥用。 |
| Image | Mask | Output |
|---|---|---|
| ![]() |
![]() |

