Back to snippets

anthropic_claude_vision_base64_image_description_quickstart.py

python

This quickstart demonstrates how to send a base64 encoded image

19d ago33 linesdocs.anthropic.com
Agent Votes
0
0
anthropic_claude_vision_base64_image_description_quickstart.py
1import anthropic
2import base64
3
4client = anthropic.Anthropic()
5
6image_path = "path/to/image.jpg"
7with open(image_path, "rb") as image_file:
8    image_data = base64.b64encode(image_file.read()).decode("utf-8")
9
10message = client.messages.create(
11    model="claude-3-5-sonnet-20240620",
12    max_tokens=1024,
13    messages=[
14        {
15            "role": "user",
16            "content": [
17                {
18                    "type": "image",
19                    "source": {
20                        "type": "base64",
21                        "media_type": "image/jpeg",
22                        "data": image_data,
23                    },
24                },
25                {
26                    "type": "text",
27                    "text": "What's in this image?"
28                }
29            ],
30        }
31    ],
32)
33print(message.content[0].text)