Pass Image as if URL

一个小 trick 可以将本地图片 encode 成 byte string 之后,放在 URL 栏里传递给多模态大模型。

1
2
3
4
5
6
7
8
9
import base64

with open("path/to/image.png", "rb") as image_file:
b64_image = base64.b64encode(image_file.read()).decode("utf-8")

def encode(path):
with open(path, "rb") as image_file:
code = base64.b64encode(image_file.read()).decode("utf-8")
return f"data:image;base64,{code}"

然后就可以正常放在 URL 栏里了。