本頁面說明如何使用外擴功能,將圖片內容擴展到較大的區域或不同尺寸的區域。
下列模型支援外繪功能:
外擴範例
外繪是以遮罩為基礎的編輯方法,可擴展基礎圖片的內容,以符合較大或不同大小的遮罩畫布。
圖片來源:Kari Shea 發表於 Unsplash。 |
|
|
查看「Imagen for Editing and Customization」模型資訊卡
事前準備
- 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
-
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
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). 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
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
為環境設定驗證機制。
選取這個頁面上的分頁,瞭解如何使用範例:
控制台
使用 Google Cloud 控制台存取 Google Cloud 服務和 API 時,無須設定驗證。
Python
如要在本機開發環境中使用本頁的 Python 範例,請安裝並初始化 gcloud CLI,然後使用您的使用者憑證設定應用程式預設憑證。
-
安裝 Google Cloud CLI。
-
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
-
如果您使用本機殼層,請為使用者帳戶建立本機驗證憑證:
gcloud auth application-default login
如果您使用 Cloud Shell,則不需要執行這項操作。
如果系統傳回驗證錯誤,且您使用外部識別資訊提供者 (IdP),請確認您已 使用聯合身分登入 gcloud CLI。
詳情請參閱 Google Cloud 驗證說明文件中的「 為本機開發環境設定 ADC」。
REST
如要在本機開發環境中使用本頁的 REST API 範例,請使用您提供給 gcloud CLI 的憑證。
安裝 Google Cloud CLI。
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
詳情請參閱 Google Cloud 驗證說明文件中的「使用 REST 進行驗證」。
-
擴展圖片內容
使用下列程式碼範例,即可擴展現有圖片的內容。
控制台
在 Google Cloud 控制台中,依序前往「Vertex AI」>「Vertex AI Studio」 頁面。
按一下「生成媒體」。
按一下「圖片」。
在「工作」清單中選取「外繪」。
從「模型」清單中選取要使用的 Imagen 模型。
在「輸入圖片」中,按一下「新增」,然後選取要上傳的編輯圖片。
在「提示」方塊中輸入提示,說明如何編輯圖片。
在顯示比例選單中選取顯示比例:
- crop_square「正方形」
- crop_16_9 16:9
- crop_9_16 9:16
- crop_landscape 4:3
- crop_portrait 3:4
- 全螢幕 自訂
在編輯工具列中,選取圖片的放置位置:
- 靠左對齊:
- 水平置中對齊:
- 靠右對齊:
- 頂端對齊:
- 垂直置中對齊:
- 底部對齊:
按一下「執行」。
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_ENTERPRISE=True
REST
如要進一步瞭解 Imagen API,請參閱下列內容:- 方法:
endpoints.predict VisionGenerativeModelInstanceVisionGenerativeModelParamsVisionGenerativeModelResult
使用任何要求資料之前,請先修改下列項目的值:
- PROJECT_ID:您的 Google Cloud 專案 ID。
- LOCATION:專案的區域。例如
us-central1、europe-west2或asia-northeast3。如需可用區域的清單,請參閱「Vertex AI 生成式 AI 服務地區」。使用地區 API 端點時,系統會根據端點網址中的區域,決定要求處理位置,並忽略資源路徑中發生衝突的LOCATION。 prompt:如要使用圖像外擴功能,可以提供空字串來建立編輯後的圖片。如果選擇提供提示,請使用遮蓋區域的說明,以獲得最佳結果。例如「a blue sky」,而非「insert a blue sky」。referenceType:ReferenceImage是提供圖片編輯額外背景資訊的圖片。編輯用途需要使用正常的 RGB 原始參考圖像 (REFERENCE_TYPE_RAW)。一個要求最多只能有一個原始參考圖像。輸出圖片的高度和寬度與原始參考圖像相同。如要使用遮罩編輯,必須提供遮罩參考圖像 (REFERENCE_TYPE_MASK)。如有原始 RAW 參考圖像,遮罩圖像的高度和寬度必須與原始 RAW 參考圖像相同。如果遮罩參照圖片為空白,且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"
}
]
}
限制
如果擴展後的圖片大小是原始圖片的 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 - 品質最高的文字轉圖像模型