Analyze Image

Use vision-capable APIs to analyze and describe images.

Process

  1. Prepare image - Ensure file is in supported format (JPG, PNG)
  2. Set up API credentials - Check for required environment variables
  3. Encode image - Convert to base64 for API transmission
  4. Craft prompt - Specify what you want to know about the image
  5. Send API request - Use appropriate vision API endpoint
  6. 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