使用 Gemini 2.5 Flash Image Preview 进行图片编辑、合成和修改。支持多图片输入、文本指令编辑,10秒快速生成。
gemini-2.5-flash-image-preview
POST /v1/chat/completions
{ "model": "gemini-2.5-flash-image-preview", "stream": false, "messages": [ { "role": "user", "content": [ { "type": "text", "text": "编辑指令文本" }, { "type": "image_url", "image_url": { "url": "图片URL1" } }, { "type": "image_url", "image_url": { "url": "图片URL2" } } ] } ] }
#!/usr/bin/env python3 import requests import json import base64 import re from datetime import datetime import sys # 配置 API_KEY = "sk-" # 请替换为你的实际密钥 API_URL = "https://api.apiyi.com/v1/chat/completions" def edit_images(): """ 图片编辑主函数 支持多图片合成和编辑 """ # 设置请求头 headers = { "Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json" } # 请求数据 - 多图片合成示例 data = { "model": "gemini-2.5-flash-image-preview", "stream": False, "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Combine 2 images and add a Corgi dog image" }, { "type": "image_url", "image_url": { "url": "https://github.com/dianping/cat/raw/master/cat-home/src/main/webapp/images/logo/cat_logo03.png" } }, { "type": "image_url", "image_url": { "url": "https://raw.githubusercontent.com/leonindy/camel/master/camel-admin/src/main/webapp/assets/images/camel_logo_blue.png" } } ] } ] } print("🚀 正在请求图片编辑API...") print(f"📝 编辑指令: {data['messages'][0]['content'][0]['text']}") print(f"🖼️ 输入图片数量: {len([c for c in data['messages'][0]['content'] if c['type'] == 'image_url'])}") try: # 发送请求 response = requests.post(API_URL, headers=headers, json=data) response.raise_for_status() print("✅ API请求成功,正在处理响应...") # 解析响应 result = response.json() # 提取内容 content = result['choices'][0]['message']['content'] print(f"📄 收到内容长度: {len(content)} 字符") print(f"📄 内容预览: {content[:200]}...") # 查找Base64图片数据 base64_data = None # 方法1: 查找标准格式 data:image/type;base64,data base64_match = re.search(r'data:image/[^;]+;base64,([A-Za-z0-9+/=]+)', content) if base64_match: base64_data = base64_match.group(1) print("✅ 找到标准格式的Base64数据") else: # 方法2: 查找纯Base64数据(长字符串) base64_match = re.search(r'([A-Za-z0-9+/=]{100,})', content) if base64_match: base64_data = base64_match.group(1) print("✅ 找到纯Base64数据") else: print("❌ 错误: 无法找到Base64图片数据") print("📄 完整响应内容:") print(json.dumps(result, indent=2, ensure_ascii=False)) return False # 生成文件名 timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") filename = f"edited_image_{timestamp}.png" print("💾 正在保存编辑后的图片...") # 解码并保存图片 try: image_data = base64.b64decode(base64_data) with open(filename, 'wb') as f: f.write(image_data) print(f"🎉 图片编辑完成!") print(f"📁 文件保存为: {filename}") print(f"📊 文件大小: {len(image_data):,} 字节") return True except Exception as e: print(f"❌ 错误: 保存图片时出现问题: {e}") return False except requests.exceptions.RequestException as e: print(f"❌ 错误: API请求失败: {e}") return False except KeyError as e: print(f"❌ 错误: 响应格式不正确,缺少字段: {e}") if 'response' in locals(): print("📄 完整响应内容:") print(json.dumps(response.json(), indent=2, ensure_ascii=False)) return False except Exception as e: print(f"❌ 错误: 未知错误: {e}") return False def custom_edit_example(): """ 自定义编辑示例 """ headers = { "Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json" } # 自定义编辑任务 data = { "model": "gemini-2.5-flash-image-preview", "stream": False, "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Please remove the background and make it transparent, then add a sunset background" }, { "type": "image_url", "image_url": { "url": "your_image_url_here" # 替换为你的图片URL } } ] } ] } print("🎨 执行自定义编辑任务...") # 调用编辑逻辑... def main(): """ 主函数 - 演示不同的编辑功能 """ print("=" * 60) print("Nano Banana 图片编辑器 - Python版本") print("=" * 60) print(f"🕐 开始时间: {datetime.now()}") # 检查API Key if API_KEY == "sk-": print("⚠️ 请先设置正确的API密钥") return print("\n🔧 可用的编辑功能:") print("1. 多图片合成") print("2. 背景替换") print("3. 元素添加") print("4. 风格转换") print("\n🚀 执行多图片合成示例...") success = edit_images() print("\n" + "=" * 60) if success: print("🎉 编辑任务执行成功!") print("✅ 图片已保存到本地") else: print("❌ 编辑任务执行失败!") print("💡 请检查API密钥和网络连接") print(f"🕐 结束时间: {datetime.now()}") print("=" * 60) if __name__ == "__main__": success = main() sys.exit(0 if success else 1)
# 将两个logo合成,并添加新元素 { "type": "text", "text": "Combine these two logos and add a cute animal" }
# 替换图片背景 { "type": "text", "text": "Remove the background and add a sunset landscape" }
# 改变图片风格 { "type": "text", "text": "Convert this photo to anime/cartoon style" }
# 添加或删除元素 { "type": "text", "text": "Add sunglasses to the person and change hair color to blue" }
图片URL无法访问
编辑结果不符合预期
API响应中没有图片数据
def batch_edit_images(image_urls, edit_instruction): """批量编辑多张图片""" results = [] for i, url in enumerate(image_urls): data = { "model": "gemini-2.5-flash-image-preview", "messages": [{ "role": "user", "content": [ {"type": "text", "text": edit_instruction}, {"type": "image_url", "image_url": {"url": url}} ] }] } # 执行编辑... results.append(result) return results
def save_edit_history(original_url, instruction, result_path): """保存编辑历史记录""" history = { "timestamp": datetime.now().isoformat(), "original_image": original_url, "edit_instruction": instruction, "result_path": result_path } # 保存到文件或数据库...