在 Vertex AI 中生成及編輯圖像

有了 Vertex AI 中的 Gemini 和 Imagen,應用程式開發人員就能使用 Google 最先進的圖像生成式 AI 功能。應用程式開發人員可以運用 Vertex AI 上的 Gemini 和 Imagen,建構新一代 AI 產品,借助 AI 生成功能,在幾秒內將使用者的想像轉化為高品質的視覺素材。

試用圖片生成功能 (Vertex AI Studio)

在 Colab 中試用 Imagen

有了 Gemini 和 Imagen,你可以:

  • 僅使用文字提示詞生成新穎圖像 (文字轉圖像 AI 生成)。
  • 使用您定義的遮罩區域,編輯或擴展上傳或生成的圖像。
  • 放大現有、生成或編輯的圖片。

前一張圖片的提示

這些圖片是使用一般 Imagen 3 圖像生成模型 (imagen-3.0-generate-002) 和下列提示生成:

  1. 黏土動畫場景。中景:一位年長女性。她穿著飄逸的服裝,她站在綠意盎然的花園中,用橘色灑水壺澆花
  2. 此圖像採用數位單眼相機拍攝,並使用偏光濾鏡。土耳其卡帕多奇亞的獨特岩層地景,上空有兩顆熱氣球。這些氣球的顏色和圖案與下方風景的土色調形成美麗的對比。這張相片捕捉了享受這類體驗時的冒險感。
  3. 在野花叢生的田野中,一隻小藍鳥停在木製機器人伸出的手上,機器人身上爬滿開花的藤蔓,顯得飽經風霜,但仍靜靜佇立。數位卡通,暖色調和柔和線條。後方聳立著高聳的懸崖和瀑布。
  4. 畫面顯示一隻手拿著小小的鳥類黏土模型,另一隻手則拿著塑形工具,正在雕塑模型。你可以看到雕塑家的圍巾。他們的手上沾滿黏土粉塵。微距數位單眼相機圖片,凸顯紋理和工藝。
  5. 桌上擺著一個舊藍色玻璃花瓶,裡面插著一大束色彩繽紛的花朵。前方有一朵美麗的牡丹花,周圍環繞著各種其他花朵,例如玫瑰、百合、雛菊、蘭花、水果、漿果和綠葉。背景為深灰色。荷蘭黃金時代風格的油畫。
  6. 單一漫畫格:男孩和父親坐在草地上,凝視日落。一個對話泡泡從男孩的嘴巴指向,上面寫著:太陽會再次升起。柔和的 1990 年代末期風格

快速入門導覽課程:使用 Imagen 透過文字提示生成圖像

你只需要輸入描述性文字,就能生成新穎的圖像。下列範例顯示簡化的圖片生成案例,但您可以使用其他參數,根據需求調整生成的圖片。

  1. 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
  2. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. 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 the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  5. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

  7. 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 the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  8. 為環境設定驗證機制。

    選取這個頁面上的分頁,瞭解如何使用範例:

    Python

    如要在本機開發環境中使用本頁的 Python 範例,請安裝並初始化 gcloud CLI,然後使用您的使用者憑證設定應用程式預設憑證。

    1. 安裝 Google Cloud CLI。

    2. 若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI

    3. 如果您使用本機殼層,請為使用者帳戶建立本機驗證憑證:

      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 進行驗證」。

  9. 使用下列範例生成圖片:

    Python

    from google import genai
    from google.genai.types import GenerateImagesConfig
    
    client = genai.Client()
    
    # TODO(developer): Update and un-comment below line
    # output_file = "output-image.png"
    
    image = client.models.generate_images(
        model="imagen-4.0-generate-001",
        prompt="A dog reading a newspaper",
        config=GenerateImagesConfig(
            image_size="2K",
        ),
    )
    
    image.generated_images[0].image.save(output_file)
    
    print(f"Created output image using {len(image.generated_images[0].image.image_bytes)} bytes")
    # Example response:
    # Created output image using 1234567 bytes
    

    REST

    1. 設定環境變數:

      export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT # Replace with your Google Cloud project
      export GOOGLE_CLOUD_LOCATION=us-central1 # Replace with the appropriate location for your project
                  
    2. 執行以下指令:

      curl -X POST \
      -H "Authorization: Bearer $(gcloud auth print-access-token)" \
      -H "Content-Type: application/json; charset=utf-8" \
      "https://${GOOGLE_CLOUD_LOCATION}-aiplatform.googleapis.com/v1/projects/${GOOGLE_CLOUD_PROJECT}/locations/${GOOGLE_CLOUD_LOCATION}/publishers/google/models/imagen-4.0-generate-preview-05-20:predict" -d \
      $'{
        "instances": [
          {
            "prompt": "a cat reading a book"
          }
        ],
        "parameters": {
          "sampleCount": 1
        }
      }'
                  

      模型會傳回 base64 圖片位元組物件。

    詳情請參閱 Imagen Generate images API

何時該使用 Vertex AI 的 Gemini 或 Imagen

除了使用 Gemini 內建的圖像生成功能,你也可以存取專門的圖像生成模型 Imagen

屬性 Gemini Image Imagen 4
優勢 預設建議。
無與倫比的彈性、情境理解能力,以及免遮罩編輯功能。獨家支援多輪對話式修圖。
最佳品質/延遲
可用性 正式發布版 正式發布版
延遲時間 更高。進階功能需要更多運算資源。 低。針對近乎即時的效能進行最佳化。
費用 以權杖為基礎的定價。 適合處理特定工作,具備成本效益。
建議工作
  • 生成文字和圖片夾雜的內容,讓文字和圖片完美融合。
  • 只要輸入一段提示詞,即可結合多張圖片的創意元素。
  • 對圖片進行精細編輯、使用語言指令修改個別元素,以及反覆處理圖片。
  • 將某張圖片的特定設計或紋理套用至另一張圖片,同時保留原始主體形狀和細節。
  • 圖片品質、擬真度、藝術細節或特定風格 (例如印象派和動漫) 是首要考量。
  • 融入品牌、風格,或生成標誌和產品設計。
  • 生成進階拼字或排版。

建議您先使用 Gemini 生成圖片。如要處理進階用途或需要最佳圖片品質,請選擇 Imagen 4 Ultra。

模型版本

你可以使用多種圖片生成模型。詳情請參閱 Gemini 模型Imagen 模型

試試更多範例

如需使用 Imagen 的完整 Jupyter 筆記本教學課程清單,請參閱 Vertex AI 的生成式 AI 教戰手冊

後續步驟

請使用下列連結查看功能說明文件。

圖片來源:所有圖片皆使用 Vertex AI 的 Imagen 生成。