Gemini で画像を生成する

Gemini 2.5 Flash Image では、テキストや画像など、複数のモダリティでのレスポンス生成がサポートされています。

画像生成

Gemini 2.5 Flash Image(gemini-2.5-flash-image)では、テキストに加えて画像を生成する機能がサポートされています。これにより、Gemini の機能が次のように拡張されます。

  • 自然言語での会話を通じて画像を繰り返し生成し、整合性とコンテキストを維持しながら画像を調整します。
  • 高品質の長いテキスト レンダリングで画像を生成します。
  • インターリーブされたテキストと画像の出力を生成します。たとえば、1 つのターンにテキストと画像を含むブログ投稿などです。これまでは、これを行うには複数のモデルを連携させる必要がありました。
  • Gemini の世界中の知識と推論機能を使用して画像を生成します。

この一般公開の試験運用版リリースでは、Gemini 2.5 Flash Image で 1,024 ピクセルの画像を生成できます。また、人物画像の生成もサポートしており、より柔軟で制限の少ないユーザー エクスペリエンスを提供するように更新された安全フィルタが含まれています。

次のモダリティと機能がサポートされています。

  • テキストから画像

    • プロンプトの例: 「背景に花火があるエッフェル塔の画像を生成してください」。
  • テキストから画像(テキスト レンダリング)

    • プロンプトの例: 「この巨大なテキスト投影が建物の正面にマッピングされた大きな建物のシネマティック フォトを生成してください。「Gemini 2.5 では長文のテキストを生成できるようになりました」」
  • テキスト画像変換とテキスト(インターリーブ)

    • プロンプトの例: 「パエリアのレシピをイラスト付きで生成してください。レシピの生成時に、テキストと一緒に表示する画像を作成します」。
    • プロンプトの例: 「3D アニメーション スタイルの犬の物語を生成してください。各シーンの画像を生成してください」
  • 画像とテキスト画像変換とテキスト(インターリーブ)

    • プロンプトの例:(家具付きの部屋の画像を提示して)「この部屋に合いそうなソファの色には他にどんなものがありますか?画像を更新してください」。
  • 言語 / 地域に対応した画像生成

    • プロンプトの例: 「朝食の画像を生成してください。」

ベスト プラクティス

画像生成の結果を改善するには、次のベスト プラクティスを参考にしてください。

  • 具体的に記述する: 詳細に説明するほど、より細かく制御できます。たとえば、「ファンタジー アーマー」ではなく、「銀の葉の模様がエッチングされた、精巧なエルフのプレート アーマー。ハイカラーとハヤブサの翼の形をした肩当て付き」のようにします。

  • コンテキストと意図を提供する: モデルがコンテキストを理解できるように、画像の目的を説明します。たとえば、「高級感のあるミニマリストのスキンケア ブランドのロゴを作成して」は、「ロゴを作成して」よりも効果的です。

  • 反復処理して精度を高める: 最初の試行で完璧な画像が生成されるとは限りません。フォローアップ プロンプトを使用して、小さな変更を加えます(例: 「照明を暖かくする」、「キャラクターの表情をより真剣なものにする」)。

  • 手順を使用する: 複雑なシーンの場合は、リクエストを手順に分割します。たとえば、「まず、夜明けの静かで霧のかかった森の背景を作成します。次に、前景に苔むした古代の石の祭壇を追加します。最後に、祭壇の上に光る剣を 1 本置きます。」

  • 望むものを説明する: 「車がない」と言うのではなく、「交通の気配がない空っぽの寂れた通り」のように、望むシーンを肯定的に説明します。

  • カメラを制御する: カメラのビューを操作します。構図を説明する際には、「広角ショット」、「マクロショット」、「ローアングル」などの写真や映画の用語を使用します。

  • 画像のプロンプト: 「~の画像を作成して」や「~の画像を生成して」などのフレーズを使用して、意図を説明します。そうしないと、マルチモーダル モデルが画像ではなくテキストで応答する可能性があります。

