如何使用OpenAI gpt-image-1 API生成和编辑图像

如何使用OpenAI gpt-image-1 API生成和编辑图像

OpenAI 的 ChatGPT 上一次推出图像生成模型时,迅速在互联网上引起了热议。人们被它创造吉卜力风格肖像的能力所吸引,将个人记忆变成了动画艺术品。现在,ChatGPT 更进一步,推出了新的原生多模态模型“gpt-image-1”,该模型可直接在 ChatGPT 中生成图像,现在可通过 API 使用。在本文中,我们将探讨 OpenAI 的 gpt-image-1 模型的主要功能,以及如何将其用于图像生成和编辑。

什么是gpt-image-1?

gpt-image-1 是 OpenAI 最新推出的最先进的多模态语言模型。它能够生成高质量的图像,同时将现实世界的知识融入到视觉内容中,因此脱颖而出。虽然 gpt-image-1 因其强大的性能而受到推荐,但图像 API 还支持 DALL-E 2 和 DALL-E 3 等其他专业模型。

gpt-image-1

Source: OpenAI

图像应用程序 API 提供三个关键终端,每个终端都是为特定任务设计的:

  • 生成:使用文本提示从头开始创建图像。
  • 编辑:使用新提示对现有图像进行部分或全部修改。
  • 变体 :生成现有图像的变体(仅适用于 DALL-E 2)。

图像应用程序 API

Source: OpenAI

gpt-image-1的主要功能

gpt-image-1 具有几个主要功能:

  • 高保真图像:生成详细、准确的视觉效果。
  • 多样化的视觉风格:支持从写实到抽象的各种美学风格。
  • 精确的图像编辑:可对生成的图像进行有针对性的修改。
  • 丰富的世界知识:根据上下文准确理解复杂的提示。
  • 一致的文本渲染:可靠地渲染图像中的文本。

可用性

OpenAI API 使用户能够使用 GPT 图像或 DALL-E 模型从文本提示生成和编辑图像。目前,图像生成只能通过图像 API 访问,但响应 API 的支持正在积极开发中。

要了解有关 gpt-image-1 的更多信息,请点击此处

gpt-image-1定价

在深入了解如何使用和部署该模型之前,有必要先了解其定价,以确保在预算允许的情况下有效使用。

gpt-image-1 模型按token计价,文本token和图像token的价格不同:

  • 文本输入令牌(提示) :每 100 万个token 5 美元
  • 图像输入令牌(上传的图像) :每 100 万个token 10 美元
  • 图像输出令牌(生成图像):每 100 万个token 40 美元

在实际应用中,这大致相当于

  • ~0.02 美元(低质量正方形图像)
  • ~0.07 美元(中等质量的正方形图像)
  • ~0.07 美元(高质量正方形图像)

有关图像质量和分辨率的更详细定价,请参阅此处的官方定价页面。

gpt-image-1定价

Source: OpenAI

注:该模型通过首先创建专门的图像token来生成图像。因此,延迟和总体成本都取决于所用标记的数量。更大的图像尺寸和更高的质量设置需要更多token,从而增加了时间和成本。

如何访问gpt-image-1?

生成 gpt-image-1 的 API 密钥:

  1. 登录 OpenAI 平台
  2. 转到项目 > API 密钥
  3. 验证您的账户

为此,请首先访问:https://platform.openai.com/settings/organization/general 。然后,点击“Verify Organization”开始验证过程。这与任何 KYC 验证都非常相似,根据国家的不同,您会被要求上传身份证照片,然后通过自拍进行验证。

您可以查看 Open AI 提供的文档,以便更好地了解验证流程。

gpt-image-1:实际应用

最后,我们来看看如何使用 gpt-image-1 API 生成图像。

我们将使用图像生成端点根据文本提示创建图像。默认情况下,API 只返回一张图片,但我们可以设置 n 参数,以便在一次请求中同时生成多张图片。

在运行主代码之前,我们需要先运行安装和设置环境的代码。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
!pip install openai
import os
os.environ['OPENAI_API_KEY'] = "<your-openai-api-key>"
!pip install openai import os os.environ['OPENAI_API_KEY'] = "<your-openai-api-key>"
!pip install openai
import os
os.environ['OPENAI_API_KEY'] = "<your-openai-api-key>"

使用gpt-image-1生成图像

现在,让我们尝试使用这个新模型生成图像。

