1 tool3d OP ![]() 说一下如何调用 API ,官网暂时没写。 先访问 https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-exp:streamGenerateContent?key=%s 把上传的猫咪图片压缩成 base64, 塞进 json 里的 inline_data 的 data 里。 然后请求的 json 添加这句:"generationConfig":{"response_modalities":["Text","Image"]} 返回的结果,就能生成图文模式了。 注:暂时没有对应的 openai 兼容调用,直接调用 openrouter 这类 API 中转,应该是没办法生成图片和修图的。必须直接调官方的 google api 。 |
2 leighton 209 天前 ``` 但是我想吐糟一句,google 把所有图片都转成了 base64 ,导致 api json 请求返回巨大。而且很不好写流式图片的加载代码(服务器返回的是 png ,如果是 jpg ,还能边解压边显示)。 ``` 理想的设计是什么样的呢 |
3 77158158 209 天前 这个功能,感觉适合电商批量修图? |
![]() | 4 bskfz 209 天前 |
5 crackidz 209 天前 你用 AI Studio 的话,右上角点击“Get Code” 就可以了吧 |
![]() | 6 binx 209 天前 你可以先用 file API https://ai.google.dev/api/files 上传再用 FileData 在 prompt 里引用就好 |
![]() | 7 iorilu 209 天前 有模型可以做到把视频里面嵌入得字幕去掉吗 |
8 kneo 209 天前 via Android 这不是啥新功能啊,豆包 app 里老早就可以了。api 调用不清楚。 |
9 mayli 209 天前 api 不错,尤其可以直接白嫖这一点就遥遥领先了 |
10 timelessg 209 天前 # 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_fil: img_file.write(image_data) print(f"图片已保存为 {output_file}") break # 找到第一张就退出 else: continue break else: continue break except Exception as e: print(f"处理 Base64 数据时出错: {e}") |
![]() | 12 PositionZero 209 天前 |
![]() | 13 Haku 208 天前 @PositionZero 这种肯定有 nsfw 的限制吧 |
![]() | 14 PositionZero 208 天前 @Haku 肯定 SFW 的,把 safety settings 全关了也不行。可能因为图片里疑似未成年? ![]() |
15 Suger828 208 天前 |
16 A90ur5 208 天前 ![]() |
![]() | 17 rekulas 208 天前 类似技术早就有了,但是完全基于语义理解实现的功能始终还是缺乏约束性,相比而言我还是喜欢国内团队出的模型,可以具体位置精确绘画蒙版然后通过语义进行修改 |
![]() | 19 rekulas 208 天前 @nicenan ReplaceAnything 项目就是,类似项目其实还有好几个 甚至直接基于 ComfyUI 的插件貌似都可以实现,只是对普通人来说可能会麻烦点, 有现成项目的话方便的多 |
![]() | 20 madou 207 天前 生图都那么厉害了,为什么就是让它去掉个水印,直接把中文给干成了乱码 |
![]() | 21 kuonkuon 207 天前 |