テキスト プロンプトを使用して画像を編集する

このページでは、テキスト プロンプトのみを使用したマスクなしの編集について説明します。マスクなしの編集では、マスクを使用せずに画像を編集できます。この編集方法は、画像全体に関連する編集や、編集対象がユースケースにおいてそれほど重要でない場合に適しています。

マスクなし(画像全体)の編集の例

テキスト プロンプトを使用するだけで、(生成またはアップロードされた)ベース画像を編集できます。変更する領域を指定する必要はなく、更新は画像全体に適用されます(マスクなしの編集とも呼ばれます)。

マスクなしで編集するには、変更箇所を指示するのではなく、描写したい内容を説明するプロンプトを使用します。たとえば、既存の猫画像を犬に変更したいとします。「この猫を一匹の犬に変える」よりも「一匹の犬」というマスクなしの編集プロンプトの方が効果が高い可能性があります。同様に、「浜辺にいる一匹の猫」というプロンプトで生成した画像について考えてみましょう。この画像を変更するには、編集プロンプト「浜辺にいる一匹の犬」を使用します。

元の猫の画像と隣接して配置された編集後の犬の画像
元画像(左): Cédric VT 氏Unsplash より
編集後の画像(右): Vertex AI の Imagen を使用して、元のベース画像とプロンプト「一匹の犬」から生成された画像

編集とカスタマイズ用 Imagen のモデルカードを表示する

始める前に

  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. Verify 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. Verify 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 をインストールして初期化し、ユーザー認証情報を使用してアプリケーションのデフォルト認証情報を設定します。

      Google Cloud CLI をインストールします。

      外部 ID プロバイダ(IdP)を使用している場合は、まず連携 ID を使用して gcloud CLI にログインする必要があります。

      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 に指定した認証情報を使用します。

      Google Cloud CLI をインストールします。

      外部 ID プロバイダ(IdP)を使用している場合は、まず連携 ID を使用して gcloud CLI にログインする必要があります。

    詳細については、 Google Cloud 認証ドキュメントの REST を使用して認証するをご覧ください。

    マスクなしの編集を使用する

    マスク領域なしで画像全体を編集するには、次のサンプルを使用します。

    コンソール

    1. Google Cloud コンソールで、[Vertex AI] > [Media Studio] ページに移動します。

      [Media Studio] に移動
    2. 下部のタスクパネルで [画像を編集] をクリックします。
    3. [画像を編集] 画面に移動します。

      生成された画像を編集する

      1. テキスト プロンプトを使用して画像を生成します。
      2. 生成された画像をクリックします。
      3. [画像を編集] をクリックします。

      アップロードした画像を編集する

      1. [画像のアップロード] をクリックします。
      2. 編集するローカル ファイルを選択します。
    4. 画像を変更する新しいプロンプトを入力します。

    5. 省略可。任意のパラメータを変更します。
    6. [生成] をクリックします。

      テキストのみを使用した編集画像のサンプル
      Vertex AI の Imagen でプロンプト「抹茶ケーキ」を使用して編集した画像の [画像を編集] 画面。右側最上部に表示されているのが元の画像。元の画像のクレジット: David Holifield 氏Unsplash より( Google Cloud コンソールに表示)

    Python

    Vertex AI SDK for Python のインストールまたは更新の方法については、Vertex AI SDK for Python をインストールするをご覧ください。 詳細については、Python API リファレンス ドキュメントをご覧ください。

    このサンプルでは、load_from_file メソッドを使用して、変更対象となるベース Image としてローカル ファイルを参照します。ベース画像を指定したら、ImageGenerationModeledit_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 メソッドと URL:

    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 でエンコードした 2 つの予測オブジェクトを返します。
    {
      "predictions": [
        {
          "bytesBase64Encoded": "BASE64_IMG_BYTES",
          "mimeType": "image/png"
        },
        {
          "mimeType": "image/png",
          "bytesBase64Encoded": "BASE64_IMG_BYTES"
        }
      ]
    }
    

    次のステップ

    Imagen や Vertex AI のその他の生成 AI プロダクトに関する次の記事を読む。