Ollama Image Story Generator
2025-05-223 turns8,252 charsgpt-4-1-mini, gpt-4o⑂ 1 fork(s)
Summary
User wants to create a Python script using Ollama's granite3.2-vision model to analyze images and generate a cohesive story from them.
Messages
⑂ thread forked here
I want to create a python script which will use ollama using granite3.2-vision:latest to analyze a folder of pictures and to write a story based on an analysis of the analysis of each picture that creates a singular story and then I want the script to also rename the pictures in the order that the story progresses so that the pictures are in the order that each analysis for each is used in the final analysis is used---- write a series of prompts I can iteratively give to cline in order to create this script
Great idea — using `Ollama` with `granite3.2-vision:latest` and `Cline` to iteratively build a story based on image analysis. Below is a structured series of prompts that you can feed into **Cline**, assuming you're building the script piece by piece and want to keep things modular and testable.
---
## 🧠 OVERVIEW OF TASK
**Goal:** Analyze each image in a folder using a vision LLM, generate individual image descriptions, use those to generate a unified story, and rename images according to their sequence in the story.
---
## 📋 PRE-REQUISITES
Before you start, ensure:
- Ollama is running locally with the `granite3.2-vision:latest` model pulled.
- Python ≥ 3.8.
- `requests`, `Pillow`, and any JSON-handling library are installed.
---
## 🧪 ITERATIVE PROMPTS FOR CLINE
### **Prompt 1: Initialize Python script and collect image paths**
```plaintext
Create a Python script that loads all image files (jpg, jpeg, png) from a folder called `input_images`. Store the sorted list of file paths in a list called `image_paths`.
```
---
### **Prompt 2: Send each image to Ollama granite3.2-vision:latest and get a visual description**
```plaintext
Using `requests`, send each image in `image_paths` to the local Ollama server using the `granite3.2-vision:latest` model. For now, prompt the model with: "Describe what you see in this image in detail." Store each image description in a dictionary mapping filename to description.
```
---
### **Prompt 3: Create a unified story from all image descriptions**
```plaintext
Take all the image descriptions and feed them as context into the model with a prompt like:
"Based on these descriptions, write a single, coherent short story that includes each scene and connects them narratively."
Return and store the story.
```
---
### **Prompt 4: Extract the image usage order from the story**
```plaintext
Parse the generated story and determine the order in which the original image descriptions are referenced.
Return an ordered list of filenames (or associated keys from the previous dictionary) representing the narrative order.
```
---
### **Prompt 5: Rename the images in narrative order**
```plaintext
Using the ordered list of image filenames, rename the images by prefixing each with a number representing their position in the story (e.g., `01_forest.png`, `02_castle.png`, etc.). Save them to a new folder `ordered_images/`.
```
---
### **Prompt 6: Save the final story to a markdown file**
```plaintext
Write the final story to a markdown file named `story.md`. Use image links (markdown format) pointing to the renamed images in `ordered_images/` between each paragraph corresponding to an image.
```
---
### **Prompt 7 (Optional): Add CLI interface**
```plaintext
Wrap the script in a CLI using argparse so that you can call it like:
`python story_from_images.py --input input_images/ --output ordered_images/ --story story.md`
```
---
## 🧠 Bonus Prompt: One-shot Image + Text Prompt Structure
To structure multi-image input to the model better, consider combining per-image prompts into a single structured prompt:
```plaintext
Provide a single message to Ollama that includes each image and a note:
"This is Image 1: [image]. Description: ...
This is Image 2: [image]. Description: ...
Now write a connected story from these scenes."
```
---
Let me know if you want a `Dockerfile`, OpenWebUI version, or `FastAPI` integration for future automation.
gpt-4o
Got it! You want to build a Python script that:
1. Uses **Ollama** with the **granite3.2-vision:latest** model to analyze each picture in a folder.
2. Creates a **single coherent story** from the combined analyses of all pictures.
3. Renames the pictures so their filenames reflect the order in which their analyses appear in the story.
I’ll give you a **series of progressive prompts** you can feed iteratively into Cline (your CLI for the Ollama + granite3.2-vision model) to build this script step-by-step.
---
## Prompt Series to Build the Script Iteratively
---
### 1. Initial Setup & Reading Images from Folder
```plaintext
Write a Python script that lists all image files (jpg, png) in a given folder path.
The script should load their filenames into a list sorted alphabetically.
Just output the code to do this.
```
---
### 2. Load and Analyze One Image Using granite3.2-vision:latest via Ollama Python API
```plaintext
Given a filename of an image, write Python code that uses the Ollama Python client
to send this image to the granite3.2-vision:latest model and receive a textual analysis.
Assume the Ollama client is imported and initialized.
Show how to pass the image and get a text description response.
Output just the code.
```
---
### 3. Analyze All Images and Collect Text Descriptions in a List
```plaintext
Combine the previous two scripts:
- For each image file in a folder, send the image to granite3.2-vision:latest via Ollama.
- Collect each textual analysis into a list.
- Print all the analyses.
Output the full Python script.
```
---
### 4. Generate a Single Story from the List of Image Analyses
```plaintext
Write Python code that takes a list of strings (image analyses) and uses Ollama with granite3.2-vision:latest
to generate a single coherent story that connects all the analyses.
Assume the list of analyses is passed as a single concatenated string prompt.
Output only the Python code that generates this story.
```
---
### 5. Determine the Order of Images Based on Their Position in the Story
```plaintext
Write Python code that, given:
- a story string generated from all image analyses,
- and a list of individual image analyses,
determines the order in which each analysis appears in the story text,
and returns a list of indices corresponding to the new order of images.
Output only the code that performs this reordering.
```
---
### 6. Rename Image Files According to the New Order
```plaintext
Given:
- a list of original image filenames,
- and a list of indices representing their new order,
write Python code to rename the image files to a numbered format (e.g., "01.jpg", "02.jpg", ...)
according to their position in the story order.
Make sure renaming won't overwrite existing files or cause conflicts.
Output only the Python code for renaming.
```
---
### 7. Combine Everything Into a Single Script
```plaintext
Combine all previous parts into one full Python script that:
- Reads all images in a folder,
- Analyzes each image with granite3.2-vision:latest via Ollama,
- Generates a single story connecting all image analyses,
- Orders the images according to the story,
- Renames the images to reflect the story order,
- Prints the generated story at the end.
Include all necessary imports and Ollama client setup.
Output the full Python script.
```
---
### 8. Add Error Handling and Logging
```plaintext
Improve the full Python script to add:
- Basic error handling for missing files or failed API calls,
- Logging info messages about progress,
- Confirmation before renaming files.
Output the improved Python script.
```
---
### 9. Make Folder Path a Command Line Argument
```plaintext
Modify the full Python script to accept the folder path as a command line argument.
Use argparse to parse the input folder path.
Output the updated script.
```
---
### 10. Optional: Save the Story to a Text File
```plaintext
Add to the script the functionality to save the generated story into a text file in the same folder.
Name the file "story.txt".
Output the updated code snippet for this feature.
```
---
---
If you want, I can help you start with the first prompt or provide example code for any step! Just tell me which step you want to begin with or if you want me to generate all code now.
gpt-4-1-mini