便携AI网
AI的百科全书

Google最新画图模型Gemini 2.5 Flash Image(nano-banana)使用教程

Gemini 2.5 Flash Image(代号“nano-banana”)是谷歌DeepMind团队开发的一款先进的图像生成与编辑模型,已于2025年8月26日正式发布,模型名称:gemini-2.5-flash-image-preview,本文分享下便携AI聚合API如何使用Google Gemini 2.5 Flash Image(nano-banana)。

一、前言

示例中的api_key可以在网站后台获取,获取方法:《便携AI聚合API新建令牌(API key)教程》。

必须要使用流模式,即指定stream为True。

二、调用方法

可以通过OpenAI的chat模式来调用Gemini 2.5 Flash画图模型,模型名称:gemini-2.5-flash-image-preview,模型会通过流模式直接返回生成图片的base64码,可以结合以下代码来解析base64:

def save_base64_image(markdown_line, filename_prefix="gemini_output"):
    # 提取 base64 字符串
    import re
    match = re.search(r'!\[.*?\]\(data:image/(png|jpeg);base64,(.*?)\)', markdown_line)
    if match:
        image_format = match.group(1)  # png 或 jpeg
        base64_data = match.group(2)

        # 解码并保存
        image_data = base64.b64decode(base64_data)
        filename = f"{filename_prefix}.{image_format}"
        with open(f"{filename}", "wb") as f:
            f.write(image_data)
        print(f"图片已保存为 {filename}")
    else:
        print("未找到合法的 base64 图片数据")

如果需要提供底图(实现图片修改),那么可以通过url或者base64的方式来提供底图,base64可以通过以下代码来获取:

def image_to_base64(image_path):
    # 以二进制方式读取图片文件
    with open(image_path, "rb") as image_file:
        # 将图像文件内容读取到变量中
        image_data = image_file.read()
        # 使用base64模块进行编码
        base64_encoded_data = base64.b64encode(image_data)
        # 将编码后的数据转换为字符串
        base64_encoded_str = base64_encoded_data.decode('utf-8')
        return base64_encoded_str

下面介绍下Google Gemini 2.5 Flash的画图和图像编辑功能:

1、图像生成(画图)教程

提供prompt,由Gemini 2.5 Flash Image(nano-banana)生成一张全新的图片:

api_key = "sk-dasfasdf"
url = f'https://api.bianxie.ai/v1/chat/completions'

headers = {
    'Content-Type': 'application/json',
    'Authorization': f'Bearer {api_key}'
}

data = {
    'model': 'gemini-2.5-flash-image-preview',
    'messages': [{'role': 'user', 'content': 'two dogs in a tree'}],
    'stream': True
}

response = requests.post(url, headers=headers, json=data)

for line in response.iter_lines():
    if line:
        decoded_line = line.decode("utf-8")
        if "![image](data:image/" in decoded_line:
            save_base64_image(decoded_line)
        if decoded_line.startswith("data: "):
            payload = decoded_line[6:]
            if payload == "[DONE]":
                break
            try:
                parsed = json.loads(payload)
                # 检查 choices 是否存在且非空
                if "choices" in parsed and len(parsed["choices"]) > 0:
                    choice = parsed["choices"][0]
                    # 检查 delta 字段是否存在
                    if "delta" in choice and "content" in choice["delta"]:
                        content = choice["delta"]["content"]
                        if content:
                            print(content, end="", flush=True)
                    # 如果是结束块,检查 finish_reason
                    elif "finish_reason" in choice:
                        print(f"\n[Stream ended: {choice.get('finish_reason')}]")
                        break
                else:
                    # 如果 choices 为空,可能是元数据块,跳过
                    print("\n[Skipping metadata or empty choices block]", flush=True)
            except Exception as e:
                print(f"\n解析失败:{e}", flush=True)

生成的图如下:

Google最新画图模型Gemini 2.5 Flash Image(nano-banana)使用教程

2、图像编辑(改图)教程

提供prompt和原始图,由Gemini 2.5 Flash Image(nano-banana)做图片修改,可以通过url或者base64的方式提供原图,例如这里我提供了上一部分由Gemini 2.5 Flash Image生成的图,并让它再加一条狗:

api_key = "sk-dasfasdf"
url = f'https://api.bianxie.ai/v1/chat/completions'

headers = {
    'Content-Type': 'application/json',
    'Authorization': f'Bearer {api_key}'
}

image_base64 = image_to_base64('two-dogs.png')
data = {
    "model": "gemini-2.5-flash-image-preview",
    "stream": True,
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": "add a dog"
                },
                {
                    "type": "image_url",
                    "image_url": {
                        "url": f"data:image/jpeg;base64,{image_base64}"
                        # "url": "https://github.com/dianping/cat/raw/master/cat-home/src/main/webapp/images/logo/cat_logo03.png"
                    }
                }
            ]
        }
    ]
}

response = requests.post(url, headers=headers, json=data)

for line in response.iter_lines():
    print(line)
    if line:
        decoded_line = line.decode("utf-8")
        if "![image](data:image/" in decoded_line:
            save_base64_image(decoded_line)
        if decoded_line.startswith("data: "):
            payload = decoded_line[6:]
            if payload == "[DONE]":
                break
            try:
                parsed = json.loads(payload)
                # 检查 choices 是否存在且非空
                if "choices" in parsed and len(parsed["choices"]) > 0:
                    choice = parsed["choices"][0]
                    # 检查 delta 字段是否存在
                    if "delta" in choice and "content" in choice["delta"]:
                        content = choice["delta"]["content"]
                        if content:
                            print(content, end="", flush=True)
                    # 如果是结束块,检查 finish_reason
                    elif "finish_reason" in choice:
                        print(f"\n[Stream ended: {choice.get('finish_reason')}]")
                        break
                else:
                    # 如果 choices 为空,可能是元数据块,跳过
                    print("\n[Skipping metadata or empty choices block]", flush=True)
            except Exception as e:
                print(f"\n解析失败:{e}", flush=True)

结果如下:

Google最新画图模型Gemini 2.5 Flash Image(nano-banana)使用教程

赞(0)
未经允许不得转载:便携AI » Google最新画图模型Gemini 2.5 Flash Image(nano-banana)使用教程