您可以使用 Vertex AI 的 Imagen 放大圖片,增加圖片大小,同時維持圖片品質。
imagen-4.0-upscale-preview 支援在「預覽」中放大圖片。
事前準備
- 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.
REST
如要在本機開發環境中使用本頁的 REST API 範例,請使用您提供給 gcloud CLI 的憑證。
安裝 Google Cloud CLI。 完成後,執行下列指令來初始化 Google Cloud CLI:
gcloud init若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
詳情請參閱 Google Cloud 驗證說明文件中的「使用 REST 進行驗證」。
提高圖片解析度
控制台
在 Google Cloud 控制台中,依序前往「Vertex AI」>「Vertex AI Studio」>「Generate media」(生成媒體) 頁面。
按一下「Imagen」。系統隨即會顯示「媒體遊樂場」頁面。
按一下「上傳圖片」,然後選取要上傳的圖片。
依序點選「匯出圖片」和「同時放大圖片」。
選擇縮放比例,然後按一下「匯出」。
系統會自動在放大後的圖片中加入數位浮水印。使用 Google Cloud 控制台放大圖片時,無法停用數位浮水印。
REST
使用任何要求資料之前,請先替換以下項目:
-
REGION:專案所在的區域。如要進一步瞭解支援的區域,請參閱「Vertex AI 的生成式 AI 服務地區」。 -
PROJECT_ID:您的 Google Cloud 專案 ID。 -
BASE64_SUBJECT_IMAGE:主體圖片的 Base64 編碼圖片。 -
ADD_WATERMARK:選用的布林值。設為true可啟用浮水印圖片,設為false則可停用浮水印圖片。預設值為true。 -
GCS_IMAGE_PATH: 圖片檔案的 Cloud Storage 路徑。 -
GCS_OUTPUT_PATH:儲存所產生輸出的 Cloud Storage 路徑。 -
OUTPUT_MIMETYPE:選用字串,用於定義圖片的輸出檔案類型。系統接受的值為"image/png"或"image/jpeg"。預設值為"image/png"。 -
COMPRESSION_QUALITY:選用整數值,指定模型為 JPEG 圖片保留的詳細程度。系統接受的值範圍為0-100。值越高,壓縮程度就越高。預設值為75。 -
UPSCALE_FACTOR: 放大圖片的縮放比例係數。套用放大係數後,圖片的最終輸出解析度不得超過 1700 萬像素。可接受的值如下:"x2""x3""x4"
HTTP 方法和網址:
POST https://REGION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/publishers/google/models/imagen-4.0-upscale-preview:predict
JSON 要求內文:
{ "instances": [ { "prompt": "Upscale the image", "image": { // use one of the following to specify the image to upscale "bytesBase64Encoded": "BASE64_SUBJECT_IMAGE" "gcsUri": "GCS_IMAGE_PATH" // end of base image input options }, } ], "parameters": { "mode": "upscale", "storageUri": "GCS_OUTPUT_PATH", "outputOptions": { "mimeType": "OUTPUT_MIMETYPE", "compressionQuality": COMPRESSION_QUALITY }, "upscaleConfig": { "upscaleFactor": "UPSCALE_FACTOR" } } }如要傳送要求,請選擇以下其中一個選項:
要求會傳回圖片物件。在本例中,系統會以 Base64 編碼圖片的形式,將圖片物件做為預測物件傳回。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-4.0-upscale-preview: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-4.0-upscale-preview:predict" | Select-Object -Expand Content{ "predictions": [ { "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-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 (世界標準時間)。"],[],[]]