使用 Gemini 生成及編輯圖片

除了文字,下列 Gemini 模型也支援生成圖片:

  • Gemini 2.5 Flash Image (又稱 Gemini 2.5 Flash,採用 Nano Banana 模型)

  • Gemini 3 Pro Image (預先發布版),又稱 Gemini 3 Pro (搭載 Nano Banana)

這項功能擴展了 Gemini 的能力,包括:

  • 透過自然語言對話反覆生成圖像,並在調整圖像時維持一致性和情境脈絡。
  • 生成圖像,並以高品質呈現長篇文字。
  • 生成文字和圖片夾雜的內容。舉例來說,單一回合的網誌文章包含文字和圖片。先前,這需要將多個模型串連在一起。
  • 運用 Gemini 的世界知識和推理能力生成圖片。

Gemini 2.5 Flash Image (gemini-2.5-flash-image) 和 Gemini 3 Pro Image 預先發布版 (gemini-3-pro-image-preview) 支援生成人物圖像,並內含更新版安全篩選器,提供更彈性且限制較少的使用體驗。Gemini 2.5 Flash Image 可生成 1024 像素的圖像。Gemini 3 Pro Image 可生成最大 4096 像素的圖片。

這兩種模型都支援下列模式和功能:

  • 文字轉圖像

    • 範例提示:「生成艾菲爾鐵塔的圖片,背景要有煙火。」
  • 文字轉圖像 (文字算繪)

    • 提示範例:「生成一張電影風格的照片,內容是大型建築物,正面投影出巨大文字:「Gemini 3 現在可以生成長篇文字」」
  • 文字轉圖像和文字 (交錯)

    • 範例提示:「生成西班牙海鮮飯的插圖食譜。在生成食譜的同時建立圖像。」
    • 提示範例:「以 3D 卡通動畫風格生成小狗的故事。為每個場景生成圖片
  • 圖片和文字轉為圖片和文字 (交錯)

    • 範例提示: (附上已擺放家具的房間圖片)「我的空間適合擺放哪些其他顏色的沙發?可以更新圖片嗎?」

最佳做法

如要提升圖像生成結果的品質,請遵循下列最佳做法:

  • 具體說明:提供越多詳細資訊,生成結果就越貼近需求。舉例來說,請改用「華麗的精靈板甲,刻有銀葉圖案,高領和肩甲形狀像獵鷹翅膀」,而非「奇幻盔甲」。

  • 提供背景資訊和意圖:說明圖片用途,幫助模型瞭解背景資訊。舉例來說,「為高檔極簡護膚品牌設計標誌」的效果會比「設計標誌」更好。

  • 反覆測試及修正:第一次嘗試時,請別期待能生成完美的圖片。使用後續提示進行微調,例如「將光線調暖」或「將角色的表情改得更嚴肅」。

  • 使用逐步操作說明:如果是複雜的場景,請將要求分成多個步驟。例如:「首先,請在黎明時分生成寧靜的霧中森林背景。接著,在前景中新增一個長滿青苔的古老石祭壇。最後,在祭壇上放上一把發光的劍。」

  • 描述你想要的內容,而不是不想要的內容:請正面描述場景,例如「空蕩蕩的街道,沒有任何交通跡象」,而不是說「沒有車輛」。

  • 控制攝影機:引導攝影機視角。使用攝影和電影術語描述構圖,例如「廣角拍攝」、「微距拍攝」或「低角度透視」。

  • 圖片提示:使用「製作…的圖片」或「生成…的圖片」等詞組,描述意圖。否則多模態模型可能會以文字回覆,而非圖片。

  • 傳遞想法簽章 使用 Gemini 3 Pro Image 時,建議您在多輪圖像建立和編輯期間,將想法簽章傳遞回模型。這樣一來,您就能在互動過程中保留推理脈絡。如需使用 Gemini 3 Pro Image 進行多輪圖像編輯的相關程式碼範例,請參閱「使用思維簽章進行多輪圖像編輯的範例」。

限制:

  • 如要使用 Gemini 2.5 Flash Image 獲得最佳成效,請使用下列語言:英文、西班牙文 (墨西哥)、日文、中文 (中國) 或印地文。如要使用 Gemini 3 Pro Image 獲得最佳成效,請使用下列語言:ar-EG、de-DE、EN、es-MX、fr-FR、hi-IN、id-ID、it-IT、ja-JP、ko-KR、pt-BR、ru-RU、ua-UA、vi-VN 和 zh-CN

  • 圖像生成功能不支援音訊或影片輸入內容。

  • 模型可能不會生成您要求的確切圖片數量。

  • 如要使用 Gemini 2.5 Flash Image 獲得最佳結果,輸入內容最多可包含三張圖片。如要使用 Gemini 3 Pro 圖片功能獲得最佳成效,輸入內容最多可包含 14 張圖片。

  • 生成含有文字的圖片時,請先生成文字,然後再生成含有該文字的圖片。

  • 在下列情況下,圖片或文字生成功能可能無法正常運作:

    • 如果提示不夠明確,模型可能只會生成文字,不會生成圖片。如要取得圖片,請在要求中明確說明。 例如「請在過程中提供圖片」。

    • 模型可能會將文字做為圖片建立。如要生成文字,請明確要求文字輸出。例如「生成敘事文字和插圖」。

    • 即使模型尚未完成生成內容,也可能會停止作業。如果發生這種情況,請再試一次或改用其他提示。

    • 如果提示可能不安全,模型可能不會處理要求,並傳回無法建立不安全圖片的回應。在本例中,FinishReasonSTOP

生成圖像

以下章節將說明如何使用 Vertex AI Studio 或 API 生成圖片。

如需提示詞的指南和最佳做法,請參閱「設計多模態提示」。

控制台

