使用文字提示來編輯圖像

本頁說明如何只使用文字提示,進行無遮罩編輯。不使用遮罩編輯圖片。如果編輯內容與整張圖片有關,或編輯位置對您的用途來說不那麼重要,就很適合使用這種編輯方式。

無遮蓋 (整張圖片) 編輯範例

您只需輸入文字提示,即可編輯生成或上傳的基礎圖片。您不必指定要修改的區域,系統會更新整張圖像 (也稱為無遮蓋編輯)。

如要進行無遮罩編輯,請使用提示描述您想看到的內容,而非變更指示。舉例來說,假設您想將現有的貓咪圖片換成狗狗圖片,「一隻狗」這類不含遮罩的編輯提示,可能比「將貓換成狗」更有效。同樣地,假設您使用「海灘上的貓」提示生成圖片,如要變更這張圖片,請使用編輯提示「海灘上的狗」

原始貓咪圖片旁邊是編輯過的狗狗圖片
原始圖片 (左側):Cédric VT,取自 Unsplash
編輯後的圖片 (右):使用 Vertex AI 的 Imagen 生成的圖片,原始基礎圖片和提示詞為「a dog」(一隻狗)。

查看「Imagen for Editing and Customization」模型資訊卡

事前準備

  1. 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.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Vertex AI API.

    Enable the API

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Enable the Vertex AI API.

    Enable the API

  8. 為環境設定驗證方法。

    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,然後使用使用者憑證設定應用程式預設憑證。

    1. Install the Google Cloud CLI.

    2. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

    3. To initialize the gcloud CLI, run the following command:

      gcloud init
    4. 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」。

    不使用遮罩編輯

    使用下列範例編輯整張圖片,不需遮罩區域。

    控制台

    1. 在 Google Cloud 控制台中,前往「Vertex AI」>「Media Studio」 頁面。

      前往媒體工作室
    2. 在下方工作面板中,按一下「編輯圖片」
    3. 前往「編輯圖片」畫面。

      編輯生成的圖片

      1. 使用文字提示生成圖像
      2. 按一下生成的圖片。
      3. 按一下「編輯圖片」

      編輯上傳的圖片

      1. 按一下「上傳圖片」
      2. 選取要編輯的本機檔案。
    4. 輸入新提示詞來修改圖片。

    5. (選用步驟) 修改任何參數
    6. 點按「生成」

      僅使用文字編輯圖像的範例
      以「抹茶蛋糕」為提示,使用 Vertex AI 的 Imagen 編輯圖片後,編輯圖片的檢視畫面。原始圖片顯示在右上角。原始圖片出處David Holifield (顯示在 Google Cloud 管理中心) 於 Unsplash

    Python

    如要瞭解如何安裝或更新 Python 適用的 Vertex AI SDK,請參閱「安裝 Python 適用的 Vertex AI SDK」。 詳情請參閱 Python API 參考說明文件

    在本範例中,您會使用 load_from_file 方法,將本機檔案參照為要修改的基礎 Image。指定基本圖片後,請在 ImageGenerationModel 上使用 edit_image 方法,並將編輯後的圖片儲存至本機。然後,您可以在筆記本中使用 show() 方法,顯示編輯後的圖片。

    
    import vertexai
    from vertexai.preview.vision_models import Image, ImageGenerationModel
    
    # TODO(developer): Update and un-comment below lines
    # PROJECT_ID = "your-project-id"
    # input_file = "input-image.png"
    # output_file = "output-image.png"
    # prompt = "" # The text prompt describing what you want to see.
    
    vertexai.init(project=PROJECT_ID, location="us-central1")
    
    model = ImageGenerationModel.from_pretrained("imagegeneration@002")
    base_img = Image.load_from_file(location=input_file)
    
    images = model.edit_image(
        base_image=base_img,
        prompt=prompt,
        # Optional parameters
        seed=1,
        # Controls the strength of the prompt.
        # -- 0-9 (low strength), 10-20 (medium strength), 21+ (high strength)
        guidance_scale=21,
        number_of_images=1,
    )
    
    images[0].save(location=output_file, include_generation_parameters=False)
    
    # Optional. View the edited image in a notebook.
    # images[0].show()
    
    print(f"Created output image using {len(images[0]._image_bytes)} bytes")
    # Example response:
    # Created output image using 1234567 bytes
    

    REST

    使用任何要求資料之前,請先替換以下項目:

    • PROJECT_ID:您的 Google Cloud 專案 ID
    • LOCATION:專案的區域。例如 us-central1europe-west2asia-northeast3。如需可用區域的清單,請參閱「Vertex AI 的生成式 AI 服務地區」。
    • TEXT_PROMPT:文字提示,引導模型生成圖片。生成和編輯時都必須填寫這個欄位。
    • B64_BASE_IMAGE:要編輯或放大畫質的基礎圖片。圖片必須指定為 base64 編碼的位元組字串。大小限制:10 MB。
    • EDIT_IMAGE_COUNT:編輯過的圖片數量。預設值: 4。

    HTTP 方法和網址:

    POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/imagegeneration@002:predict

    JSON 要求主體:

    {
      "instances": [
        {
          "prompt": "TEXT_PROMPT",
          "image": {
            "bytesBase64Encoded": "B64_BASE_IMAGE"
          }
        }
      ],
      "parameters": {
        "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/imagegeneration@002: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@002:predict" | Select-Object -Expand Content
    以下是要求 "sampleCount": 2 的範例回應。回應會傳回兩個預測物件,其中包含以 base64 編碼的生成圖片位元組。
    {
      "predictions": [
        {
          "bytesBase64Encoded": "BASE64_IMG_BYTES",
          "mimeType": "image/png"
        },
        {
          "mimeType": "image/png",
          "bytesBase64Encoded": "BASE64_IMG_BYTES"
        }
      ]
    }
    

    後續步驟

    閱讀有關 Imagen 和其他 Vertex AI 生成式 AI 產品的文章: