图像描述与配文提示
引导模型关注对象、关系、情绪和技术细节
图像描述与配文提示 是 CoddyKit 上的免费 AI Prompt Engineering 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 AI Prompt Engineering 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 AI Prompt Engineering 课程共包含 4 节课。
视觉模型与提示词
视觉语言模型(VLM)例如 GPT-4o 和 Claude,可以同时处理图片和文本。您随图片发送的提示词会极大影响模型描述的质量、重点和格式。
如果没有引导性提示词,模型会自行决定描述什么,这可能与您的需求不符。结构化描述提示词会明确告诉模型应关注哪些元素,以及如何组织输出。
通过提示词发送图片
Anthropic 应用程序接口接受经过 Base64 编码的内容或网址形式的图片。基本结构如下:
import anthropic, base64
client = anthropic.Anthropic(api_key='YOUR_API_KEY')
with open('image.jpg', 'rb') as f:
image_data = base64.standard_b64encode(f.read()).decode('utf-8')
response = client.messages.create(
model='claude-opus-4-5',
max_tokens=500,
messages=[{
'role': 'user',
'content': [
{
'type': 'image',
'source': {
'type': 'base64',
'media_type': 'image/jpeg',
'data': image_data
}
},
{
'type': 'text',
'text': 'Describe this image in detail.'
}
]
}]
)
print(response.content[0].text)无结构描述提示词
最简单的提示词——描述这张图片——完全由模型的优先级决定输出形式。对于许多使用场景而言,这并不够:
- 模型可能会关注视觉上最醒目的元素,而不是最相关的元素
- 对于相似图片,描述的长度和组织方式可能差异很大
- 重要细节(文字、小物体和背景环境)经常会被遗漏
结构化描述提示词可以解决所有这些问题。
结构化描述:引导注意力
结构化描述提示词会明确引导模型关注特定的视觉元素:
structured_prompt = '''
Describe this image in detail, addressing each of the following aspects:
1. FOREGROUND: Main subjects and objects in the foreground
2. BACKGROUND: Setting, environment, and background elements
3. COLORS: Dominant color palette and notable color contrasts
4. MOOD: Emotional tone, atmosphere, and lighting
5. TEXT: Any visible text, signs, labels, or written content
6. PEOPLE: If people are present — count, approximate age, pose, expression
Organize your response using these exact section headers.
Be specific and descriptive. Avoid vague terms like "some" or "various".
'''
print(structured_prompt)控制描述长度
同一张图片在不同使用场景下可能需要不同长度的描述。请在提示词中明确控制长度:
# For image captions in a product catalog
short_prompt = '''
Write a 1-sentence product image caption (under 15 words).
Focus on the product, its key feature, and setting.
'''
# For accessibility alt-text
alt_text_prompt = '''
Write an image alt-text description for a visually impaired user.
Limit: 125 characters.
Include: what the image shows, any text visible in the image, the most important action or emotion.
'''
# For detailed analysis
detailed_prompt = '''
Write a detailed image analysis of 200-300 words.
Cover: composition, subjects, setting, colors, mood, and any notable technical or artistic elements.
Structure as a single flowing paragraph.
'''
print('Three length-controlled description prompts defined.')特定领域的描述提示词
不同领域需要使用不同的描述词汇并关注不同的重点:
# Medical imaging description
medical_prompt = '''
Describe the key visual findings in this medical image.
Focus on: anatomical structures visible, any abnormalities or anomalies,
location using standard anatomical terms (left/right, superior/inferior, medial/lateral),
and image quality or artifacts.
Note: this description is for informational purposes only, not diagnostic.
'''
# Architecture / real estate
architecture_prompt = '''
Describe this property image for a real estate listing.
Cover: room type, approximate size, key features (flooring, ceiling, natural light),
condition, notable fixtures or finishes, and overall style.
Tone: professional, appealing, factual.
'''
# Security / surveillance
security_prompt = '''
Describe this security camera image.
Note: number of people, approximate location in frame, clothing colors,
any objects being carried, direction of movement, and time of day if discernible.
'''
print('Domain-specific prompts defined.')从图片描述中生成结构化输出
对于自动化流水线,与其让图片描述输出自然语言段落,不如请求结构化 JSON 输出:
json_description_prompt = '''
Analyze this product image and return a JSON description:
{
"product_name": "inferred product name or null",
"category": "electronics|clothing|furniture|food|other",
"colors": ["primary color", "secondary color"],
"condition": "new|used|unclear",
"background": "white|lifestyle|outdoor|studio|other",
"people_visible": true | false,
"text_visible": "extracted text or null",
"quality_score": 1-10,
"caption": "one sentence product caption"
}
Return only the JSON object.
'''
print(json_description_prompt)注重无障碍的描述
为无障碍需求撰写图片描述,需要采用特定的提示词风格,优先提供视障用户所需的信息:
accessibility_prompt = '''
Write an image description optimized for screen reader accessibility.
Guidelines:
- Start with the most important content (what is this image about?)
- Describe spatial relationships (the man on the left, the building in the background)
- Include all visible text verbatim
- Describe faces and expressions if relevant to the content
- Skip decorative descriptions unless they convey meaning
- End with: if this is a graph or chart, include the key data it shows
- Maximum 250 characters for alt-text. If more is needed, write a 1-sentence alt-text plus a longer caption.
'''
print(accessibility_prompt)避免描述提示词中的常见错误
图片描述提示词中的常见错误及修复方法:
- 过于模糊:描述这张图片 → 修复:明确指定要描述哪些元素
- 没有格式:模型在您需要 JSON 时写出自然语言段落 → 修复:明确指定输出格式
- 没有长度限制:模型写出 1000 个字 → 修复:指定目标长度
- 没有重点:平等地描述所有元素 → 修复:指定哪个元素是主要元素
- 没有领域背景:为专业图片生成通用描述 → 修复:加入领域词汇和重点标准
批量图片描述流水线
在自动化流水线中处理多张图片:
import anthropic, base64, json
from pathlib import Path
client = anthropic.Anthropic(api_key='YOUR_API_KEY')
DESCRIPTION_PROMPT = '''
Describe this image for a product catalog.
Return JSON: {"caption": str, "colors": [str], "category": str, "alt_text": str}
'''
def describe_image(image_path):
with open(image_path, 'rb') as f:
img_b64 = base64.standard_b64encode(f.read()).decode('utf-8')
r = client.messages.create(
model='claude-opus-4-5', max_tokens=200,
messages=[{'role': 'user', 'content': [
{'type': 'image', 'source': {'type': 'base64', 'media_type': 'image/jpeg', 'data': img_b64}},
{'type': 'text', 'text': DESCRIPTION_PROMPT}
]}]
)
return json.loads(r.content[0].text)
print('Batch image description pipeline defined.')测试描述提示词质量
在多样化的图片集上测试描述提示词,以确保覆盖范围和一致性:
- 白色背景上的简单产品
- 包含多人的生活方式照片
- 文字密集的文档或标志
- 昏暗或低质量的图片
- 抽象或有歧义的内容
对于每张测试图片,请检查输出是否涵盖所有必需元素、遵守长度限制并使用所需格式。任何类别出现系统性失败时,都应调整提示词。
快速检查
与简单的“描述这张图片”提示词相比,结构化图片描述提示词的主要优势是什么?
图片描述提示词——要点总结
结构化图片描述提示词对于生成一致且有用的视觉人工智能输出至关重要:
- 明确列出要描述的视觉元素(前景、背景、颜色、氛围、文字和人物)
- 指定输出格式——自然语言段落、JSON 或特定的章节标题
- 明确控制长度——根据使用场景调整长度(15 字说明与 250 字分析)
- 特定领域的提示词(医疗、房地产和安全)需要使用领域词汇和重点标准
- 对于无障碍需求,应优先提供信息而不是美观效果,并原样包含所有可见文字
- 在多样化的图片类型上进行测试:产品、生活方式、文字密集、低质量和抽象图片
常见问题解答
「图像描述与配文提示」课时是免费的吗?
是的 — 「图像描述与配文提示」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 AI Prompt Engineering 课程的其余内容,请升级到 CoddyKit PRO。 AI Prompt Engineering 课程共包含 4 节课。
「图像描述与配文提示」这节课中我会学到什么?
引导模型关注对象、关系、情绪和技术细节 你通过在浏览器中直接运行的动手代码来练习 AI Prompt Engineering,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 AI Prompt Engineering 需要有经验吗?
无需任何先前经验。CoddyKit 上的 AI Prompt Engineering 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「图像描述与配文提示」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 AI Prompt Engineering 课中编写并运行代码吗?
能。每节 AI Prompt Engineering 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 图像描述与配文提示
- 视觉问答
- 多图比较提示
- OCR 与文档分析提示