Analyze Image
Use vision-capable APIs to analyze and describe images.
Process
- Prepare image - Ensure file is in supported format (JPG, PNG)
- Set up API credentials - Check for required environment variables
- Encode image - Convert to base64 for API transmission
- Craft prompt - Specify what you want to know about the image
- Send API request - Use appropriate vision API endpoint
- Parse response - Extract the analysis from API response
Examples
python3 - << 'PY'
import os
import base64
import json
import requests
image_path = "/path/to/image.jpg"
api_key = os.environ.get("ANTHROPIC_API_KEY")
if not api_key:
raise SystemExit("ANTHROPIC_API_KEY not set")
with open(image_path, "rb") as f:
image_b64 = base64.b64encode(f.read()).decode("utf-8")
payload = {
"model": "claude-sonnet-4-5",
"max_tokens": 512,
"messages": [
{
"role": "user",
"content": [
{"type": "image", "source": {"type": "base64", "media_type": "image/jpeg", "data": image_b64}},
{"type": "text", "text": "Describe the image"}
]
}
]
}
resp = requests.post(
"https://api.anthropic.com/v1/messages",
headers={"x-api-key": api_key, "content-type": "application/json"},
data=json.dumps(payload),
)
print(resp.status_code)
print(resp.text)
PY
Tips
- Prefer JPG/PNG with reasonable size.
- Redact secrets before sending.
- If a vision API is not available, ask for human review.