本頁面說明外繪功能。外擴功能可讓您使用 Imagen 將圖片內容擴展至較大的區域,或擴展至不同尺寸的區域。
外繪範例
外繪是以遮罩為基礎的編輯方法,可擴展基礎圖片的內容,以符合較大或不同尺寸的遮罩畫布。
![]() 圖片來源:Kari Shea 發表於 Unsplash。 |
![]() |
![]() |
查看「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.
Java
如要在本機開發環境中使用本頁的 Java 範例,請安裝並初始化 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」。
Node.js
如要在本機開發環境中使用本頁的 Node.js 範例,請安裝並初始化 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」。
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」。
擴充圖片內容
使用下列程式碼範例,即可擴展現有圖片的內容。
Imagen 3
使用下列範例,透過 Imagen 3 模型傳送外繪要求。
控制台
-
在 Google Cloud 控制台中,前往「Vertex AI」>「Media Studio」 頁面。
- 按一下 [上傳]。在顯示的檔案對話方塊中,選取要上傳的檔案。
- 按一下「外繪」。
- 在「外擴」選單中,選取最終圖片的預先定義顯示比例,或按一下「自訂」,為最終圖片定義自訂尺寸。
-
在編輯工具列中,選取圖片的放置位置:
- 靠左對齊:
- 水平置中對齊:
- 靠右對齊:
- 頂端對齊:
- 垂直置中對齊:
- 底部對齊:
-
選用:在「參數」面板中,調整下列選項:
- 模型:要使用的 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
詳情請參閱「編輯圖片」API 參考資料。
使用任何要求資料之前,請先替換以下項目:
- PROJECT_ID:您的 Google Cloud 專案 ID。
- LOCATION:專案的區域。例如
us-central1
、europe-west2
或asia-northeast3
。如需可用區域的清單,請參閱「Vertex AI 的生成式 AI 服務地區」。 prompt
:如要使用圖像外繪功能,可以提供空字串來建立編輯後的圖像。如果選擇提供提示,請使用遮蓋區域的說明,以獲得最佳結果。例如「a blue sky」,而非「insert a blue sky」。referenceType
:ReferenceImage
是提供圖片編輯額外背景資訊的圖片。編輯用途需要正常的 RGB 原始參考圖像 (REFERENCE_TYPE_RAW
)。一個要求最多只能有一個原始參照圖片。 輸出圖片的高度和寬度與原始參考圖片相同。如要使用遮罩編輯功能,必須提供遮罩參考圖片 (REFERENCE_TYPE_MASK
)。如有原始參照圖片,遮罩圖片的高度和寬度必須與原始參照圖片相同。如果遮罩參照圖片為空白,且maskMode
未設為MASK_MODE_USER_PROVIDED
,系統會根據原始參照圖片計算遮罩。- B64_BASE_IMAGE:要編輯或放大畫質的基礎圖片。圖片必須指定為 base64 編碼的位元組字串。大小限制:10 MB。
- B64_OUTPAINTING_MASK:您要用來做為遮罩圖層的黑白圖片,可編輯原始圖片。遮罩的解析度應與輸入圖片相同。輸出圖片的解析度與輸入圖片相同。 這個遮罩圖片必須指定為 Base64 編碼的位元組字串。大小限制:10 MB。
- MASK_DILATION - float。遮罩擴張的圖片寬度百分比。建議將外繪值設為
0.03
。設定"dilation": 0.0
可能會在擴充點產生明顯的邊框,或造成白色邊框效果。 - EDIT_STEPS - 整數。基礎模型的取樣步驟數。如要使用外繪功能,請從步驟
35
開始。如果品質不符合規定,請增加步驟。 - 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": "", "referenceImages": [ { "referenceType": "REFERENCE_TYPE_RAW", "referenceId": 1, "referenceImage": { "bytesBase64Encoded": "B64_BASE_IMAGE" } }, { "referenceType": "REFERENCE_TYPE_MASK", "referenceId": 2, "referenceImage": { "bytesBase64Encoded": "B64_OUTPAINTING_MASK" }, "maskImageConfig": { "maskMode": "MASK_MODE_USER_PROVIDED", "dilation": MASK_DILATION } } ] } ], "parameters": { "editConfig": { "baseSteps": EDIT_STEPS }, "editMode": "EDIT_MODE_OUTPAINT", "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 2
使用下列範例,透過 Imagen 2 模型傳送外繪要求。
控制台
-
在 Google Cloud 控制台中,前往「Vertex AI」>「Media Studio」 頁面。
-
在下方工作面板中,按一下「編輯圖片」
。 -
按一下「上傳」,選取要編輯的本機產品圖片。
-
在編輯工具列中,按一下「外繪」圖示
。 -
選取最終圖片的預先定義顯示比例,或按一下「自訂」,為最終圖片定義自訂尺寸。
-
(選用步驟) 在編輯工具列中,選取要生成圖片的畫布中,原始圖片的水平位置 (
左側、 水平置中或 右側對齊) 和垂直位置 ( 頂端、 垂直置中或 底部對齊)。 -
(選用步驟) 在「Parameters」(參數) 面板中,調整「Number of results」(結果數量) 或其他參數。
-
按一下「生成」
。
Python
如要瞭解如何安裝或更新 Python 適用的 Vertex AI SDK,請參閱「安裝 Python 適用的 Vertex AI SDK」。 詳情請參閱 Python API 參考說明文件。
REST
使用任何要求資料之前,請先替換以下項目:
- PROJECT_ID:您的 Google Cloud 專案 ID。
- LOCATION:專案的區域。例如
us-central1
、europe-west2
或asia-northeast3
。如需可用區域的清單,請參閱「Vertex AI 的生成式 AI 服務地區」。 prompt
:如要使用圖像外繪功能,可以提供空字串來建立編輯後的圖像。- B64_BASE_IMAGE:要編輯或放大畫質的基礎圖片。圖片必須指定為 base64 編碼的位元組字串。大小限制:10 MB。
- B64_OUTPAINTING_MASK:您要用來做為遮罩圖層的黑白圖片,可編輯原始圖片。遮罩的解析度應與輸入圖片相同。輸出圖片的解析度與輸入圖片相同。 這個遮罩圖片必須指定為 Base64 編碼的位元組字串。大小限制:10 MB。
- EDIT_IMAGE_COUNT:編輯過的圖片數量。預設值: 4。
HTTP 方法和網址:
POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/imagegeneration@006:predict
JSON 要求主體:
{ "instances": [ { "prompt": "", "image": { "bytesBase64Encoded": "B64_BASE_IMAGE" }, "mask": { "image": { "bytesBase64Encoded": "B64_OUTPAINTING_MASK" } } } ], "parameters": { "sampleCount": EDIT_IMAGE_COUNT, "editConfig": { "editMode": "outpainting" } } }
如要傳送要求,請選擇以下其中一個選項:
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@006: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@006:predict" | Select-Object -Expand Content"sampleCount": 2
的範例回應。回應會傳回兩個預測物件,其中包含以 base64 編碼的生成圖片位元組。{ "predictions": [ { "bytesBase64Encoded": "BASE64_IMG_BYTES", "mimeType": "image/png" }, { "mimeType": "image/png", "bytesBase64Encoded": "BASE64_IMG_BYTES" } ] }
Java
在試用這個範例之前,請先按照Java使用用戶端程式庫的 Vertex AI 快速入門中的操作說明進行設定。 詳情請參閱 Vertex AI Java API 參考說明文件。
如要向 Vertex AI 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
在本範例中,您會將模型指定為
EndpointName
的一部分。EndpointName
會傳遞至predict
方法,該方法會在PredictionServiceClient
上呼叫。服務會傳回編輯後的圖片版本,然後儲存在本機。Node.js
在試用這個範例之前,請先按照Node.js使用用戶端程式庫的 Vertex AI 快速入門中的操作說明進行設定。 詳情請參閱 Vertex AI Node.js API 參考說明文件。
如要向 Vertex AI 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
在這個範例中,您會在PredictionServiceClient
上呼叫predict
方法。 這項服務會生成圖片,然後儲存在本機。限制
如果擴展後的圖片比原始圖片大 200% 以上,模型可能會產生扭曲的細節。建議的最佳做法是新增後續處理步驟,對外繪圖片執行 Alpha 混合。
以下是後續處理的範例程式碼:
parameters = { "editConfig": { "outpaintingConfig": { "blendingMode": "alpha-blending", "blendingFactor": 0.01, }, }, }
後續步驟
閱讀有關 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-16 (世界標準時間)。
-