Skip to content

Image Embeddings Fresh 🌱

Embed images into the unified vector space for cross-modal search, classification, and clustering.

Overview

Specifications

ParameterValue
Modelgemini-embedding-2-preview
Max images per request6
Supported formatsPNG, JPEG
Default dimensions3,072
Dimension range128 - 3,072
Pricing (Standard)$0.45 / 1M tokens (~$0.00012 per image)
Pricing (Batch)$0.225 / 1M tokens (~$0.00006 per image)

Step 1: Embed a Single Image (Python)

python
from google import genai
from google.genai import types

with open('example.png', 'rb') as f:
    image_bytes = f.read()

client = genai.Client()

result = client.models.embed_content(
    model='gemini-embedding-2-preview',
    contents=[
        types.Part.from_bytes(
            data=image_bytes,
            mime_type='image/png',
        ),
    ]
)

embedding = result.embeddings[0].values
print(f"Dimensions: {len(embedding)}")  # 3072

Step 2: Embed a Single Image (Node.js)

javascript
import { GoogleGenAI } from "@google/genai";
import * as fs from "node:fs";

const ai = new GoogleGenAI({});
const imgBase64 = fs.readFileSync("example.png", { encoding: "base64" });

const response = await ai.models.embedContent({
    model: 'gemini-embedding-2-preview',
    contents: [{
        inlineData: {
            mimeType: 'image/png',
            data: imgBase64,
        },
    }],
});

console.log(response.embeddings[0].values.length);  // 3072

Step 3: Embed via cURL

bash
# Base64-encode the image first
IMG_BASE64=$(base64 -w 0 example.png)

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-embedding-2-preview:embedContent" \
    -H "Content-Type: application/json" \
    -H "x-goog-api-key: ${GEMINI_API_KEY}" \
    -d '{
        "content": {
            "parts": [{
                "inline_data": {
                    "mime_type": "image/png",
                    "data": "'"${IMG_BASE64}"'"
                }
            }]
        }
    }'

Step 4: Multiple Images in One Request

Up to 6 images can be sent in a single request. Each image gets its own embedding.

python
from google import genai
from google.genai import types
import glob

client = genai.Client()
image_files = glob.glob('images/*.png')[:6]  # Max 6

contents = []
for path in image_files:
    with open(path, 'rb') as f:
        contents.append(
            types.Part.from_bytes(data=f.read(), mime_type='image/png')
        )

result = client.models.embed_content(
    model='gemini-embedding-2-preview',
    contents=contents
)

for i, emb in enumerate(result.embeddings):
    print(f"Image {i+1}: {len(emb.values)} dimensions")

Search images using text queries — both map to the same vector space.

python
from google import genai
from google.genai import types
import numpy as np

client = genai.Client()

# Embed the text query
query_result = client.models.embed_content(
    model='gemini-embedding-2-preview',
    contents='a sunset over the ocean',
    config=types.EmbedContentConfig(task_type='RETRIEVAL_QUERY')
)
query_vec = np.array(query_result.embeddings[0].values)

# Embed an image
with open('beach_sunset.jpg', 'rb') as f:
    img_result = client.models.embed_content(
        model='gemini-embedding-2-preview',
        contents=[types.Part.from_bytes(data=f.read(), mime_type='image/jpeg')]
    )
img_vec = np.array(img_result.embeddings[0].values)

# Compute cosine similarity
similarity = np.dot(query_vec, img_vec) / (np.linalg.norm(query_vec) * np.linalg.norm(img_vec))
print(f"Similarity: {similarity:.4f}")

Verification Checklist

  • [ ] Single image embedded successfully (PNG or JPEG)
  • [ ] Output vector has expected dimensions (3072 default)
  • [ ] Multiple images per request tested (up to 6)
  • [ ] Cross-modal text-to-image similarity computed
  • [ ] Batch processing for 7+ images implemented with grouping

Troubleshooting

IssueSolution
Unsupported formatConvert to PNG or JPEG before embedding
Too many imagesMax 6 per request; batch into groups
Large image fileCompress or resize; no explicit size limit but affects token count
Low cross-modal similarityEnsure descriptive text matches image content
Base64 encoding errors (cURL)Use base64 -w 0 flag to avoid line wraps

See Also

Built with VitePress. Not affiliated with Google.