MoonDream Image Captions for HTML Page
- Eli the Computer Guy
- Mar 28
- 1 min read

In this lab we use the caption() function of MoonDream to auto create a caption for an image, and then create an HTML file with the image embedded and the caption written below.
python3 -m pip install moondream
python3 -m pip install pillow
download 2B int 8 Model from: https://github.com/vikhyat/moondream
import moondream as md
from PIL import Image
model = md.vl(model='./moondream-2b-int8.mf.gz')
picture = './img2.jpeg'
image = Image.open(picture)
encoded_image = model.encode_image(image)
# Caption any image (length options: "short" or "normal" (default))
caption = model.caption(encoded_image, "short")
print("Caption:", caption['caption'])
with open('moondream-caption.html', 'w') as file:
file.write(f'''
<div style="position: relative;
height: 400px;
width: auto;
display: inline-block;">
<img src="{picture}"
style="height: 100%;
display: block;">
<div>{caption['caption']}</div>
</div>''')
Commentaires