如何使用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 具有內建的尺寸、質量、格式等自定義選項,甚至還具有內繪功能,為開發人員提供了對所需輸出的完全、透明的控制。有些人可能會擔心這種技術會取代人類的創造力,但必須指出的是,這種工具的目的是提高人類的創造力,成為藝術家的有用工具。我們必須找到適當的平衡點,既讓這類工具幫助我們創新,又不剝奪真實的人類作品的價值。

評論留言