> ## Documentation Index
> Fetch the complete documentation index at: https://docs.openp.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# 视觉(图片输入)

> 传入图片让模型读懂、描述、抽取信息

主流多模态模型(GPT-4o、Claude 4.x、Gemini 2.5)都支持 **图片输入**。
你可以让模型描述图片、识别物体、读 OCR、生成 alt text、回答与图片有关的问题。

## 支持的模型

| 模型                                      | OpenAI 协议 | Claude 协议 | Gemini 协议 |
| --------------------------------------- | :-------: | :-------: | :-------: |
| `gpt-5.5`、`gpt-5.4-mini`                |     ✅     |     —     |     —     |
| `o3` 系列                                 |     ✅     |     —     |     —     |
| `claude-haiku-4-5`、`sonnet`、`opus`      |  ✅(自动转换)  |     ✅     |     —     |
| `gemini-3.1-pro-preview` / `flash-lite` |  ✅(自动转换)  |     —     |     ✅     |
| `qwen-vl-*`                             |     ✅     |     —     |     —     |

## OpenAI 协议

### 通过 URL

```json theme={null}
{
  "model": "gpt-5.5",
  "messages": [{
    "role": "user",
    "content": [
      {"type": "text", "text": "图里是什么动物?"},
      {"type": "image_url", "image_url": {
        "url": "https://example.com/cat.jpg",
        "detail": "high"
      }}
    ]
  }]
}
```

`detail` 取值:`low`(快、便宜)/ `high`(高分辨率,贵)/ `auto`(默认)。

### 通过 base64

```python theme={null}
import base64
with open("photo.jpg", "rb") as f:
    b64 = base64.b64encode(f.read()).decode()

resp = client.chat.completions.create(
    model="gpt-5.5",
    messages=[{"role": "user", "content": [
        {"type": "text", "text": "描述这张图"},
        {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{b64}"}}
    ]}]
)
```

支持格式:`image/png` / `jpeg` / `gif` / `webp`。

## Claude 协议

```json theme={null}
{
  "model": "claude-opus-4-8",
  "max_tokens": 1024,
  "messages": [{
    "role": "user",
    "content": [
      {"type": "text", "text": "OCR 这张图"},
      {"type": "image", "source": {
        "type": "url",
        "url": "https://example.com/doc.png"
      }}
    ]
  }]
}
```

或 base64:

```json theme={null}
{"type": "image", "source": {
  "type": "base64",
  "media_type": "image/png",
  "data": "<base64>"
}}
```

## Gemini 协议

```json theme={null}
{
  "contents": [{
    "role": "user",
    "parts": [
      {"text": "图里在做什么?"},
      {"inline_data": {"mime_type": "image/png", "data": "<base64>"}}
    ]
  }]
}
```

Gemini 还支持 `file_data`(已上传文件)与 PDF / 视频部分:

```json theme={null}
{"file_data": {"mime_type": "application/pdf", "file_uri": "https://..."}}
```

## 多图输入

把多个图片对象放进同一条 message 即可:

```json theme={null}
{
  "role": "user",
  "content": [
    {"type": "text", "text": "对比两张图的差异"},
    {"type": "image_url", "image_url": {"url": "https://.../a.jpg"}},
    {"type": "image_url", "image_url": {"url": "https://.../b.jpg"}}
  ]
}
```

* GPT-4o:单次最多约 50 张。
* Claude:单次最多约 20 张。
* Gemini:单次最多约 3000 张图片或 1 个长视频。

## 计费

图片输入按 **像素 → token** 的策略折算计入 `prompt_tokens`,具体规则取决于上游:

* **OpenAI**:`detail: low` ≈ 85 token / 张;`high` 按 512×512 tiles × 170 token 估算。
* **Claude**:`(width × height) / 750` token,1024×1024 ≈ 1400 token。
* **Gemini**:每张图固定 258 token。

控制台日志会展示实际 `prompt_tokens` 中的图片部分。

## 最佳实践

* **保持图片清晰**:模糊或低分辨率会显著降低识别精度。
* **必要时缩放**:大图压缩到 1024-2048 长边即可,可省 token。
* **明确问题**:不要只说 "描述图片",问具体问题(对象 / 位置 / 数字 / 文字 / 颜色)效果更好。
* **OCR 用专业模型**:文档 OCR 推荐 `claude-opus-4-8` 或 `gemini-3.1-pro-preview`,中文文档可考虑 `qwen-vl-max`。