如要使用圖像生成功能,請按照下列步驟操作:

  1. 開啟 Vertex AI Studio > 建立提示詞
  2. 按一下「切換模型」,然後從選單中選取下列其中一個模型:
    • gemini-2.5-flash-image
    • gemini-3-pro-image-preview
  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-3-pro-image-preview",
    contents=("Generate an image of the Eiffel tower with fireworks in the background."),
    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-image-eiffel-tower.png")

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 generateImage(
  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 outputDir = 'output-folder';
      if (!fs.existsSync(outputDir)) {
        fs.mkdirSync(outputDir, {recursive: true});
      }
      const fileName = `${outputDir}/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);
      }
    }
  }

  // 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.

  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."
        }
      ]
    },
    "generationConfig": {
      "responseModalities": ["TEXT", "IMAGE"],
      "imageConfig": {
        "aspectRatio": "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
    • gemini-3-pro-image-preview
  3. 在「輸出」面板中,從下拉式選單選取「圖片和文字」
  4. 在「撰寫提示」文字區域中,描述要生成的圖片。舉例來說,「製作教學影片,以簡單三步驟說明如何製作花生醬和果醬三明治。為每個步驟提供標題 (附上步驟編號)、說明,並生成圖片,每張圖片的長寬比為 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-3-pro-image-preview",
    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)")

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."
        }
      ]
    },
    "generationConfig": {
      "responseModalities": ["TEXT", "IMAGE"],
      "imageConfig": {
        "aspectRatio": "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 可生成及編輯圖片 (gemini-2.5-flash-image)。Gemini 2.5 Flash Image 支援更完善的圖片編輯和多輪編輯功能,並採用更新的安全防護篩選器,提供更靈活、限制更少的使用者體驗。

支援的模態和功能如下:

  • 圖片編輯 (文字和圖片轉為圖片)

    • 範例提示:「將這張圖片編輯成卡通風格」
    • 範例提示:[貓咪圖片] + [枕頭圖片] +「在這顆枕頭上製作我貓咪的十字繡。」
  • 多輪圖像編輯 (聊天)

    • 範例提示:[上傳藍色車輛的圖片。] 「將這輛車變成敞篷車。」

      • [模型會傳回同一場景中敞篷車的圖片]「現在將顏色改為黃色。」
      • [模型傳回一輛黃色敞篷車的圖片]「加上擾流板。」
      • [模型傳回附有擾流板的敞篷車圖片]

編輯圖片

控制台

如要編輯圖片,請按照下列步驟操作:

  1. 開啟 Vertex AI Studio > 建立提示詞
  2. 按一下「切換模型」,然後從選單中選取下列其中一個模型:
    • gemini-2.5-flash-image
    • gemini-3-pro-image-preview
  3. 在「輸出」面板中,從下拉式選單選取「圖片和文字」
  4. 按一下「插入媒體」圖示 (),然後從選單中選取來源,並按照對話方塊的指示操作。
  5. 在「撰寫提示」文字區域中,寫下要對圖片進行的編輯。
  6. 按一下「提示」 () 按鈕。

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()

# Using an image of Eiffel tower, with fireworks in the background.
image = Image.open("test_resources/example-image-eiffel-tower.png")

response = client.models.generate_content(
    model="gemini-3-pro-image-preview",
    contents=[image, "Edit this image to make it look like a cartoon."],
    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/bw-example-image.png")

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.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;

public class ImageGenMmFlashEditImageWithTextAndImage {

  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/bw-example-image.png";
    generateContent(modelId, outputFile);
  }

  // Edits an image with image and 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()) {

      byte[] localImageBytes =
          Files.readAllBytes(Paths.get("resources/example-image-eiffel-tower.png"));

      GenerateContentResponse response =
          client.models.generateContent(
              modelId,
              Content.fromParts(
                  Part.fromBytes(localImageBytes, "image/png"),
                  Part.fromText("Edit this image to make it look like a cartoon.")),
              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:
      // No problem! Here's the image in a cartoon style...
      //
      // Content written to: resources/output/bw-example-image.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": [
        {"fileData": {
          "mimeType": "image/jpg",
          "fileUri": "FILE_NAME"
          }
        },
        {"text": "Convert this photo to black and white, in a cartoonish style."},
      ]

    },
    "generationConfig": {
      "responseModalities": ["TEXT", "IMAGE"],
      "imageConfig": {
        "aspectRatio": "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 和 Gemini 3 Pro Image,可進行多輪編輯,在收到編輯後的圖像回覆後,可回覆模型並進行變更。這樣一來,您就能繼續以對話方式編輯圖片。

請注意,建議將整個要求檔案大小限制在 50MB 以內。

如要測試多輪圖片編輯功能,請嘗試下列筆記本:

如要查看使用 Gemini 3 Pro Image 建立及編輯圖片的多輪對話程式碼範例,請參閱使用思維簽章編輯圖片的多輪對話範例

負責任的 AI 技術

為確保安全無虞的體驗,Vertex AI 的圖片生成功能採用多層式安全防護機制。這項功能旨在防止生成不當內容,包括煽情露骨、危險、暴力、仇恨或有害的素材。

所有使用者都必須遵守《生成式 AI 使用限制政策》。這項政策嚴格禁止生成以下內容:

  • 兒少性虐待或兒少剝削相關內容。
  • 助長暴力極端主義或恐怖主義的內容。
  • 散布未經當事人同意的私密圖像的內容。 助長自傷的內容。
  • 含有煽情露骨內容。
  • 構成仇恨言論。
  • 鼓吹騷擾或霸凌行為。

如果提示內容不安全,模型可能會拒絕生成圖片,或提示/生成的回覆可能會遭到安全篩選器封鎖。

  • 模型拒絕:如果提示可能不安全,模型可能會拒絕處理要求。如果發生這種情況,模型通常會提供文字回覆,表示無法生成不安全的圖片。FinishReason 將為 STOP
  • 安全性篩選器封鎖:
    • 如果安全篩選器判定提示詞可能有害,API 會在 PromptFeedback 中傳回 BlockedReason
    • 如果安全篩選器判斷回覆可能有害,API 回覆會包含 FinishReason,值為 IMAGE_SAFETYIMAGE_PROHIBITED_CONTENT 或類似項目。

安全篩選器代碼類別

視您設定的安全篩選機制而定,輸出內容可能包含類似下列的安全原因代碼:

    {
      "raiFilteredReason": "ERROR_MESSAGE. Support codes: 56562880"
    }

列出的代碼對應至特定有害類別。這些代碼與類別的對應關係如下:

錯誤代碼 安全類別 說明 已篩除的內容:提示輸入內容或圖片輸出內容
58061214
17301594
子項 偵測因 API 要求設定或允許清單而不得出現的兒童內容。 輸入 (提示):58061214
輸出 (圖片):17301594
29310472
15236754
名人 偵測要求中名人的逼真圖像。 輸入 (提示):29310472
輸出 (圖片):15236754
62263041 危險內容 偵測可能具有危險性的內容。 輸入 (提示)
57734940
22137204
仇恨 偵測仇恨相關主題或內容。 輸入 (提示詞):57734940
輸出 (圖片):22137204
74803281
29578790
42876398
其他 偵測要求中的其他雜項安全問題。 輸入 (提示):42876398
輸出 (圖片):29578790、74803281
39322892 人物/臉部 因要求安全設定不允許,而偵測到人或臉孔。 輸出內容 (圖片)
92201652 個人資訊 偵測文字中的個人識別資訊 (PII),例如提及信用卡號碼、住家地址或其他這類資訊。 輸入 (提示)
89371032
49114662
72817394
禁止宣傳的內容 偵測要求中是否含有違禁內容。 輸入 (提示):89371032
輸出 (圖片):49114662、72817394
90789179
63429089
43188360
色情內容 偵測色情內容。 輸入 (提示):90789179
輸出 (圖片):63429089、43188360
78610348 惡意言論 偵測文字中的惡意主題或內容。 輸入 (提示)
61493863
56562880
暴力 偵測圖片或文字中與暴力相關的內容。 輸入內容 (提示):61493863
輸出內容 (圖片):56562880
32635315 粗俗 從文字中偵測粗俗的主題或內容。 輸入 (提示)
64151117 名人或兒童 偵測名人或兒童的擬真圖像,且違反 Google 安全政策。 輸入內容 (提示詞)
輸出內容 (圖片)