Generate captions for images

Automatically create descriptive captions for images using AI vision models.

Images · Notebook · Updated

Colab runs it in your browser. If it calls a model provider, it asks for that provider's API key in a cell.

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
terminal
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

img-generate-captions.ipynb
# 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')
)

One cell of the notebook, copied as it is. Open it in Colab for the rest.

Browse all 103