AI Prompt Engineering · 课时

多图比较提示

比较两张或更多图像的差异、相似之处和随时间发生的变化

第 3 / 4 课13 个步骤

多图比较提示 是 CoddyKit 上的免费 AI Prompt Engineering 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 AI Prompt Engineering 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 AI Prompt Engineering 课程共包含 4 节课。

多图提示词

视觉模型可以在一次应用程序接口调用中处理多张图片。这使得强大的比较任务成为可能:

  • 前后分析(产品照片、房间翻修、医学影像)
  • 产品变体比较(颜色选项、尺寸比较)
  • 质量比较(为商品页面选择最佳照片)
  • 变化检测(同一文档的两个版本、两张带时间标记的图片)

多图提示词需要仔细组织结构,以便模型知道每张图片分别是什么,以及要分析它们之间的何种关系。

在一次调用中发送多张图片

应用程序接口接受以内容项目列表形式提供的多张图片。请明确标记每张图片:

import anthropic, base64

client = anthropic.Anthropic(api_key='YOUR_API_KEY')

def compare_images(image_path_1, image_path_2, comparison_prompt):
    def encode(path):
        with open(path, 'rb') as f:
            return base64.standard_b64encode(f.read()).decode('utf-8')

    r = client.messages.create(
        model='claude-opus-4-5', max_tokens=600,
        messages=[{'role': 'user', 'content': [
            {'type': 'text', 'text': 'IMAGE 1:'},
            {'type': 'image', 'source': {'type': 'base64', 'media_type': 'image/jpeg', 'data': encode(image_path_1)}},
            {'type': 'text', 'text': 'IMAGE 2:'},
            {'type': 'image', 'source': {'type': 'base64', 'media_type': 'image/jpeg', 'data': encode(image_path_2)}},
            {'type': 'text', 'text': comparison_prompt}
        ]}]
    )
    return r.content[0].text

print('Multi-image comparison function defined.')

两款产品比较提示词

为面向消费者的应用比较两张产品图片:

product_comparison_prompt = '''
Compare the two product images above (Image 1 and Image 2).

Analyze each of the following dimensions:

1. SIMILARITIES: What features, design elements, or characteristics do both products share?

2. DIFFERENCES: What are the key visual differences? Focus on:
   - Color and finish
   - Size and proportions (estimate if possible)
   - Design style (minimalist, ornate, modern, traditional)
   - Materials (if discernible)
   - Quality indicators

3. QUALITY ASSESSMENT: Which image appears to show a higher-quality product, and why?
   Rate each product 1-10 for apparent quality.

4. USE CASE: Based on appearance alone, which product seems better suited for:
   a) Professional/office use
   b) Home/casual use

Return your response in this format exactly.
'''

print(product_comparison_prompt)

前后对比

前后对比提示词需要明确说明哪张是前图、哪张是后图,以及变化发生的背景:

before_after_prompt = '''
You are looking at two images: a BEFORE image (Image 1) and an AFTER image (Image 2).

Analyze the transformation:

1. WHAT CHANGED: List all visible changes from Before to After
2. WHAT STAYED THE SAME: List elements that are unchanged
3. QUALITY IMPROVEMENT: Rate the improvement on a scale of 1-10 (1=no improvement, 10=dramatic improvement)
4. REMAINING ISSUES: What could still be improved that the transformation did not address?

Context: This is a [CONTEXT_PLACEHOLDER] before/after comparison.

Return JSON:
{
  "changes": ["string"],
  "unchanged": ["string"],
  "improvement_score": 1-10,
  "remaining_issues": ["string"],
  "summary": "one sentence summary"
}
'''

# Use contexts: room renovation, product refurbishment, skin care treatment, document cleanup
print('Before/after prompt with JSON output defined.')

照片质量选择

从多个选项中选择最佳照片——适用于电子商务、社交媒体和出版工作流程:

import anthropic, base64, json

