Sub topic 2
Sending Your First Image
Assume you know basic Python, JSON, and how to call an LLM API. You should also understand that tokens measure input and output text, while an image is processed as visual input.
Sending an Image as a Content Block
A multimodal model receives typed content blocks. A user message can contain text and an image in the same content list. This example reads a local image, encodes it as Base64, and sends it with a question.
import base64
import mimetypes
import os
import anthropic
def image_as_base64(path):
media_type, _ = mimetypes.guess_type(path)
if media_type not in {"image/png", "image/jpeg", "image/gif", "image/webp"}:
raise ValueError("Use a supported PNG, JPEG, GIF, or WebP image")
with open(path, "rb") as image_file:
data = base64.standard_b64encode(image_file.read()).decode("ascii")
return media_type, data
media_type, data = image_as_base64("chart.png")
client = anthropic.Anthropic()
message = client.messages.create(
model=os.environ["ANTHROPIC_MODEL"],
max_tokens=300,
messages=[{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "base64",
"media_type": media_type,
"data": data,
},
},
{"type": "text", "text": "Describe the trend in this chart."},
],
}],
)
print(message.content[0].text)A provider may also accept an HTTPS image URL. Use a URL when the provider can fetch it safely and the image is already hosted. Use Base64 for a local file or a short-lived request.
Prepare Images Before Sending
Resize very large images and normalize their format. Smaller inputs usually upload faster and cost less. Preserve enough detail for labels, tables, and other information the model must read.
from PIL import Image
with Image.open("large_chart.png") as image:
image = image.convert("RGB")
image.thumbnail((1568, 1568))
image.save("chart.jpg", format="JPEG", quality=90)Cost, Latency, and Multiple Images
Visual inputs consume processing capacity based on their dimensions and detail. Providers convert images into internal visual tokens or tiles, so exact costs differ. Larger images and more pages usually increase both cost and latency.
When sending several images, label them in the surrounding prompt. Put the instruction near the images it describes, and state whether the model should compare them or process them independently.
Sending a PDF Directly
If the provider supports native PDF input, send the PDF as a document block. Native processing can preserve text and page visuals better than treating the whole file as an image.
import anthropic
with open("report.pdf", "rb") as pdf_file:
pdf_data = pdf_file.read()
client = anthropic.Anthropic()
message = client.messages.create(
model="MODEL_NAME",
max_tokens=500,
messages=[{
"role": "user",
"content": [
{
"type": "document",
"source": {
"type": "base64",
"media_type": "application/pdf",
"data": __import__("base64").b64encode(pdf_data).decode("ascii"),
},
},
{"type": "text", "text": "List the report's three main findings."},
],
}],
)
print(message.content[0].text)If native PDF input is unavailable, render each page as an image or extract its text. Choose the method that preserves the information your task needs.
Exercise
Why might you resize an image before sending it to a multimodal model?
Show answer
Resizing can reduce upload time and visual processing cost while retaining enough detail for the task.