输入代码:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
from openai import OpenAI
import base64
client = OpenAI()
prompt = """
A serene, peaceful park scene where humans and friendly robots are enjoying the
day together - some are walking, others are playing games or sitting on benches
under trees. The atmosphere is warm and harmonious, with soft sunlight filtering
through the leaves.
"""
result = client.images.generate(
model="gpt-image-1",
prompt=prompt
)
image_base64 = result.data[0].b64_json
image_bytes = base64.b64decode(image_base64)
# Save the image to a file
with open("utter_bliss.png", "wb") as f:
f.write(image_bytes)
from openai import OpenAI import base64 client = OpenAI() prompt = """ A serene, peaceful park scene where humans and friendly robots are enjoying the day together - some are walking, others are playing games or sitting on benches under trees. The atmosphere is warm and harmonious, with soft sunlight filtering through the leaves. """ result = client.images.generate( model="gpt-image-1", prompt=prompt ) image_base64 = result.data[0].b64_json image_bytes = base64.b64decode(image_base64) # Save the image to a file with open("utter_bliss.png", "wb") as f: f.write(image_bytes)
from openai import OpenAI
import base64
client = OpenAI()
prompt = """
A serene, peaceful park scene where humans and friendly robots are enjoying the
day together - some are walking, others are playing games or sitting on benches
under trees. The atmosphere is warm and harmonious, with soft sunlight filtering
through the leaves.
"""
result = client.images.generate(
model="gpt-image-1",
prompt=prompt
)
image_base64 = result.data[0].b64_json
image_bytes = base64.b64decode(image_base64)
# Save the image to a file
with open("utter_bliss.png", "wb") as f:
f.write(image_bytes)

输出:

使用gpt-image-1生成图像

Editing Images Using gpt-image-1

gpt-image-1 offers a number of image editing options. The image edits endpoint lets us:

  • Edit existing images
  • Generate new images using other images as a reference
  • Edit parts of an image by uploading an image and mask indicating which areas should be replaced (a process known as inpainting)

Editing an Image Using a Mask

Let’s try editing an image using a mask. We’ll upload an image and provide a mask to specify which parts of it should be edited.

使用gpt-image-1编辑图像

gpt-image-1 提供了许多图像编辑选项。通过图像编辑端点,我们可以

  • 编辑现有图像
  • 以其他图像为参考生成新图像
  • 通过上传图像和遮罩来编辑图像的部分内容,并指明应替换的区域(这一过程称为 “内绘”)。

使用蒙版编辑图像

让我们尝试使用蒙版编辑图像。我们将上传一张图片,并提供一个蒙版来指定要编辑的部分。

使用蒙版编辑图像

遮罩的透明区域将根据提示进行替换,而彩色区域将保持不变。

现在,让我要求模型将埃隆-马斯克添加到我上传的图片中。

输入代码:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
from openai import OpenAI
client = OpenAI()
result = client.images.edit(
model="gpt-image-1",
image=open("/content/analytics_vidhya_1024.png", "rb"),
mask=open("/content/mask_alpha_1024.png", "rb"),
prompt="Elon Musk standing in front of Company Logo"
)
image_base64 = result.data[0].b64_json
image_bytes = base64.b64decode(image_base64)
# Save the image to a file
with open("Elon_AV.png", "wb") as f:
f.write(image_bytes)
from openai import OpenAI client = OpenAI() result = client.images.edit( model="gpt-image-1", image=open("/content/analytics_vidhya_1024.png", "rb"), mask=open("/content/mask_alpha_1024.png", "rb"), prompt="Elon Musk standing in front of Company Logo" ) image_base64 = result.data[0].b64_json image_bytes = base64.b64decode(image_base64) # Save the image to a file with open("Elon_AV.png", "wb") as f: f.write(image_bytes)
from openai import OpenAI
client = OpenAI()
result = client.images.edit(
model="gpt-image-1",
image=open("/content/analytics_vidhya_1024.png", "rb"),
mask=open("/content/mask_alpha_1024.png", "rb"),
prompt="Elon Musk standing in front of Company Logo"
)
image_base64 = result.data[0].b64_json
image_bytes = base64.b64decode(image_base64)
# Save the image to a file
with open("Elon_AV.png", "wb") as f:
f.write(image_bytes)

输出:

编辑图像并添加元素

使用gpt-image-1编辑图像时的注意事项:

  • 要编辑的图像和相应的遮罩必须是相同的格式和尺寸,大小都应小于 25MB。
  • 您给出的提示可以用来描述整个新图像,而不仅仅是被编辑的部分。
  • 如果提供多张输入图像,遮罩将只应用于第一张图像。
  • 遮罩图像必须包含 alpha 通道。如果使用图像编辑工具创建遮罩,请确保保存时启用了阿尔法通道。
  • 如果您有黑白图像,可以使用程序添加阿尔法通道,并将其转换为有效的掩码,如下所示:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