制限事項:

  • 最高のパフォーマンスを実現するには、EN、es-MX、ja-JP、zh-CN、hi-IN のいずれかの言語を使用してください。

  • 画像生成では、音声や動画の入力はサポートされていません。

  • モデルは、リクエストした数の画像を正確に作成できない場合があります。

  • 最適な結果を得るには、入力に含める画像の数を最大 3 枚にしてください。

  • テキストを含む画像を生成する場合は、まずテキストを生成してから、そのテキストを含む画像を生成します。

  • 次のような状況では、画像やテキストの生成が想定どおりに機能しないことがあります。

    • モデルがテキストのみを作成する場合があります。画像が必要な場合は、リクエストで画像を明確に指定してください。例: 「作業時に画像を提供してください。」

    • モデルがテキストを画像として生成する場合があります。テキストを生成するには、テキスト出力が必要であることを明示的に指定します。たとえば、「イラスト付きの説明テキストを生成してください」などです。

    • モデルは、コンテンツの生成が完了していない場合でも、生成を停止することがあります。この場合は、もう一度お試しいただくか、別のプロンプトを使用してください。

    • プロンプトが安全でない可能性がある場合、モデルはリクエストを処理せず、安全でない画像を作成できないことを示すレスポンスを返すことがあります。この場合、FinishReasonSTOP です。

画像を生成

以降のセクションでは、Vertex AI Studio または API を使用して画像を生成する方法について説明します。

プロンプトのガイダンスとベスト プラクティスについては、マルチモーダル プロンプトを設計するをご覧ください。

コンソール

画像生成を使用するには:

  1. [Vertex AI Studio] > [プロンプトを作成] を開きます。
  2. [モデルの切り替え] をクリックし、メニューから gemini-2.5-flash-image を選択します。
  3. [出力] パネルで、プルダウン メニューから [画像とテキスト] を選択します。
  4. [プロンプトを作成] テキスト領域に、生成する画像の説明を入力します。
  5. [プロンプト]()ボタンをクリックします。

Gemini は、説明に基づいて画像を生成します。このプロセスには数秒かかりますが、容量によっては比較的遅くなることがあります。

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=global
export GOOGLE_GENAI_USE_VERTEXAI=True

from google import genai
from google.genai.types import GenerateContentConfig, Modality
from PIL import Image
from io import BytesIO

client = genai.Client()

response = client.models.generate_content(
    model="gemini-2.5-flash-image",
    contents=("Generate an image of the Eiffel tower with fireworks in the background."),
    config=GenerateContentConfig(
        response_modalities=[Modality.TEXT, Modality.IMAGE],
        candidate_count=1,
        safety_settings=[
            {"method": "PROBABILITY"},
            {"category": "HARM_CATEGORY_DANGEROUS_CONTENT"},
            {"threshold": "BLOCK_MEDIUM_AND_ABOVE"},
        ],
    ),
)
for part in response.candidates[0].content.parts:
    if part.text:
        print(part.text)
    elif part.inline_data:
        image = Image.open(BytesIO((part.inline_data.data)))
        image.save("output_folder/example-image-eiffel-tower.png")
# Example response:
#   I will generate an image of the Eiffel Tower at night, with a vibrant display of
#   colorful fireworks exploding in the dark sky behind it. The tower will be
#   illuminated, standing tall as the focal point of the scene, with the bursts of
#   light from the fireworks creating a festive atmosphere.

Node.js

インストール

npm install @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=global
export GOOGLE_GENAI_USE_VERTEXAI=True

const fs = require('fs');
const {GoogleGenAI, Modality} = require('@google/genai');

const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
const GOOGLE_CLOUD_LOCATION =
  process.env.GOOGLE_CLOUD_LOCATION || 'us-central1';

async function generateContent(
  projectId = GOOGLE_CLOUD_PROJECT,
  location = GOOGLE_CLOUD_LOCATION
) {
  const client = new GoogleGenAI({
    vertexai: true,
    project: projectId,
    location: location,
  });

  const response = await client.models.generateContentStream({
    model: 'gemini-2.5-flash-image',
    contents:
      'Generate an image of the Eiffel tower with fireworks in the background.',
    config: {
      responseModalities: [Modality.TEXT, Modality.IMAGE],
    },
  });

  const generatedFileNames = [];
  let imageIndex = 0;
  for await (const chunk of response) {
    const text = chunk.text;
    const data = chunk.data;
    if (text) {
      console.debug(text);
    } else if (data) {
      const fileName = `generate_content_streaming_image_${imageIndex++}.png`;
      console.debug(`Writing response image to file: ${fileName}.`);
      try {
        fs.writeFileSync(fileName, data);
        generatedFileNames.push(fileName);
      } catch (error) {
        console.error(`Failed to write image file ${fileName}:`, error);
      }
    }
  }

  return generatedFileNames;
}

Java

Java をインストールまたは更新します。

詳しくは、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=global
export GOOGLE_GENAI_USE_VERTEXAI=True


