使用 Gemini 編輯圖片

Gemini 2.5 Flash 圖像預覽支援多種圖像編輯類型。

圖像編輯

Gemini 2.5 Flash Image 圖像生成功能公開預先發布版 (gemini-2.5-flash-image-preview) 除了生成圖像,也支援編輯圖像。在這次的公開搶先版中,Gemini 2.5 Flash Image 支援更優異的圖片編輯功能和多輪編輯,並提供更新的安全篩選器,讓使用者體驗更靈活且限制較少。

支援的模式和功能如下:

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

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

    • 提示範例:[上傳藍色汽車的圖片。] 「把這輛車變成敞篷車。」「Now change the color to yellow.」(現在將顏色改為黃色)。 「新增劇透內容。」

編輯圖片

控制台

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

  1. 開啟 Vertex AI Studio > 建立提示詞
  2. 按一下「切換模型」,然後從選單中選取 gemini-2.5-flash-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-2.5-flash-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")
# Example response:
#  Here's the cartoon-style edit of the image:
#  Cartoon-style edit:
#  - Simplified the Eiffel Tower with bolder lines and slightly exaggerated proportions.
#  - Brightened and saturated the colors of the sky, fireworks, and foliage for a more vibrant, cartoonish look.
#  ....

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": [
        {"file_data": {
          "mime_type": "image/jpg",
          "file_uri": "<var>FILE_NAME</var>"
          }
        },
        {"text": "Convert this photo to black and white, in a cartoonish style."},
      ]

    },
    "generation_config": {
      "response_modalities": ["TEXT", "IMAGE"],
    },
    "safetySettings": {
      "method": "PROBABILITY",
      "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
      "threshold": "BLOCK_MEDIUM_AND_ABOVE"
    },
  }' 2>/dev/null >response.json

Gemini 會根據你的描述生成圖片。這項程序應該只需要幾秒鐘,但視容量而定,速度可能會較慢。

多輪圖像編輯

Gemini 2.5 Flash 圖像預覽版也支援改良的多輪編輯功能,讓您在收到編輯後的圖像回覆後,可以回覆模型並進行變更。這樣一來,你就能繼續透過對話編輯圖片。

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

如要測試多輪圖像編輯功能,請試用 Gemini 2.5 Flash Image 預覽版筆記本