Getting started - how to pass images into Claude
Getting started - how to pass images into Claude
Claude models support image inputs in the API. Here’s how you can pass images to Claude:
%pip install anthropic httpx IPython
from IPython.display import Image
Image(filename="../images/sunset.jpeg")

import base64
from anthropic import Anthropic
client = Anthropic()
MODEL_NAME = "claude-opus-5-5"
with open("../images/sunset.jpeg", "rb") as image_file:
binary_data = image_file.read()
base_64_encoded_data = base64.b64encode(binary_data)
base64_string = base_64_encoded_data.decode("utf-8")
message_list = [
{
"role": "user",
"content": [
{
"type": "image",
"source": {"type": "base64", "media_type": "image/jpeg", "data": base64_string},
},
{"type": "text", "text": "Write a sonnet based on this image."},
],
}
]
response = client.messages.create(model=MODEL_NAME, max_tokens=2048, messages=message_list)
print("".join(block.text for block in response.content if block.type == "text"))
**Lighthouse at Dusk** The sun has slipped below the ocean's rim And left a band of ember on the sea; Above, the blue grows deep, the light grows dim, Where one thin moon hangs pale and silently. The water, hushed to silk, forgets its spray, And lies like mist against the pitted stone, Whose ancient face, worn hollow day by day, Has borne the tide's long argument alone. Upon the point, a striped and slender tower Ignites its lamp against the coming night, And holds through every slow and darkened hour A promise kept in one small spark of light. So when my own horizon dims to blue, May some small light stand watch and guide me through.
Passing an image through a url
Passing an image through a url
If you only have a URL of the image you can still pass it to Claude with just a few short lines of code.
IMAGE_URL = "https://upload.wikimedia.org/wikipedia/commons/thumb/c/ca/Machu_Picchu%2C_Peru_%282018%29.jpg/1920px-Machu_Picchu%2C_Peru_%282018%29.jpg"
Image(url=IMAGE_URL)
<IPython.core.display.Image object>
import httpx
image_response = httpx.get(IMAGE_URL, follow_redirects=True)
image_response.raise_for_status() # fail loudly instead of base64-encoding an error page
IMAGE_DATA = base64.b64encode(image_response.content).decode("utf-8")
message_list = [
{
"role": "user",
"content": [
{
"type": "image",
"source": {"type": "base64", "media_type": "image/jpeg", "data": IMAGE_DATA},
},
{"type": "text", "text": "Describe this image in two sentences."},
],
}
]
response = client.messages.create(model=MODEL_NAME, max_tokens=2048, messages=message_list)
print("".join(block.text for block in response.content if block.type == "text"))
This image shows the ancient Inca citadel of Machu Picchu in Peru, with its weathered stone buildings, terraced fields, and bright green central plaza perched on a mountain ridge. Behind the ruins rises the steep, vegetation-covered peak of Huayna Picchu, framed by lush Andean mountains under a vivid blue sky with scattered white clouds.