import com.google.genai.Client;
import com.google.genai.types.Blob;
import com.google.genai.types.Candidate;
import com.google.genai.types.Content;
import com.google.genai.types.GenerateContentConfig;
import com.google.genai.types.GenerateContentResponse;
import com.google.genai.types.Part;
import com.google.genai.types.SafetySetting;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;

public class ImageGenMmFlashWithText {

  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String modelId = "gemini-2.5-flash-image";
    String outputFile = "resources/output/example-image-eiffel-tower.png";
    generateContent(modelId, outputFile);
  }

  // Generates an image with text input
  public static void generateContent(String modelId, String outputFile) throws IOException {
    // Client Initialization. Once created, it can be reused for multiple requests.
    try (Client client = Client.builder().location("global").vertexAI(true).build()) {

      GenerateContentConfig contentConfig =
          GenerateContentConfig.builder()
              .responseModalities("TEXT", "IMAGE")
              .candidateCount(1)
              .safetySettings(
                  SafetySetting.builder()
                      .method("PROBABILITY")
                      .category("HARM_CATEGORY_DANGEROUS_CONTENT")
                      .threshold("BLOCK_MEDIUM_AND_ABOVE")
                      .build())
              .build();

      GenerateContentResponse response =
          client.models.generateContent(
              modelId,
              "Generate an image of the Eiffel tower with fireworks in the background.",
              contentConfig);

      // Get parts of the response
      List<Part> parts =
          response
              .candidates()
              .flatMap(candidates -> candidates.stream().findFirst())
              .flatMap(Candidate::content)
              .flatMap(Content::parts)
              .orElse(new ArrayList<>());

      // For each part print text if present, otherwise read image data if present and
      // write it to the output file
      for (Part part : parts) {
        if (part.text().isPresent()) {
          System.out.println(part.text().get());
        } else if (part.inlineData().flatMap(Blob::data).isPresent()) {
          BufferedImage image =
              ImageIO.read(new ByteArrayInputStream(part.inlineData().flatMap(Blob::data).get()));
          ImageIO.write(image, "png", new File(outputFile));
        }
      }

      System.out.println("Content written to: " + outputFile);
      // Example response:
      // Here is the Eiffel Tower with fireworks in the background...
      //
      // Content written to: resources/output/example-image-eiffel-tower.png
    }
  }
}

REST

ターミナルで次のコマンドを実行して、このファイルを現在のディレクトリに作成または上書きします。

curl -X POST \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  -H "Content-Type: application/json" \
  https://${API_ENDPOINT}:generateContent \
  -d '{
    "contents": {
      "role": "USER",
      "parts": { "text": "Create a tutorial explaining how to make a peanut butter and jelly sandwich in three easy steps."},
    },
    "generation_config": {
      "response_modalities": ["TEXT", "IMAGE"],
      "image_config": {
        "aspect_ratio": "16:9",
      },
     },
     "safetySettings": {
      "method": "PROBABILITY",
      "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
      "threshold": "BLOCK_MEDIUM_AND_ABOVE"
    },
  }' 2>/dev/null >response.json

Gemini は、説明に基づいて画像を生成します。このプロセスには数秒かかりますが、容量によっては比較的遅くなることがあります。

インターリーブされた画像とテキストを生成する

Gemini 2.5 Flash Image では、テキスト レスポンスと画像を交互に生成できます。たとえば、生成されたレシピの各ステップがどのようなものになるかを示す画像を、そのステップのテキストに合わせて生成できます。この場合、モデルに個別のリクエストを行う必要はありません。

コンソール

テキスト レスポンスと画像が混在するコンテンツを生成するには:

  1. [Vertex AI Studio] > [プロンプトを作成] を開きます。
  2. [モデルの切り替え] をクリックし、メニューから gemini-2.5-flash-image を選択します。
  3. [出力] パネルで、プルダウン メニューから [画像とテキスト] を選択します。
  4. [プロンプトを作成] テキスト領域に、生成する画像の説明を入力します。たとえば、「ピーナッツ バターとジャムのサンドイッチを 3 つの簡単なステップで作る方法を説明するチュートリアルを作成します。各ステップについて、ステップの番号と説明を含むタイトルを付け、画像も生成してください。各画像の縦横比は 1:1 にしてください。」
  5. [プロンプト]()ボタンをクリックします。

Gemini は、説明に基づいてレスポンスを生成します。このプロセスには数秒かかりますが、容量によっては比較的遅くなることがあります。

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=global
export GOOGLE_GENAI_USE_VERTEXAI=True

from google import genai
from google.genai.types import GenerateContentConfig, Modality
from PIL import Image
from io import BytesIO

