使用 Gemini 编辑图片

Gemini 2.5 Flash 图片预览版支持多种类型的图片编辑。

图片修改

Gemini 2.5 Flash Image 的图片生成公开预览版 (gemini-2.5-flash-image-preview) 支持修改图片和生成图片。在此公开预览版中,Gemini 2.5 Flash Image 支持改进的图片编辑和多轮编辑,并包含更新的安全过滤条件,可提供更灵活、限制更少的用户体验。

它支持以下模态和功能:

  • 图片修改(文本和图片转图片)

    • 提示示例:“修改此图片,使其看起来像卡通图片”
    • 提示示例:[猫的图片] + [枕头的图片] +“请在这只枕头上制作一张我的猫的十字绣图案。”
  • 多轮图片修改(聊天)

    • 提示示例:[上传一张蓝色汽车的图片。]“把这辆车变成敞篷车。”“现在将颜色更改为黄色。” “添加剧透。”

修改图片

控制台

如需修改图片,请执行以下操作:

  1. 依次打开 Vertex AI Studio > 创建提示
  2. 点击切换模型,然后从菜单中选择 gemini-2.5-flash-image-preview
  3. 输出面板中,从下拉菜单中选择图片和文本
  4. 点击插入媒体 (),并从菜单中选择一个来源,然后按照对话框中的说明操作。
  5. 编写提示文本区域中,写下您想对图片进行的修改。
  6. 点击提示 () 按钮。

Gemini 将根据您的说明生成所提供图片的修改版本。此过程应需要几秒钟,但可能会相对较慢,具体取决于容量。

Python

安装

pip install --upgrade google-genai

如需了解详情,请参阅 SDK 参考文档

设置环境变量以将 Gen AI SDK 与 Vertex AI 搭配使用:

# 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 预览版笔记本