本頁說明如何只使用文字提示,進行無遮罩編輯。不使用遮罩編輯圖片。如果編輯內容與整張圖片有關,或編輯位置對您的用途來說不那麼重要,就很適合使用這種編輯方式。
無遮蓋 (整張圖片) 編輯範例
您只需輸入文字提示,即可編輯生成或上傳的基礎圖片。您不必指定要修改的區域,系統會更新整張圖像 (也稱為無遮蓋編輯)。
如要進行無遮罩編輯,請使用提示描述您想看到的內容,而非變更指示。舉例來說,假設您想將現有的貓咪圖片換成狗狗圖片,「一隻狗」這類不含遮罩的編輯提示,可能比「將貓換成狗」更有效。同樣地,假設您使用「海灘上的貓」提示生成圖片,如要變更這張圖片,請使用編輯提示「海灘上的狗」。
查看「Imagen for Editing and Customization」模型資訊卡
事前準備
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Vertex AI API.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Vertex AI API.
-
為環境設定驗證方法。
Select the tab for how you plan to use the samples on this page:
Console
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
Python
如要在本機開發環境中使用本頁的 Python 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
詳情請參閱 Google Cloud 驗證說明文件中的「 為本機開發環境設定 ADC」。
REST
如要在本機開發環境中使用本頁的 REST API 範例,請使用您提供給 gcloud CLI 的憑證。
After installing the Google Cloud CLI, initialize it by running the following command:
gcloud init
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
詳情請參閱 Google Cloud 驗證說明文件中的「Authenticate for using REST」。
不使用遮罩編輯
使用下列範例編輯整張圖片,不需遮罩區域。
控制台
-
在 Google Cloud 控制台中,前往「Vertex AI」>「Media Studio」 頁面。
前往媒體工作室 - 在下方工作面板中,按一下「編輯圖片」。
前往「編輯圖片」
畫面。編輯生成的圖片
- 使用文字提示生成圖像
- 按一下生成的圖片。
- 按一下「編輯圖片」。
編輯上傳的圖片
- 按一下「上傳圖片」。
- 選取要編輯的本機檔案。
輸入新提示詞來修改圖片。
- (選用步驟) 修改任何參數。
點按「生成」。
以「抹茶蛋糕」為提示,使用 Vertex AI 的 Imagen 編輯圖片後,編輯圖片的檢視畫面。原始圖片顯示在右上角。原始圖片出處: David Holifield (顯示在 Google Cloud 管理中心) 於 Unsplash
Python
如要瞭解如何安裝或更新 Python 適用的 Vertex AI SDK,請參閱「安裝 Python 適用的 Vertex AI SDK」。 詳情請參閱 Python API 參考說明文件。
在本範例中,您會使用
load_from_file
方法,將本機檔案參照為要修改的基礎Image
。指定基本圖片後,請在ImageGenerationModel
上使用edit_image
方法,並將編輯後的圖片儲存至本機。然後,您可以在筆記本中使用show()
方法,顯示編輯後的圖片。REST
使用任何要求資料之前,請先替換以下項目:
- PROJECT_ID:您的 Google Cloud 專案 ID。
- LOCATION:專案的區域。例如
us-central1
、europe-west2
或asia-northeast3
。如需可用區域的清單,請參閱「Vertex AI 的生成式 AI 服務地區」。 - TEXT_PROMPT:文字提示,引導模型生成圖片。生成和編輯時都必須填寫這個欄位。
- B64_BASE_IMAGE:要編輯或放大畫質的基礎圖片。圖片必須指定為 base64 編碼的位元組字串。大小限制:10 MB。
- EDIT_IMAGE_COUNT:編輯過的圖片數量。預設值: 4。
HTTP 方法和網址:
POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/imagegeneration@002:predict
JSON 要求主體:
{ "instances": [ { "prompt": "TEXT_PROMPT", "image": { "bytesBase64Encoded": "B64_BASE_IMAGE" } } ], "parameters": { "sampleCount": EDIT_IMAGE_COUNT } }
如要傳送要求,請選擇以下其中一個選項:
curl
將要求主體儲存在名為
request.json
的檔案中,然後執行下列指令:curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/imagegeneration@002:predict"PowerShell
將要求主體儲存在名為
request.json
的檔案中,然後執行下列指令:$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/imagegeneration@002:predict" | Select-Object -Expand Content"sampleCount": 2
的範例回應。回應會傳回兩個預測物件,其中包含以 base64 編碼的生成圖片位元組。{ "predictions": [ { "bytesBase64Encoded": "BASE64_IMG_BYTES", "mimeType": "image/png" }, { "mimeType": "image/png", "bytesBase64Encoded": "BASE64_IMG_BYTES" } ] }
後續步驟
閱讀有關 Imagen 和其他 Vertex AI 生成式 AI 產品的文章:
- 開發人員指南:開始使用 Vertex AI 中的 Imagen 3
- 專為創作者打造的全新生成式媒體模型和工具
- Gemini 新功能:自訂 Gem 和 Imagen 3 圖像生成功能
- Google DeepMind:Imagen 3 - 品質最高的文字轉圖像模型
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-09 (世界標準時間)。
-