from PIL import Image
from io import BytesIO
# 1. Load your black & white mask as a grayscale image
mask = Image.open("/content/analytics_vidhya_masked.jpeg").convert("L")
# 2. Convert it to RGBA so it has space for an alpha channel
mask_rgba = mask.convert("RGBA")
# 3. Then use the mask itself to fill that alpha channel
mask_rgba.putalpha(mask)
# 4. Convert the mask into bytes
buf = BytesIO()
mask_rgba.save(buf, format="PNG")
mask_bytes = buf.getvalue()
# 5. Save the resulting file
img_path_mask_alpha = "mask_alpha.png"
with open(img_path_mask_alpha, "wb") as f:
f.write(mask_bytes)
from PIL import Image from io import BytesIO # 1. Load your black & white mask as a grayscale image mask = Image.open("/content/analytics_vidhya_masked.jpeg").convert("L") # 2. Convert it to RGBA so it has space for an alpha channel mask_rgba = mask.convert("RGBA") # 3. Then use the mask itself to fill that alpha channel mask_rgba.putalpha(mask) # 4. Convert the mask into bytes buf = BytesIO() mask_rgba.save(buf, format="PNG") mask_bytes = buf.getvalue() # 5. Save the resulting file img_path_mask_alpha = "mask_alpha.png" with open(img_path_mask_alpha, "wb") as f: f.write(mask_bytes)
from PIL import Image
from io import BytesIO
# 1. Load your black & white mask as a grayscale image
mask = Image.open("/content/analytics_vidhya_masked.jpeg").convert("L")
# 2. Convert it to RGBA so it has space for an alpha channel
mask_rgba = mask.convert("RGBA")
# 3. Then use the mask itself to fill that alpha channel
mask_rgba.putalpha(mask)
# 4. Convert the mask into bytes
buf = BytesIO()
mask_rgba.save(buf, format="PNG")
mask_bytes = buf.getvalue()
# 5. Save the resulting file
img_path_mask_alpha = "mask_alpha.png"
with open(img_path_mask_alpha, "wb") as f:
f.write(mask_bytes)

使用模型的最佳实践

以下是使用 gpt-image-1 生成或编辑图像时应遵循的一些提示和最佳实践。

  1. 您可以通过设置大小、质量、文件格式、压缩级别以及背景是否透明等选项,自定义图像的外观。这些设置可帮助你控制最终输出,以满足你的特定需求。
  2. 要想获得更快的效果,请选择正方形图像(1024×1024)和标准质量。还可以选择纵向(1536×1024)或横向(1024×1536)格式。质量可设置为低、中或高,如果未指定,大小和质量默认为自动。
  3. 请注意,图像 API 返回的是 base64 编码的图像数据。默认格式为 png,但我们也可以请求 jpeg 或 webp 格式。
  4. 如果使用 jpeg 或 webp 格式,还可以指定 output_compression 参数来控制压缩级别(0-100%)。例如,output_compression=50 将把图像压缩 50%。

gpt-image-1的应用

从创意设计和电子商务到教育、企业软件和游戏,gpt-image-1 的应用范围非常广泛。

  • 游戏:内容创建、精灵面具、动态背景、角色生成、概念艺术
  • 创意工具:艺术品生成、风格转换、设计原型、视觉叙事
  • 教育:视觉辅助工具、历史再现、互动学习内容、概念可视化
  • 企业软件:幻灯片视觉效果、报告插图、数据到图像生成、品牌资产
  • 广告与营销:活动视觉效果、社交媒体图形、本地化内容创作
  • 医疗保健:医学插图、病人扫描视觉效果、用于模型训练的合成图像数据
  • 建筑与房地产:室内模型、室外效果图、布局预览、装修创意
  • 娱乐与媒体:场景概念、宣传材料、数字加倍

GPT-IMAGE-1 的局限性

GPT-4o 图像模型是一种功能强大、用途广泛的图像生成工具,但仍有一些局限性需要注意:

  • 延迟:较复杂的提示可能需要长达 2 分钟的处理时间。
  • 文本渲染:虽然明显优于 DALL-E 模型,但该模型在文本精确对齐和清晰度方面仍可能面临挑战。
  • 一致性:虽然该模型可以生成视觉上一致的图像,但有时可能难以在多个图像中保持重复出现的角色或品牌元素的一致性。
  • 构图控制:即使改进了遵循指令的能力,该模型也不一定能在结构化或布局敏感的设计中准确放置元素。

模型比较

以下是 OpenAI 的 gpt-image-1 与流行的 DALL-E 模型的比较:

模型 终端 特征
DALL·E 2 生成,编辑和变体 成本更低,支持并发请求,包括内绘功能
DALL·E 3 仅生成 与 DALL-E 2 相比,分辨率更高,图像质量更好
gpt-image-1 生成、编辑(即将支持Responses API) 出色的指导、详细的编辑、真实世界意识

小结

OpenAI 的 gpt-image-1 展示了强大的图像生成功能,支持创建、编辑和变体,所有这些都只需简单的文本提示。gpt-image-1 具有内置的尺寸、质量、格式等自定义选项,甚至还具有内绘功能,为开发人员提供了对所需输出的完全、透明的控制。有些人可能会担心这种技术会取代人类的创造力,但必须指出的是,这种工具的目的是提高人类的创造力,成为艺术家的有用工具。我们必须找到适当的平衡点,既让这类工具帮助我们创新,又不剥夺真实的人类作品的价值。

评论留言