V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  timelessg  ›  全部回复第 1 页 / 共 35 页
回复总数  695
1  2  3  4  5  6  7  8  9  10 ... 35  
# 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}")
cybex carbon
18 天前
回复了 darkce 创建的主题 程序员 字节的 Lynx 跨端框架开源了
缺的永远不是跨平台框架,缺的是生态,flutter ,rn 已经把生态做起来了,而像是 skip 之类据说可以用 swifui 写 Android 的框架没生态谁会用,除了 UI 两端还要写一堆 actual
32 天前
回复了 Chicagoake 创建的主题 Mac mini Mac mini 搭配外接硬盘求推荐
硬盘选无缓,盒子尽量选 jhl7440 主控,祥硕发热略大
Xcode 确实🌶︎🐔,比起花里胡哨的 Android studio ,🍎这几年真是不思进取
58 天前
回复了 shonnliberty 创建的主题 分享发现 kiwi 作者弃坑了
应该是作者向 edge 合并了 extension 的支持代码,相当于交给 edge 维护了
61 天前
回复了 BeijingBaby 创建的主题 音乐 多端同步听音乐是个很困难的事
spotify
挺好的,阮大神之前不就是这公司的
67 天前
回复了 Int100 创建的主题 程序员 Mac 外接硬盘该怎么选择? 求推荐
如果是当系统盘,建议雷电 4 而且是 jhl8440 主控的,发热相对小不容易掉盘,固态就相对无所谓了,反正主控最多能跑到 3000
68 天前
回复了 magic3584 创建的主题 Android 如何入门 Android 开发
别看 Android view 了,直接上 compose
88 天前
回复了 hao7Chen 创建的主题 日本 第一次日本自由行的个人心得
日本无现金支付还是可以的,下次店里碰到 paypay 的二维码可以用支付宝扫试试能不能付款
google voice
爱情神话的导演啊,值得一看
114 天前
回复了 blackguester 创建的主题 Swift 问一个关于 CALayer 的资源释放问题
当然是都要做了,不然引用一直在
Android 可以直接上 compose 了
这么看起来我跑量还是太少了,一个月只有 120~150 ,半马 150 ,但加跑量又感觉膝盖要废,op 真的很厉害
suica 买个日版 pixel watch or Garmin 不就行了
1  2  3  4  5  6  7  8  9  10 ... 35  
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1014 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 43ms · UTC 19:45 · PVG 03:45 · LAX 12:45 · JFK 15:45
Developed with CodeLauncher
♥ Do have faith in what you're doing.