---
title: "Generate captions for images"
description: "Automatically create descriptive captions for images using AI vision models."
url: "https://www.pixeltable.com/notebooks/img-generate-captions"
updated: "2026-04-10"
---

# Generate captions for images

Automatically create descriptive captions for images using AI vision models.

- **Type:** Notebook
- **Group:** Images

## How it works

You add a computed column that sends each image to a vision model with a captioning prompt. New images are captioned automatically on insert.

## What it covers

- Generate captions using OpenAI's vision models
- Customize caption style (short, detailed, SEO-focused)
- Process images in batch automatically

## What it uses

- **Install:** `pip install pixeltable openai`
- **Environment variables it names:** `OPENAI_API_KEY`
- **Pixeltable calls:** `openai.chat_completions()`, `pxt.create_table()`, `insert()`, `add_computed_column()`

## A look at the code

```python
# Add caption using OpenAI vision

messages = [
    {
        'role': 'user',
        'content': [
            {
                'type': 'text',
                'text': 'Write a concise, descriptive caption for this image in one sentence.',
            },
            {'type': 'image_url', 'image_url': images.image},
        ],
    }
]

images.add_computed_column(
    caption=chat_completions(messages, model='gpt-4o-mini')
)
```

## Open it

- **Open in Colab:** https://colab.research.google.com/github/pixeltable/pixeltable/blob/release/docs/release/howto/cookbooks/images/img-generate-captions.ipynb
- **Docs page:** https://docs.pixeltable.com/howto/cookbooks/images/img-generate-captions
- **Source on GitHub:** https://github.com/pixeltable/pixeltable/blob/release/docs/release/howto/cookbooks/images/img-generate-captions.ipynb
