# This is a sample Python script.
import base64
# Press ⌃R to execute it or replace it with your code.
# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings.
import requests
import json
import mimetypes
import os
BASE_URL = "
https://generativelanguage.googleapis.com"
GEMINI_API_KEY = ""
IMG_PATH_2 = ""
DISPLAY_NAME = "TEXT"
# 获取 MIME 类型和文件大小
MIME_TYPE, _ = mimetypes.guess_type(IMG_PATH_2)
NUM_BYTES = os.path.getsize(IMG_PATH_2)
# 发送初始 resumable 上传请求
data = {"file": {"display_name": DISPLAY_NAME}}
headers = {
"X-Goog-Upload-Protocol": "resumable",
"X-Goog-Upload-Command": "start",
"X-Goog-Upload-Header-Content-Length": str(NUM_BYTES),
"X-Goog-Upload-Header-Content-Type": MIME_TYPE,
"Content-Type": "application/json",
}
response =
requests.post(
f"{BASE_URL}/upload/v1beta/files?key={GEMINI_API_KEY}",
headers=headers,
json=data
)
# 提取上传 URL
upload_url = response.headers.get("X-Goog-Upload-URL")
if not upload_url:
print("Failed to get upload URL.")
exit(1)
# 读取文件数据并上传
with open(IMG_PATH_2, "rb") as f:
file_data = f.read()
headers = {
"Content-Length": str(NUM_BYTES),
"X-Goog-Upload-Offset": "0",
"X-Goog-Upload-Command": "upload, finalize"
}
response =
requests.post(upload_url, headers=headers, data=file_data)
file_info = response.json()
file_uri = file_info.get("file", {}).get("uri")
if not file_uri:
print("Failed to get file URI.")
exit(1)
print(f"file_uri={file_uri}")
# 生成内容请求
data = {
"contents": [{
"parts": [
{"text": "把全部人脸替换成猫头"},
{"file_data": {"mime_type": "image/jpeg", "file_uri": file_uri}}
]
}],
"generationConfig": {"response_modalities": ["Text", "Image"]}
}
headers = {"Content-Type": "application/json"}
response =
requests.post(
f"{BASE_URL}/v1beta/models/gemini-2.0-flash-exp:streamGenerateContent?key={GEMINI_API_KEY}",
headers=headers,
json=data
)
response_json = response.json()
with open("response.json", "w", encoding="utf-8") as f:
json.dump(response_json, f, ensure_ascii=False, indent=4)
try:
for item in response_json: # 遍历列表
candidates = item.get("candidates", [])
for candidate in candidates:
content = candidate.get("content", {})
parts = content.get("parts", [])
for part in parts:
inline_data = part.get("inlineData")
if inline_data and "data" in inline_data:
base64_data = inline_data["data"]
mime_type = inline_data.get("mimeType", "image/png")
# 生成对应的文件扩展名
ext = "jpg" if "jpeg" in mime_type else "png"
output_file = f"output.{ext}"
# 解码 Base64 并保存为图片
image_data = base64.b64decode(base64_data)
with open(output_file, "wb") as img_file:
img_file.write(image_data)
print(f"图片已保存为 {output_file}")
break # 找到第一张就退出
else:
continue
break
else:
continue
break
except Exception as e:
print(f"处理 Base64 数据时出错: {e}")