How to analyze image with OpenAI GPT-4o
Quick answer
Use the
gpt-4o model with OpenAI's Python SDK to analyze images by sending the image URL or base64 data in the messages array with image_url or image_base64 fields. The model returns a text analysis of the image content.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.
pip install openai>=1.0 Step by step
This example sends an image URL to gpt-4o for analysis and prints the textual description returned by the model.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
messages = [
{
"role": "user",
"content": "Analyze the content of this image.",
"image_url": {
"url": "https://images.unsplash.com/photo-1506744038136-46273834b3fb"
}
}
]
response = client.chat.completions.create(
model="gpt-4o",
messages=messages
)
print(response.choices[0].message.content) output
A scenic photo showing a mountain landscape with a clear blue sky and some clouds, featuring lush greenery in the foreground.
Common variations
- Use
image_base64instead ofimage_urlto send images encoded in base64. - Switch to
gpt-4o-minifor faster, cheaper analysis with slightly reduced accuracy. - Use async calls with
asynciofor concurrent image analysis.
import os
import asyncio
from openai import OpenAI
async def analyze_image():
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
messages = [
{
"role": "user",
"content": "Describe this image.",
"image_url": {"url": "https://images.unsplash.com/photo-1506744038136-46273834b3fb"}
}
]
response = await client.chat.completions.acreate(
model="gpt-4o",
messages=messages
)
print(response.choices[0].message.content)
asyncio.run(analyze_image()) output
A scenic photo showing a mountain landscape with a clear blue sky and some clouds, featuring lush greenery in the foreground.
Troubleshooting
- If you get an authentication error, verify your
OPENAI_API_KEYenvironment variable is set correctly. - If the image is not analyzed, ensure the URL is publicly accessible and uses HTTPS.
- For large images, consider resizing or compressing before sending to avoid timeouts.
Key Takeaways
- Use
gpt-4owithimage_urlorimage_base64in messages to analyze images. - Always set your API key in
os.environ["OPENAI_API_KEY"]for secure authentication. - Async calls enable efficient batch image analysis with the OpenAI Python SDK.
- Ensure images are accessible via HTTPS URLs or properly encoded base64 strings.
- Use smaller or compressed images to avoid request timeouts or errors.