File size: 2,261 Bytes
2ce4ddf
 
 
 
 
 
1c484d0
2ce4ddf
1c484d0
 
 
 
 
 
 
2ce4ddf
 
1c484d0
2ce4ddf
 
 
 
1c484d0
2ce4ddf
 
1c484d0
2ce4ddf
1c484d0
 
 
2ce4ddf
 
1c484d0
 
2ce4ddf
1b144fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ce4ddf
 
1c484d0
c1eb4e1
 
2ce4ddf
1c484d0
2ce4ddf
1c484d0
 
2ce4ddf
 
 
1c484d0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import base64
from pathlib import Path

import anthropic
from PIL import Image

MODEL = "claude-sonnet-4-6"

_MEDIA_TYPES = {
    ".jpg": "image/jpeg",
    ".jpeg": "image/jpeg",
    ".png": "image/png",
    ".gif": "image/gif",
    ".webp": "image/webp",
}


class ImageProcessingAgent:
    def __init__(self, client: anthropic.Anthropic):
        self.client = client

    def describe(self, image_path: Path, document_context: str = "") -> str:
        media_type = self._media_type(image_path)
        image_data = base64.standard_b64encode(image_path.read_bytes()).decode("utf-8")

        context_note = f" This image appears in {document_context}." if document_context else ""
        prompt = (
            f"Describe this image in detail.{context_note} "
            "Include what it shows, any visible text or labels, the content type "
            "(diagram, chart, screenshot, etc.), and why it might be useful to someone searching this document."
        )

        resp = self.client.messages.create(
            model=MODEL,
            max_tokens=512,
            messages=[
                {
                    "role": "user",
                    "content": [
                        {
                            "type": "image",
                            "source": {
                                "type": "base64",
                                "media_type": media_type,
                                "data": image_data,
                            },
                        },
                        {"type": "text", "text": prompt},
                    ],
                }
            ],
        )

        description = resp.content[0].text.strip()
        # always use a path relative to the output dir — images/ is always next to the .md
        return f"![{image_path.name}](images/{image_path.name})\n\n> **Image description:** {description}\n"

    def _media_type(self, image_path: Path) -> str:
        ext = image_path.suffix.lower()
        if ext not in _MEDIA_TYPES:
            # pillow can handle weird formats — just convert to png
            converted = image_path.with_suffix(".png")
            Image.open(image_path).save(converted, "PNG")
            return "image/png"
        return _MEDIA_TYPES[ext]