Freedom Sale
Independence Day Special — Unlock the AI Path 70% off our most popular AI course · Limited time offer
--Days
--Hrs
--Min
--Sec
Claim Your Discount
✦ Beginner ⏱ 25 min

🖼️ Build an AI Image Caption Generator with BLIP

🎯 What You'll Build

A tool that takes any image and generates a descriptive caption automatically using Salesforce's BLIP model — runs locally, completely free.

📋 What You'll Need

1

Load the BLIP model

Download the model once from Hugging Face — it caches locally for future use.

from transformers import BlipProcessor, BlipForConditionalGeneration
from PIL import Image

processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
model     = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
print("Model loaded.")
Model loaded.
2

Caption a local image

Load any image and generate an unconditional caption.

image = Image.open("photo.jpg").convert("RGB")

inputs  = processor(image, return_tensors="pt")
out     = model.generate(**inputs, max_new_tokens=50)
caption = processor.decode(out[0], skip_special_tokens=True)

print("Caption:", caption)
Caption: a dog sitting on a grassy field next to a red frisbee
3

Caption from a URL

BLIP can also process images fetched directly from the web.

import requests
from PIL import Image
from io import BytesIO

url = "https://upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Cat03.jpg/1200px-Cat03.jpg"
image = Image.open(BytesIO(requests.get(url).content)).convert("RGB")

inputs  = processor(image, return_tensors="pt")
out     = model.generate(**inputs, max_new_tokens=50)
caption = processor.decode(out[0], skip_special_tokens=True)
print("Caption:", caption)
Caption: a orange cat sitting on a wooden floor looking at the camera
4

Batch caption a folder of images

Process an entire folder and save captions to a text file.

from pathlib import Path

folder = Path("images/")
results = []

for img_path in folder.glob("*.jpg"):
    image   = Image.open(img_path).convert("RGB")
    inputs  = processor(image, return_tensors="pt")
    out     = model.generate(**inputs, max_new_tokens=50)
    caption = processor.decode(out[0], skip_special_tokens=True)
    results.append(f"{img_path.name}: {caption}")
    print(f"{img_path.name}: {caption}")

Path("captions.txt").write_text("\n".join(results))
print(f"\nSaved {len(results)} captions.")
photo1.jpg: a woman holding a coffee cup standing in a kitchen
photo2.jpg: a city street at night with neon signs
photo3.jpg: a mountain lake surrounded by pine trees
Saved 3 captions.
💡 Tip: For more detailed or guided captions, use conditional generation: pass a text prompt as the second argument to the processor.

🎉 You Did It!

You now have a fully local image captioner. Use it to auto-generate alt text for accessibility, organise photo archives, or feed captions into a search index.

Found something wrong?

Spotted a bug, broken code, or something that doesn't look right? Tell us what's off and we'll fix it.