client = genai.Client()

response = client.models.generate_content(
    model="gemini-2.5-flash-image",
    contents=(
        "Generate an illustrated recipe for a paella."
        "Create images to go alongside the text as you generate the recipe"
    ),
    config=GenerateContentConfig(response_modalities=[Modality.TEXT, Modality.IMAGE]),
)
with open("output_folder/paella-recipe.md", "w") as fp:
    for i, part in enumerate(response.candidates[0].content.parts):
        if part.text is not None:
            fp.write(part.text)
        elif part.inline_data is not None:
            image = Image.open(BytesIO((part.inline_data.data)))
            image.save(f"output_folder/example-image-{i+1}.png")
            fp.write(f"![image](example-image-{i+1}.png)")
# Example response:
#  A markdown page for a Paella recipe(`paella-recipe.md`) has been generated.
#   It includes detailed steps and several images illustrating the cooking process.

Java

Java をインストールまたは更新します。

詳しくは、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=global
export GOOGLE_GENAI_USE_VERTEXAI=True


import com.google.genai.Client;
import com.google.genai.types.Blob;
import com.google.genai.types.Candidate;
import com.google.genai.types.Content;
import com.google.genai.types.GenerateContentConfig;
import com.google.genai.types.GenerateContentResponse;
import com.google.genai.types.Part;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;

public class ImageGenMmFlashTextAndImageWithText {

  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String modelId = "gemini-2.5-flash-image";
    String outputFile = "resources/output/paella-recipe.md";
    generateContent(modelId, outputFile);
  }

  // Generates text and image with text input
  public static void generateContent(String modelId, String outputFile) throws IOException {
    // Client Initialization. Once created, it can be reused for multiple requests.
    try (Client client = Client.builder().location("global").vertexAI(true).build()) {

      GenerateContentResponse response =
          client.models.generateContent(
              modelId,
              Content.fromParts(
                  Part.fromText("Generate an illustrated recipe for a paella."),
                  Part.fromText(
                      "Create images to go alongside the text as you generate the recipe.")),
              GenerateContentConfig.builder().responseModalities("TEXT", "IMAGE").build());

      try (BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile))) {

        // Get parts of the response
        List<Part> parts =
            response
                .candidates()
                .flatMap(candidates -> candidates.stream().findFirst())
                .flatMap(Candidate::content)
                .flatMap(Content::parts)
                .orElse(new ArrayList<>());

        int index = 1;
        // For each part print text if present, otherwise read image data if present and
        // write it to the output file
        for (Part part : parts) {
          if (part.text().isPresent()) {
            writer.write(part.text().get());
          } else if (part.inlineData().flatMap(Blob::data).isPresent()) {
            BufferedImage image =
                ImageIO.read(new ByteArrayInputStream(part.inlineData().flatMap(Blob::data).get()));
            ImageIO.write(
                image, "png", new File("resources/output/example-image-" + index + ".png"));
            writer.write("![image](example-image-" + index + ".png)");
          }
          index++;
        }

        System.out.println("Content written to: " + outputFile);

        // Example response:
        // A markdown page for a Paella recipe(`paella-recipe.md`) has been generated.
        // It includes detailed steps and several images illustrating the cooking process.
        //
        // Content written to:  resources/output/paella-recipe.md
      }
    }
  }
}

REST

ターミナルで次のコマンドを実行して、このファイルを現在のディレクトリに作成または上書きします。

curl -X POST \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  -H "Content-Type: application/json" \
  https://${API_ENDPOINT}:generateContent \
  -d '{
    "contents": {
      "role": "USER",
      "parts": { "text": "Create a tutorial explaining how to make a peanut butter and jelly sandwich in three easy steps. For each step, provide a title with the number of the step, an explanation, and also generate an image, generate each image in a 1:1 aspect ratio."},
    },
    "generation_config": {
      "response_modalities": ["TEXT", "IMAGE"],
      "image_config": {
        "aspect_ratio": "16:9",
      },
    },
    "safetySettings": {
      "method": "PROBABILITY",
      "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
      "threshold": "BLOCK_MEDIUM_AND_ABOVE"
    },
  }' 2>/dev/null >response.json

Gemini は、説明に基づいて画像を生成します。このプロセスには数秒かかりますが、容量によっては比較的遅くなることがあります。

言語 / 地域に対応した画像生成

Gemini 2.5 Flash Image では、テキストや画像のレスポンスを提供する際に、位置情報を含めることもできます。たとえば、モデルに場所を指定しなくても、現在の場所を考慮した場所や体験の画像を生成できます。

