Last active
March 7, 2025 06:38
-
-
Save kobaltz/7646a15ee289fc12f36364b2004bab05 to your computer and use it in GitHub Desktop.
Rename Screenshots
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. Install ollama from www.ollama.com | |
2. Install python | |
3. `pip install ollama` | |
4. `ollama pull llama3.2-vision:latest` | |
5. Put the `rename_images.py` script in the folder of your choice | |
6. Edit the script and update the `IMAGE_FILE_NAME_PATTERN` if yours is different | |
7. Run `python rename_images.py` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import re | |
import ollama | |
from pathlib import Path | |
# Constants. Adjust as needed. | |
IMAGE_FILE_NAME_PATTERN = "cleanshot*.png" | |
MODEL = "llama3.2-vision:latest" | |
FOLDER_PATH = "." | |
folder_path = os.path.expanduser(FOLDER_PATH) | |
def clean_filename(text): | |
text = text.lower().replace(" ", "_") | |
text = re.sub(r"[^a-zA-Z0-9_]", "", text) | |
return text[:128] | |
def get_unique_filename(folder, base_name, ext): | |
new_name = f"{base_name}{ext}" | |
counter = 1 | |
while (folder / new_name).exists(): | |
new_name = f"{base_name}_{counter}{ext}" | |
counter += 1 | |
return new_name | |
for image_path in Path(folder_path).glob(IMAGE_FILE_NAME_PATTERN): | |
print(f"Processing: {image_path.name}") | |
response = ollama.chat( | |
model=MODEL, | |
messages=[ | |
{"role": "system", "content": "You are an AI that provides short, accurate filenames for images."}, | |
{"role": "user", "content": "Describe this image in a few words, suitable for a filename. Most likely, it's a computer screenshot, so read the text and give an accurate description. Exclude filler works like 'the image shows a' or 'screen shot of'", "images": [str(image_path)]} | |
] | |
) | |
if response and "message" in response and "content" in response["message"]: | |
description = response["message"]["content"].strip() | |
base_filename = clean_filename(description) | |
new_filename = get_unique_filename(image_path.parent, base_filename, ".png") | |
new_path = image_path.parent / new_filename | |
os.rename(image_path, new_path) | |
print(f"Renamed to: {new_filename}") | |
else: | |
print(f"Failed to process: {image_path.name}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment