Featured image of post Google Gemini-2.0-Flash-Exp Test

Google Gemini-2.0-Flash-Exp Test

Gemini-2.0-Flash-Exp多模态原生图像生成

Google Gemini-2.0-Flash-Exp

2025 年 3 月 12 日,谷歌正式发布 Gemini 2.0 Flash Exp 全模态图像生成器,支持原生图像生成功能,所有开发者都可以通过 Gemini API 和 Google AI Studio 中的实验版本使用 Gemini 2.0 Flash 进行原生图像生成。

image-20250316193351777

使用方法&费用

Google AI Studio在线使用

Google AI Studio

  • 按下图所示,右侧选择模型Model为Gemini 2.0 Flash(Image Generation) Experimental
  • Output format选择为Images and text,左侧Create Prompt开始使用
  • 可以先点击3种官方给出的提示词测试看下提示词怎么写

这个翻译插件【沉浸式翻译】比较好用,如下图所示网页上能中英对照等等功能,免费功能够用。

配置Gemini API调用

按照以下官网几个说明文档

用AI IDE trae调试了一个脚本,测试成功。

  • 关键需要装的是这个库pip install google-generativeai
  • 图片数据的处理

脚本如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from google import genai
from google.genai import types
from PIL import Image
from io import BytesIO
import base64
import os
from datetime import datetime
from dotenv import load_dotenv

# 加载环境变量
load_dotenv()

# 从环境变量获取API密钥,修改.env.example文件为.env文件并填写YOUR_API_KEY
api_key = os.getenv("GEMINI_API_KEY")
if not api_key:
    raise ValueError("请设置环境变量GEMINI_API_KEY或创建.env文件")

# 初始化客户端
client = genai.Client(api_key=api_key)

# 设置文本提示
contents = ('Hi, can you create a 3d rendered image of a pig '
            'with wings and a top hat flying over a happy ' 
            'futuristic scifi city with lots of greenery?')

# 生成内容,注意模型名称
response = client.models.generate_content(
    model="models/gemini-2.0-flash-exp",
    contents=contents,
    config=types.GenerateContentConfig(response_modalities=['Text', 'Image'])
)

# 保存生成的图像
save_dir = os.path.join(os.path.dirname(__file__), 'GeminiFlash2.0Exp')
os.makedirs(save_dir, exist_ok=True)

for part in response.candidates[0].content.parts:
    if part.text is not None:
        print(part.text)
    elif part.inline_data is not None:
        try:
            image_data = base64.b64decode(part.inline_data.data)
            image = Image.open(BytesIO(image_data))
            
            # 生成带时间戳的文件名
            timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
            image_path = os.path.join(save_dir, f'gemini_image_{timestamp}.png')
            
            # 保存图片
            image.save(image_path)
            print(f'图片已保存至: {image_path}')
            
            # 显示图片
            image.show()
        except Exception as e:
            print(f"无法处理图像数据: {e}")
            print(f"数据类型: {type(part.inline_data.data)}")

费用

Gemini 2.0 Flash(Image Generation) Exp免费可用,每天免费请求次数1500次。

image-20250316204651101

记录

官方提供3种处理样例:

  • 图片编辑
  • 视觉故事
  • 生日卡片

根据官方的三种样例,结合一些网上视频加了扩展用法做了以下测试

图片编辑-红烧肉食谱

视觉故事-小老虎冒险故事

生日卡片-中英文效果

扩展-人物一致性-换装、物体

这是两张原图

以下两张是Gemini 生成(有一个Gemini AI生成的标识)image-20250316212232411

点开查看衣服错的那张,人脸有一致性

  • 这是更换蓝色T恤成功的

GeneratedImage9_16PM.png

Google的AI生图一向是有意避免生成人脸的;换一个动物来测试下

扩展-动物-一致性测试

总结

  1. Google 的 Gemini 2.0 Flash Exp可以实现免费完成图片编辑、生图功能,保持上下文这一点很强,可以解决一些日常基础需求
  2. Stable Diffusion 的WebUI和Comfyui开源免费,当然能更好的实现换装、生图等等,使用门槛太高。
comments powered by Disqus
使用 Hugo 构建
主题 StackJimmy 设计