client = anthropic.Anthropic(api_key='YOUR_API_KEY')

def select_best_photo(image_paths, use_case='e-commerce product listing'):
    def encode(path):
        with open(path, 'rb') as f:
            return base64.standard_b64encode(f.read()).decode('utf-8')

    content = []
    for i, path in enumerate(image_paths):
        content.append({'type': 'text', 'text': f'IMAGE {i+1}:'})
        content.append({'type': 'image', 'source': {'type': 'base64', 'media_type': 'image/jpeg', 'data': encode(path)}})

    prompt = f'''
You have received {len(image_paths)} images. Select the best one for: {use_case}

Evaluate each on: lighting, composition, clarity, and suitability for the use case.
Return JSON: {{"best_image": 1-{len(image_paths)}, "score_breakdown": [{{"image_id": int, "score": 1-10, "reason": str}}]}}
'''
    content.append({'type': 'text', 'text': prompt})

    r = client.messages.create(model='claude-opus-4-5', max_tokens=300, messages=[{'role': 'user', 'content': content}])
    return json.loads(r.content[0].text)

print('Photo selection function defined.')

变化检测提示词

检测同一图片的两个版本之间发生的具体变化——适用于文档版本管理、用户界面设计评审和监控:

change_detection_prompt = '''
Compare Image 1 (version A) with Image 2 (version B) of the same item.

Identify ALL changes, no matter how small.
For each change:
- Describe what changed
- Where in the image the change occurs (use quadrant: top-left, top-right, bottom-left, bottom-right, center)
- Classify the change: addition, removal, modification, movement

Return JSON:
{
  "total_changes": int,
  "changes": [
    {
      "description": str,
      "location": str,
      "type": "addition|removal|modification|movement"
    }
  ],
  "is_significant_change": true | false
}

If the images appear identical, return: {"total_changes": 0, "changes": [], "is_significant_change": false}
'''

print(change_detection_prompt)

甲/乙设计比较

客观比较两个设计变体——适用于用户界面/用户体验决策和创意方向:

design_comparison_prompt = '''
You are an experienced UX designer reviewing two design variants (Image 1 = Design A, Image 2 = Design B).

Evaluate both designs on:

1. VISUAL HIERARCHY: Which design guides the eye more effectively? Why?
2. READABILITY: Which has better text legibility and information density?
3. BRAND CONSISTENCY: Which feels more professional and polished?
4. USABILITY: Which would be easier for a new user to navigate?
5. EMOTIONAL IMPACT: Which creates a stronger positive first impression?

For each dimension, declare a winner (A or B) and explain in one sentence.

Final verdict: Return JSON:
{
  "winner": "A|B|tie",
  "dimension_winners": {"visual_hierarchy": str, "readability": str, "brand": str, "usability": str, "emotional": str},
  "winning_reasons": [str],
  "recommendation": str
}
'''

print('Design comparison prompt defined.')

结构化相似度评分

对于自动化流程,请生成图片之间的数值相似度分数:

similarity_prompt = '''
Compare these two images and provide a structured similarity analysis.

Return JSON:
{
  "overall_similarity": 0.0-1.0,
  "dimensions": {
    "subject_match": 0.0-1.0,
    "color_match": 0.0-1.0,
    "composition_match": 0.0-1.0,
    "style_match": 0.0-1.0
  },
  "key_differences": [str],
  "are_same_item": true | false | "cannot_determine"
}

Scoring: 1.0 = identical, 0.0 = completely different.
'''

import json
def similarity_score(image_path_1, image_path_2):
    result_text = compare_images(image_path_1, image_path_2, similarity_prompt)
    return json.loads(result_text)

print('Similarity scoring function defined.')
print('Use case: duplicate detection, product matching, visual search.')

处理两张以上图片

对于三张或更多图片,应组织提示词以处理额外的复杂性:

