本頁說明如何將物件插入圖片,這個程序也稱為修復。使用 Vertex AI 的 Imagen 時,您可以指定遮罩區域,將物件插入圖片中。您可以自行提供遮罩,也可以讓 Imagen 為您生成遮罩。
內容插入範例
使用修復功能時,您可以提供基礎圖片、圖片遮罩和文字提示,在現有圖片中加入內容。
輸入
| 要編輯的基礎圖片* | 使用 Google Cloud 控制台中的工具遮蓋指定區域 | 文字提示 |
|---|---|---|
|
|
草莓 |
在 Google Cloud 控制台中指定遮罩區域後的輸出內容
|
|
|
查看「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.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Vertex AI API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. -
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Vertex AI API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. -
為環境設定驗證機制。
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,然後使用使用者憑證設定應用程式預設憑證。
安裝 Google Cloud CLI。
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
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 的憑證。
安裝 Google Cloud CLI。
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
詳情請參閱 Google Cloud 驗證說明文件中的「使用 REST 進行驗證」。
插入時定義遮罩區域
使用下列範例,透過 Imagen 3 模型傳送修復要求。
控制台
1. 在 Google Cloud 控制台中,依序前往「Vertex AI」>「Media Studio」頁面。<a href="https://console.cloud.google.com/vertex-ai/studio/media/generate;tab=image" class="button button-primary" target="console" track-name="consoleLink" track-type="task">Go to Media Studio</a>按一下「上傳」,然後選取要上傳的檔案。
按一下「Inpaint」(修復)。
執行下列其中一個步驟:
上傳自己的遮罩:
在電腦上建立遮罩。
按一下「上傳遮罩」,然後選取要上傳的遮罩。
定義遮罩:在編輯工具列中,使用遮罩工具 (方塊、筆刷或 masked_transitions 反轉工具),指定要新增內容的區域。
選用步驟:在「Parameters」(參數) 面板中調整下列選項:
模型:要使用的 Imagen 模型。
結果數量:要生成的結果數量。
負面提示:描述要從生成的圖片中排除的內容。
在提示欄位中輸入提示,修改圖片。
按一下「生成」。
Python
安裝
pip install --upgrade google-genai
詳情請參閱 SDK 參考說明文件。
設定環境變數,透過 Vertex AI 使用 Gen AI SDK:
# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values # with appropriate values for your project. export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT export GOOGLE_CLOUD_LOCATION=us-central1 export GOOGLE_GENAI_USE_VERTEXAI=True
REST
使用任何要求資料之前,請先替換以下項目:
-
REGION:專案所在的區域。如要進一步瞭解支援的區域,請參閱「Vertex AI 的生成式 AI 服務地區」。 -
PROJECT_ID:您的 Google Cloud 專案 ID。 -
TEXT_PROMPT: 選用。文字提示,用來引導模型生成圖片。為獲得最佳效果,請使用遮蓋區域的說明,並避免使用單字提示。舉例來說,請使用「一隻可愛的柯基犬」,而不是「柯基犬」。 -
B64_BASE_IMAGE: 經過編輯的圖片 (大小上限為 10 MB,並採用 Base64 編碼)。如要進一步瞭解 base64 編碼,請參閱「Base64 編碼及解碼檔案」。 -
B64_MASK_IMAGE: 大小為 10 MB 以下的黑白遮罩圖片,採用 Base64 編碼。 -
MASK_DILATION: 選用。介於 0 和 1 之間的浮點值 (含 0 和 1),代表遮罩要擴大的圖片寬度百分比。使用dilation有助於補償不精確的遮罩。建議將這個值設為0.01。 -
EDIT_STEPS: 選用。代表取樣步數的整數。值越高,影像品質越好;值越低,延遲時間越短。建議您先嘗試
35步驟。如果品質不符合需求,建議將值調高至上限75。 -
SAMPLE_COUNT: 選用。整數,用來描述要生成的圖片數量。可接受的值範圍為1至4,預設值為4。
HTTP 方法和網址:
POST https://REGION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/publishers/google/models/imagen-3.0-capability-001:predict
JSON 要求內文:
{ "instances": [ { "prompt": "TEXT_PROMPT", "referenceImages": [ { "referenceType": "REFERENCE_TYPE_RAW", "referenceId": 1, "referenceImage": { "bytesBase64Encoded": "B64_BASE_IMAGE" } }, { "referenceType": "REFERENCE_TYPE_MASK", "referenceImage": { "bytesBase64Encoded": "B64_MASK_IMAGE" }, "maskImageConfig": { "maskMode": "MASK_MODE_USER_PROVIDED", "dilation": MASK_DILATION } } ] } ], "parameters": { "editConfig": { "baseSteps": EDIT_STEPS }, "editMode": "EDIT_MODE_INPAINT_INSERTION", "sampleCount": SAMPLE_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://REGION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/publishers/google/models/imagen-3.0-capability-001: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://REGION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/publishers/google/models/imagen-3.0-capability-001:predict" | Select-Object -Expand Content"sampleCount": 2。回應會傳回兩個預測物件,其中包含以 base64 編碼產生的圖片位元組。{ "predictions": [ { "bytesBase64Encoded": "BASE64_IMG_BYTES", "mimeType": "image/png" }, { "mimeType": "image/png", "bytesBase64Encoded": "BASE64_IMG_BYTES" } ] }插入時自動偵測遮罩
使用下列樣本指定修復功能,插入內容。在這些範例中,您會指定基礎圖片和文字提示。Imagen 會自動偵測並建立遮罩區域,以修改基礎圖片。
控制台
1. 在 Google Cloud 控制台中,依序前往「Vertex AI」>「Media Studio」頁面。<a href="https://console.cloud.google.com/vertex-ai/studio/media/generate;tab=image" class="button button-primary" target="console" track-name="consoleLink" track-type="task">Go to Media Studio</a>按一下「上傳」,然後選取要上傳的檔案。
按一下「Inpaint」(修復)。
在編輯工具列中,按一下 background_replace「擷取遮罩」。
選取其中一個遮罩擷取選項:
背景元素:偵測背景元素,並在周圍建立遮罩。
前景元素:偵測前景物件,並在周圍建立遮罩。
background_replace People: 偵測人物並建立周圍的遮罩。
選用步驟:在「Parameters」(參數) 面板中,調整下列選項:
模型:要使用的 Imagen 模型。
結果數量:要生成的結果數量。
負面提示:描述要從生成的圖片中排除的內容。
在提示欄位中輸入提示,修改圖片。
按一下「傳送」生成。
Python
安裝
pip install --upgrade google-genai
詳情請參閱 SDK 參考說明文件。
設定環境變數,透過 Vertex AI 使用 Gen AI SDK:
# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values # with appropriate values for your project. export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT export GOOGLE_CLOUD_LOCATION=us-central1 export GOOGLE_GENAI_USE_VERTEXAI=True
REST
使用任何要求資料之前,請先替換以下項目:
- PROJECT_ID:您的 Google Cloud 專案 ID。
- LOCATION:專案的區域。例如
us-central1、europe-west2或asia-northeast3。如需可用區域的清單,請參閱「Vertex AI 的生成式 AI 服務地區」。 - TEXT_PROMPT:文字提示會引導模型生成圖片。使用提示進行局部修復插入時,請盡量詳細描述遮罩區域,以獲得最佳效果。避免使用單字提示。舉例來說,請使用「一隻可愛的柯基犬」,不要使用「柯基犬」。
- B64_BASE_IMAGE:要編輯或放大畫質的基礎圖片。圖片必須指定為 base64 編碼的位元組字串。大小限制:10 MB。
- MASK_MODE - 一個字串,用於設定模型使用的自動遮罩建立類型。可用值:
MASK_MODE_BACKGROUND:使用背景區隔功能自動生成遮罩。MASK_MODE_FOREGROUND:使用前景區隔自動生成遮罩。MASK_MODE_SEMANTIC:根據您在maskImageConfig.maskClasses陣列中指定的區隔類別,使用語意區隔自動生成遮罩。例如:"maskImageConfig": { "maskMode": "MASK_MODE_SEMANTIC", "maskClasses": [175, 176], // bicycle, car "dilation": 0.01 }
- MASK_DILATION - float。這個遮罩要擴張的圖片寬度百分比。建議使用
0.01的值,補償不完美的輸入遮罩。 - EDIT_STEPS - 整數。基礎模型的取樣步驟數。如要插入修復內容,請從
35步驟開始。如果品質不符合需求,請將步數增加至上限75。增加步驟也會增加要求延遲時間。 - EDIT_IMAGE_COUNT - 編輯過的圖片數量。接受的整數值:1 到 4。 預設值為 4。
HTTP 方法和網址:
POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/imagen-3.0-capability-001:predict
JSON 要求內文:
{ "instances": [ { "prompt": "TEXT_PROMPT", "referenceImages": [ { "referenceType": "REFERENCE_TYPE_RAW", "referenceId": 1, "referenceImage": { "bytesBase64Encoded": "B64_BASE_IMAGE" } }, { "referenceType": "REFERENCE_TYPE_MASK", "referenceId": 2, "maskImageConfig": { "maskMode": "MASK_MODE", "dilation": MASK_DILATION } } ] } ], "parameters": { "editConfig": { "baseSteps": EDIT_STEPS }, "editMode": "EDIT_MODE_INPAINT_INSERTION", "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/imagen-3.0-capability-001: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/imagen-3.0-capability-001:predict" | Select-Object -Expand Content"sampleCount": 2。回應會傳回兩個預測物件,其中包含以 base64 編碼產生的圖片位元組。{ "predictions": [ { "bytesBase64Encoded": "BASE64_IMG_BYTES", "mimeType": "image/png" }, { "mimeType": "image/png", "bytesBase64Encoded": "BASE64_IMG_BYTES" } ] }限制
以下各節說明 Imagen 物件移除功能的限制。
修改過的像素
模型會以自己的解析度 (例如 1024x1024) 生成像素,這可能與輸入圖片的解析度不同。也就是說,生成的圖片可能會出現原始圖片沒有的細微變化。
如要完美保留圖片,建議使用遮罩將生成的圖片與輸入圖片混合。一般來說,如果輸入圖片解析度為 2K 以上,就必須混合生成圖片和輸入圖片。
插入限制
雖然插入的物體通常會與基礎圖片的風格相符,但某些關鍵字可能會產生卡通風格的結果,而非相片擬真輸出內容。
舉例來說,如果提示是「黃色長頸鹿」,由於長頸鹿的自然顏色是棕色和黃褐色,因此系統可能會生成卡通風格的圖片。生成色彩不自然的擬真圖像可能很困難。
後續步驟
閱讀有關 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-12-04 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-12-04 (世界標準時間)。"],[],[]]