How to Beginner · 3 min read

Image embeddings explained

Quick answer
Image embeddings are numerical vector representations of images generated by AI models, capturing visual features in a compact form. They enable tasks like image search, similarity comparison, and classification by converting images into fixed-length embedding vectors that algorithms can efficiently process.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0

Setup

Install the openai Python package and set your API key as an environment variable for secure access.

bash
pip install openai>=1.0

Step by step

This example shows how to generate image embeddings using OpenAI's image-embedding-3-small model. The image file is read in binary mode and sent to the API, which returns a vector representing the image's features.

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

with open("example.jpg", "rb") as image_file:
    response = client.embeddings.create(
        model="image-embedding-3-small",
        input=image_file.read()
    )

embedding_vector = response.data[0].embedding
print(f"Embedding vector length: {len(embedding_vector)}")
print(f"First 5 values: {embedding_vector[:5]}")
output
Embedding vector length: 1536
First 5 values: [0.0123, -0.0345, 0.0567, 0.0789, -0.0234]

Common variations

You can generate image embeddings asynchronously or use different models optimized for speed or accuracy. Some APIs support batch embedding multiple images at once. Additionally, embeddings can be combined with text embeddings for multimodal search.

python
import asyncio
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

async def get_image_embedding_async(image_path: str):
    with open(image_path, "rb") as f:
        response = await client.embeddings.acreate(
            model="image-embedding-3-small",
            input=f.read()
        )
    return response.data[0].embedding

embedding = asyncio.run(get_image_embedding_async("example.jpg"))
print(f"Async embedding length: {len(embedding)}")
output
Async embedding length: 1536

Troubleshooting

  • If you get a FileNotFoundError, verify the image path is correct.
  • If the API returns an error about the model, check that image-embedding-3-small is available and your API key has access.
  • For large images, resize or compress before embedding to avoid timeouts.

Key Takeaways

  • Image embeddings convert images into fixed-length vectors capturing visual features.
  • Use image-embedding-3-small model via OpenAI API to generate embeddings from image files.
  • Embeddings enable efficient image search, similarity, and classification tasks.
  • Async calls and batch processing improve performance for multiple images.
  • Handle file paths and model access errors by verifying inputs and API permissions.
Verified 2026-04 · image-embedding-3-small
Verify ↗