How to analyze multiple images with OpenAI
Quick answer
Use the OpenAI Python SDK's
client.chat.completions.create method with a model that supports image inputs like gpt-4o. Pass multiple images as image_url or base64-encoded data in the messages array to analyze them in one request.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install openai>=1.0
Setup
Install the OpenAI Python SDK and set your API key as an environment variable.
- Run
pip install openai>=1.0to install the SDK. - Set your API key in your shell:
export OPENAI_API_KEY='your_api_key_here'(Linux/macOS) orsetx OPENAI_API_KEY "your_api_key_here"(Windows).
pip install openai>=1.0 Step by step
This example shows how to analyze multiple images by sending them as separate messages with image_url content to the gpt-4o model. The response will contain analysis for each image.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
# List of image URLs to analyze
image_urls = [
"https://example.com/image1.png",
"https://example.com/image2.jpg"
]
# Prepare messages with images
messages = []
for idx, url in enumerate(image_urls, 1):
messages.append({
"role": "user",
"content": f"Analyze this image #{idx}",
"image_url": {"url": url}
})
response = client.chat.completions.create(
model="gpt-4o",
messages=messages
)
# Print the analysis for each image
for i, choice in enumerate(response.choices, 1):
print(f"Analysis for image #{i}:")
print(choice.message.content)
print("---") output
Analysis for image #1: Description, objects detected, or other insights about image1.png --- Analysis for image #2: Description, objects detected, or other insights about image2.jpg ---
Common variations
- Use base64-encoded images by replacing
image_urlwithimage_base64in the message. - Switch to
gpt-4o-minifor faster, cheaper analysis with slightly less detail. - Use asynchronous calls with
asynciofor concurrent image analysis.
import os
import asyncio
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
async def analyze_images_async(image_urls):
messages = [{
"role": "user",
"content": f"Analyze this image #{i+1}",
"image_url": {"url": url}
} for i, url in enumerate(image_urls)]
response = await client.chat.completions.acreate(
model="gpt-4o",
messages=messages
)
for i, choice in enumerate(response.choices, 1):
print(f"Async analysis for image #{i}:")
print(choice.message.content)
print("---")
image_urls = ["https://example.com/image1.png", "https://example.com/image2.jpg"]
asyncio.run(analyze_images_async(image_urls)) output
Async analysis for image #1: Description, objects detected, or other insights about image1.png --- Async analysis for image #2: Description, objects detected, or other insights about image2.jpg ---
Troubleshooting
- If you get an authentication error, verify your
OPENAI_API_KEYenvironment variable is set correctly. - If images fail to load, check that URLs are publicly accessible and use HTTPS.
- For large images or many images, watch for token limits and split requests accordingly.
Key Takeaways
- Use the OpenAI Python SDK with
gpt-4oto analyze multiple images in one request. - Pass images as
image_urlor base64 in separate messages within themessagesarray. - Handle large batches by splitting requests to avoid token limits and use async calls for concurrency.