コンソール

言語 / 地域に対応した画像生成を使用するには:

  1. [Vertex AI Studio] > [プロンプトを作成] を開きます。
  2. [モデルの切り替え] をクリックし、メニューから gemini-2.5-flash-image を選択します。
  3. [出力] パネルで、プルダウン メニューから [画像とテキスト] を選択します。
  4. [プロンプトを作成] テキスト領域に、生成する画像の説明を入力します。たとえば、「典型的な朝食の写真を生成してください」などです。
  5. [プロンプト]()ボタンをクリックします。

Gemini は、説明に基づいてレスポンスを生成します。このプロセスには数秒かかりますが、容量によっては比較的遅くなることがあります。

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=global
export GOOGLE_GENAI_USE_VERTEXAI=True

from google import genai
from google.genai.types import GenerateContentConfig, Modality
from PIL import Image
from io import BytesIO

client = genai.Client()

response = client.models.generate_content(
    model="gemini-2.5-flash-image",
    contents=("Generate a photo of a breakfast meal."),
    config=GenerateContentConfig(response_modalities=[Modality.TEXT, Modality.IMAGE]),
)
for part in response.candidates[0].content.parts:
    if part.text:
        print(part.text)
    elif part.inline_data:
        image = Image.open(BytesIO((part.inline_data.data)))
        image.save("output_folder/example-breakfast-meal.png")
# Example response:
#   Generates a photo of a vibrant and appetizing breakfast meal.
#   The scene will feature a white plate with golden-brown pancakes
#   stacked neatly, drizzled with rich maple syrup and ...

Java

Java をインストールまたは更新します。

詳しくは、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=global
export GOOGLE_GENAI_USE_VERTEXAI=True


import com.google.genai.Client;
import com.google.genai.types.Blob;
import com.google.genai.types.Candidate;
import com.google.genai.types.Content;
import com.google.genai.types.GenerateContentConfig;
import com.google.genai.types.GenerateContentResponse;
import com.google.genai.types.Part;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;

public class ImageGenMmFlashLocaleAwareWithText {

  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String modelId = "gemini-2.5-flash-image";
    String outputFile = "resources/output/example-breakfast-meal.png";
    generateContent(modelId, outputFile);
  }

  // Generates an image with text input
  public static void generateContent(String modelId, String outputFile) throws IOException {
    // Client Initialization. Once created, it can be reused for multiple requests.
    try (Client client = Client.builder().location("global").vertexAI(true).build()) {

      GenerateContentResponse response =
          client.models.generateContent(
              modelId,
              "Generate a photo of a breakfast meal.",
              GenerateContentConfig.builder().responseModalities("TEXT", "IMAGE").build());

      // Get parts of the response
      List<Part> parts =
          response
              .candidates()
              .flatMap(candidates -> candidates.stream().findFirst())
              .flatMap(Candidate::content)
              .flatMap(Content::parts)
              .orElse(new ArrayList<>());

      // For each part print text if present, otherwise read image data if present and
      // write it to the output file
      for (Part part : parts) {
        if (part.text().isPresent()) {
          System.out.println(part.text().get());
        } else if (part.inlineData().flatMap(Blob::data).isPresent()) {
          BufferedImage image =
              ImageIO.read(new ByteArrayInputStream(part.inlineData().flatMap(Blob::data).get()));
          ImageIO.write(image, "png", new File(outputFile));
        }
      }

      System.out.println("Content written to: " + outputFile);

      // Example response:
      // Here is a photo of a breakfast meal for you!
      //
      // Content written to: resources/output/example-breakfast-meal.png
    }
  }
}

REST

ターミナルで次のコマンドを実行して、このファイルを現在のディレクトリに作成または上書きします。

curl -X POST \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  -H "Content-Type: application/json" \
  https://${API_ENDPOINT}:generateContent \
  -d '{
    "contents": {
      "role": "USER",
      "parts": { "text": "Generate a photo of a typical breakfast."},
    },
    "generation_config": {
      "response_modalities": ["TEXT", "IMAGE"],
      "image_config": {
        "aspect_ratio": "16:9",
      },
    },
    "safetySettings": {
      "method": "PROBABILITY",
      "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
      "threshold": "BLOCK_MEDIUM_AND_ABOVE"
    },
  }' 2>/dev/null >response.json

Gemini は、説明に基づいて画像を生成します。このプロセスには数秒かかりますが、容量によっては比較的遅くなることがあります。