Google今天推出了全新的画图人工智能:Gemini 3 Pro Image Preview(Nano Banana Pro),专为图像生成、编辑和理解设计,支持高级功能如精确控制物理效果(光线、相机、对焦、颜色分级)和多图像融合。目前便携AI聚合API已经接入了Nano Banana Pro的API:gemini-3-pro-image-preview,本文分享下使用教程。
一、前言
示例中的api_key可以在网站后台获取,获取方法:《便携AI聚合API新建令牌(API key)教程》。
二、调用方法
可以通过OpenAI的chat模式来调用Gemini 3 Pro Image Preview(Nano Banana Pro)画图模型,模型名称:gemini-3-pro-image-preview,可以结合以下代码来解析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
1、图像生成(画图)教程
提供prompt,由Gemini 3 Pro Image Preview(Nano Banana Pro)生成一张全新的图片:
api_key = "sk-34swdf"
url = f'https://api.bianxie.ai/v1/chat/completions'
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {api_key}'
}
data = {
"model": "gemini-3-pro-image-preview",
"messages": [
{
"role": "user",
"content": "Create a highly realistic 1/7 scale commercialized figure based on the illustration’s adult character, ensuring the appearance and content are safe, healthy, and free from any inappropriate elements. Render the figure in a detailed, lifelike style and environment, placed on a shelf inside an ultra-realistic figure display cabinet, mounted on a circular transparent acrylic base without any text. Maintain highly precise details in texture, material, and paintwork to enhance realism. The cabinet scene should feature a natural depth of field with a smooth transition between foreground and background for a realistic photographic look. Lighting should appear natural and adaptive to the scene, automatically adjusting based on the overall composition instead of being locked to a specific direction, simulating the quality and reflection of real commercial photography. Other shelves in the cabinet should contain different figures which are slightly blurred due to being out of focus, enhancing spatial realism and depth"
}
]
}
response = requests.post(url, headers=headers, json=data)
result = response.json()
if "choices" in result and len(result["choices"]) > 0:
content = result["choices"][0]["message"]["content"]
if isinstance(content, str):
print("\n模型输出内容:", content)
# 提取并保存图片(如果有)
save_base64_image(content, filename_prefix="gemini_3_pro_image_edit")
效果图:
2、图像编辑(改图)教程
提供prompt和原始图,由Gemini 3 Pro Image Preview(Nano Banana Pro)做图片修改,可以通过url或者base64的方式提供原图,例如这里我提供了上一部分由Gemini 3 Pro Image Preview(Nano Banana Pro)生成的图,并让它再加一些点缀:
api_key = "sk-fsdfsdf"
url = f'https://api.bianxie.ai/v1/chat/completions'
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {api_key}'
}
image_base64 = image_to_base64('gemini_3_pro_image_generation.png')
data = {
"model": "gemini-3-pro-image-preview",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": """Add additional scene elements next to the existing figure while keeping the original character fully intact and unchanged. Place a small set of complementary display items on the same shelf inside the ultra-realistic figure display cabinet, such as a miniature plant, a subtle ambient light sphere, or a small secondary figure stand that is visually balanced and non-distracting. These added items should enhance the realism and storytelling of the scene without drawing attention away from the main 1/7 scale commercialized figure.
Maintain the same highly realistic commercial-figure rendering style, including precise textures, lifelike paintwork, natural materials, and accurate lighting reflection. The new objects should follow the same physical scale and lighting behavior as the main figure, seamlessly integrating into the cabinet’s environment.
The shelf should retain natural depth of field with smooth bokeh transition, while the background figures on other shelves remain softly blurred. The circular transparent acrylic base of the main figure should stay unchanged and text-free. Ensure the added items do not introduce any inappropriate, unsafe, or distracting elements; everything should remain clean, healthy, and commercially appropriate. Ensure the final result looks like a professionally photographed high-end figure display scene."""
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{image_base64}"
}
}
]
}
]
}
response = requests.post(url, headers=headers, json=data)
result = response.json()
if "choices" in result and len(result["choices"]) > 0:
content = result["choices"][0]["message"]["content"]
if isinstance(content, str):
print("\n模型输出内容:", content)
# 提取并保存图片(如果有)
save_base64_image(content, filename_prefix="gemini_3_pro_image_edit")
效果如下:







