字
字节笔记本
2026年2月22日
GLM-4V 本地图片使用指南:base64 编码完整教程
本文介绍如何通过 base64 编码在 GLM-4V API 中使用本地图片,包含完整的 cURL 和 Python 示例代码。
问题背景
GLM-4V 视觉模型支持图片输入,但官方示例通常使用远程图片 URL:
json
{
"type": "image_url",
"image_url": {
"url": "https://example.com/image.jpg"
}
}对于本地图片文件,需要使用 base64 数据 URI 格式。
解决方案
核心格式
本地图片必须通过 base64 编码,使用 data:image/{type};base64,{编码} 格式:
json
{
"type": "image_url",
"image_url": {
"url": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQ..."
}
}cURL 完整示例
bash
curl https://ai.bytenote.net/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"model": "glm-4v",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "这张图片里有什么?"
},
{
"type": "image_url",
"image_url": {
"url": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQ..."
}
}
]
}
],
"max_tokens": 300
}'Python 完整示例
python
import base64
import requests
# 1. 读取本地图片并编码
with open("/path/to/image.jpg", "rb") as f:
image_base64 = base64.b64encode(f.read()).decode("utf-8")
# 2. 构建 data URL
data_url = f"data:image/jpeg;base64,{image_base64}"
# 3. 发送请求
response = requests.post(
"https://ai.bytenote.net/v1/chat/completions",
headers={
"Content-Type": "application/json",
"Authorization": "Bearer YOUR_API_KEY"
},
json={
"model": "glm-4v",
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "这张图片里有什么?"},
{
"type": "image_url",
"image_url": {"url": data_url}
}
]
}
],
"max_tokens": 300
}
)
print(response.json())关键要点
| 图片类型 | URL 格式 |
|---|---|
| 远程图片 | https://example.com/image.jpg |
| 本地图片 | data:image/jpeg;base64,{base64编码} |
生成 Base64 编码
macOS/Linux:
bash
base64 -i /path/to/image.jpgPython:
python
import base64
with open("/path/to/image.jpg", "rb") as f:
encoded = base64.b64encode(f.read()).decode("utf-8")
print(f"data:image/jpeg;base64,{encoded}")支持的图片格式
image/jpeg/image/jpgimage/pngimage/webpimage/gif
注意事项
- 文件大小: 建议图片不超过 10MB
- 编码格式: 必须使用标准 base64 编码,不含换行符
- MIME 类型: 根据实际图片格式设置正确的 MIME 类型
- URL 长度: base64 编码后长度约为原文件的 4/3,确保不会超出请求体限制
完整测试脚本
python
#!/usr/bin/env python3
import base64
import requests
import os
API_URL = "https://ai.bytenote.net/v1/chat/completions"
API_KEY = "YOUR_API_KEY"
def call_glm4v(image_path: str, prompt: str = "描述这张图片"):
# 检查文件
if not os.path.exists(image_path):
raise FileNotFoundError(f"文件不存在: {image_path}")
# base64 编码
with open(image_path, "rb") as f:
image_base64 = base64.b64encode(f.read()).decode("utf-8")
# 获取 MIME 类型
ext = os.path.splitext(image_path)[1].lower()
mime_types = {
".jpg": "image/jpeg",
".jpeg": "image/jpeg",
".png": "image/png",
".webp": "image/webp",
}
mime_type = mime_types.get(ext, "image/jpeg")
# 构建 data URL
data_url = f"data:{mime_type};base64,{image_base64}"
# 发送请求
response = requests.post(
API_URL,
headers={
"Content-Type": "application/json",
"Authorization": f"Bearer {API_KEY}"
},
json={
"model": "glm-4v",
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": prompt},
{"type": "image_url", "image_url": {"url": data_url}}
]
}
],
"max_tokens": 1000
}
)
return response.json()
# 使用示例
result = call_glm4v("/path/to/your/image.png", "这张图片里有什么?")
print(result["choices"][0]["message"]["content"])总结
GLM-4V 使用本地图片的核心是将图片文件转换为 base64 编码的数据 URI 格式。通过 data:image/{type};base64,{编码} 的方式,可以在 API 请求中直接嵌入图片内容,无需上传到远程服务器。
分享: