Claude的视觉能力让您可以在消息中包含图像,并要求Claude以复杂的方式对其进行分析。您可以要求Claude描述图像内容、比较多张图像、计数物体,或执行复杂的视觉分析任务。
图像处理基础

在Claude中处理图像时,您需要了解几个关键限制:
- 单个请求中所有消息中最多可包含100张图像
- 每张图像经base64编码后的最大大小为10MB
- 发送单张图像时:最大高度/宽度为8000px
- 发送多张图像时:最大高度/宽度为2000px
- 图像只能以base64编码形式包含
- 每张图像根据尺寸计算为令牌数:
tokens = ⌈width / 28⌉ × ⌈height / 28⌉
要包含图像,您需要在用户消息中添加一个图像块,与文本块并列。结构如下:
import base64
with open("image.png", "rb") as f:
image_bytes = base64.standard_b64encode(
f.read()
).decode("utf-8")
add_user_message(messages, [
# 图片块
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/png",
"data": image_bytes,
}
},
# 文本块
{
"type": "text",
"text": "What do you see in this image?"
}
])消息流程

对话的工作方式与纯文本交互完全相同。您的服务器向Claude发送一条同时包含图像块和文本块的用户消息,Claude则用分析图像的文本消息作出回应。
提示技巧

关于Claude视觉能力,最重要的一点是:良好的提示技巧绝对至关重要。即使图像清晰,简单的提示也往往会产生不佳的结果。
例如,针对包含12颗弹珠的图像问"这张图片中有多少颗弹珠?",可能会得到错误的计数结果13。您可以通过应用与文本提示相同的提示工程技巧,大幅提高准确性:
- 提供详细的指导方针和分析步骤
- 使用单样本(one-shot)或多样本(multi-shot)示例
- 将复杂任务拆分为更小的步骤
逐步分析

与其提出简单的问题,不如为Claude提供一套方法:
Analyze this image of marbles and determine the exact count using this methodology:
1. Begin by identifying each unique marble one at a time. Assign each a number as you identify it.
2. Verify your result by counting with a different method. Start from the bottom-left corner and work row by row, from left to right.
What is the exact, verified number of marbles in this image?这种结构化的方法帮助Claude得出了正确的计数结果12颗弹珠。
单样本示例

您还可以通过在单条消息中包含多个图像-文本对来使用单样本提示:
[Image of 11 marbles]
The image above has 11 marbles in it.
[Image of 12 marbles]
How many marbles are in this image?提供一个示例可以显著提高Claude在目标图像上的准确性。
真实案例:火灾风险评估

以下是一个实际应用:为房屋保险自动化火灾风险评估。保险公司通常要求房主修剪房产周围的树木以降低野火风险。与其向每处房产派遣检查人员,您可以结合Claude使用卫星图像。
该系统分析卫星图像,以识别:
- 靠近住宅的密集、紧密排列的树木
- 紧急服务难以到达的通道
- 悬垂在住宅上方的树枝
与其使用"给出一个火灾风险评分"这样的简单提示,不如创建一个详细的分析框架:
Analyze the attached satellite image of a property with these specific steps:
1. Residence identification: Locate the primary residence on the property by looking for:
- The largest roofed structure
- Typical residential features (driveway connection, regular geometry)
- Distinction from other structures (garages, sheds, pools)
2. Tree overhang analysis: Examine all trees near the primary residence:
- Identify any trees whose canopy extends directly over any portion of the roof
- Estimate the percentage of roof covered by overhanging branches (0-25%, 25-50%, 50-75%, 75%+)
- Note particularly dense areas of overhang
3. Fire risk assessment: For any overhanging trees, evaluate:
- Potential wildfire vulnerability (ember catch points, continuous fuel paths to structure)
- Proximity to chimneys, vents, or other roof openings if visible
- Areas where branches create a "bridge" between wildland vegetation and the structure
4. Defensible space identification: Assess the property's overall vegetative structure:
- Identify if trees connect to form a continuous canopy over or near the home
- Note any obvious fuel ladders (vegetation that can carry fire from ground to tree to roof)
5. Fire risk rating: Based on your analysis, assign a Fire Risk Rating from 1-4:
- Rating 1 (Low Risk): No tree branches overhanging the roof, good defensible space around
- Rating 2 (Moderate Risk): Minimal overhang (<25% of roof), some separation between tree canopies
- Rating 3 (High Risk): Significant overhang (25-50% of roof), connected tree canopies, multiple points of vulnerability
- Rating 4 (Severe Risk): Extensive overhang (>50% of roof), dense vegetation against structure
For each item above (1-5), write one sentence summarizing your findings, with your final response being the numerical rating.这个全面的提示引导Claude进行系统化分析,从而得出准确且可操作的火灾风险评估。在对一处树木覆盖严重的房产进行测试时,Claude正确地将其判定为"3(高风险)",原因是树木严重悬垂,且建筑周围树冠相连。
关键要点是:Claude的视觉能力强大,但它们需要与处理任何复杂任务时一样细致的提示工程。应投入时间创建详细、结构化的提示,而不是依赖简单的问题。