A tool that takes any image and generates a descriptive caption automatically using Salesforce's BLIP model — runs locally, completely free.
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.
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
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
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.
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.
Spotted a bug, broken code, or something that doesn't look right? Tell us what's off and we'll fix it.