def multi_image_prompt(n_images):
    image_labels = ', '.join(f'Image {i+1}' for i in range(n_images))
    return f'''
You have received {n_images} images: {image_labels}.

Rank all {n_images} images from best to worst for use as a product hero image.
For each image, provide:
- Rank (1=best)
- Score 1-10
- Key strengths
- Key weaknesses

Return JSON:
{{
  "ranking": [
    {{"rank": int, "image_id": int, "score": int, "strengths": [str], "weaknesses": [str]}}
  ],
  "recommended_image": int
}}
'''

# Works for 3, 4, or 5 images
print(multi_image_prompt(3)[:300])

多图提示词中的常见问题

处理多张图片时的常见错误及其避免方法:

  • 不标记图片:模型可能混淆图片的对应关系——请始终在每张图片前用IMAGE 1:等文字进行标记
  • 没有比较框架:未指定比较维度就要求比较,会产生缺乏重点的输出——请准确列出要比较的内容
  • 缺少背景:前后对比提示词需要说明变化背景(例如房间翻修,而不只是两张房间照片)
  • 图片过多:图片达到 6 张以上时质量可能下降——请每批处理 2 至 4 张
  • 忘记指定对象表示法输出:叙述性比较难以解析——对于自动化流程,请始终要求结构化对象表示法输出

时间序列图像分析

当图片代表一个时间序列(每周跟踪记录、施工进度、医疗复诊)时,比较提示词应明确分析随时间推移的发展过程:

temporal_prompt = '''
You are analyzing a sequence of images taken over time.
Image 1 = earliest, Image 2 = most recent.

Analyze the progression:
1. PROGRESS: What improvements or changes occurred from earliest to most recent?
2. REGRESSION: Any deterioration or negative changes?
3. RATE: Is the rate of change faster, slower, or as expected?
4. TRAJECTORY: Based on the trend, what is the likely state in the next period?

Return JSON:
{
  "progress": [str],
  "regression": [str],
  "change_rate": "faster|on_track|slower|stalled",
  "trajectory": str,
  "next_period_prediction": str
}
'''

print("Temporal sequence analysis prompt defined.")
print("Use cases: fitness progress, construction tracking, medical imaging follow-up.")

快速检查

多图比较提示词中最重要的结构要素是什么?

多图比较——要点总结

多图比较提示词可以实现强大的视觉分析能力:

  • 始终在内容数组中的每张图片前添加文字,明确标记图片
  • 指定比较维度——视觉层级、质量、相似度——而不是泛泛地要求比较
  • 前后对比提示词需要说明变化背景(发生了什么类型的变化)
  • 对于自动化流程使用结构化对象表示法输出——相似度分数、排名列表、变化检测
  • 产品比较、照片质量选择和甲/乙设计评审都是高价值应用场景
  • 每批最多处理 2 至 4 张图片——图片更多时质量会下降
  • 变化检测提示词应按类型对每项变化进行分类:添加、移除、修改、移动
免费开始

用 AI 导师学习 AI Prompt Engineering — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
53
课程
199

常见问题解答

「多图比较提示」课时是免费的吗?

是的 — 「多图比较提示」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 AI Prompt Engineering 课程的其余内容,请升级到 CoddyKit PRO。 AI Prompt Engineering 课程共包含 4 节课。

「多图比较提示」这节课中我会学到什么?

比较两张或更多图像的差异、相似之处和随时间发生的变化 你通过在浏览器中直接运行的动手代码来练习 AI Prompt Engineering,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 AI Prompt Engineering 需要有经验吗?

无需任何先前经验。CoddyKit 上的 AI Prompt Engineering 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「多图比较提示」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 AI Prompt Engineering 课中编写并运行代码吗?

能。每节 AI Prompt Engineering 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 图像描述与配文提示
  2. 视觉问答
  3. 多图比较提示
  4. OCR 与文档分析提示
← 返回 AI Prompt Engineering