An A.I. tool to scan for Mac screenshots and rename them based on their content as visually recognised by OpenAI's GPT-4o-mini
Project description
mac-batch-screenshot-renamer-ai
Introduction
mac-batch-screenshot-renamer-ai is a Python class designed to automate the process of renaming screenshot files using AI-generated titles. It leverages OpenAI's GPT models to analyze the content of screenshots and generate informative filenames. Additionally, it offers functionality to group related screenshots based on their timestamps.
Quick Start
Use the ScreenshotRenamer class in your Python script:
from screenshot_renamer import ScreenshotRenamer
# Initialize the renamer
renamer = ScreenshotRenamer(directory="/path/to/screenshots")
# Rename screenshots
renamer.rename_screenshots()
# Group existing files
renamer.group_existing_files()
# Write grouped files to JSON
renamer.write_grouped_files_to_json()
Initialization Parameters
The ScreenshotRenamer
class accepts several parameters during initialization:
max_filename_length
(int, default: 60): Maximum length of the new filename.model
(str, default: "gpt-4o-mini"): OpenAI model to use for generating titles.openai_key
(Optional[str], default: None): OpenAI API key. If None, it will be read from the environment variable.prompt
(Optional[str], default: None): Custom prompt for the AI. If None, a default prompt will be used.group_files
(bool, default: False): Whether to group files based on timestamp.group_time_threshold
(int, default: 15): Time threshold (in minutes) for grouping files.directory
(Optional[str], default: None): Directory to use for input and output operations. If None, it defaults to "~/Documents".
Main Methods
1. rename_screenshots()
This method renames screenshots in the specified directory using AI-generated titles.
Usage:
renamer.rename_screenshots()
Process:
- Scans the specified directory for screenshot files matching the predefined pattern.
- For each screenshot:
- Extracts the creation timestamp from the filename.
- Sends the image to the OpenAI API for content analysis.
- Generates a new filename based on the AI's response, adhering to the
max_filename_length
. - Sanitizes the new filename to ensure it's valid for the file system.
- Renames the file with the new name while preserving the original timestamp.
- If
group_files
is enabled, adds the file to a group based on its timestamp.
- Handles potential API errors and file system issues gracefully.
- Provides console output for each renamed file.
2. group_existing_files()
This method groups existing renamed files in the specified directory based on their timestamps.
Usage:
renamer.group_existing_files()
Process:
- Scans the specified directory for PNG files that match the renamed format.
- For each file:
- Extracts the timestamp from the filename.
- Converts the timestamp to a datetime object.
- Groups files based on the extracted timestamps and the
group_time_threshold
:- Files within the threshold are placed in the same group.
- Each group is identified by the earliest timestamp in the group.
- Merges close groups to prevent excessive fragmentation:
- If the time difference between the last file of one group and the first file of the next group is within the threshold, the groups are merged.
- Stores the grouped files information in the
grouped_files
attribute.
3. write_grouped_files_to_json()
This method writes the grouped files information to a JSON file in the specified directory.
Usage:
renamer.write_grouped_files_to_json()
Process:
- Checks if
grouped_files
contains data. If not, it callsgroup_existing_files()
first. - Generates a unique filename for the JSON output:
- Format:
grouped_screenshots_YYYYMMDD_HHMMSS.json
- Uses the current date and time to ensure uniqueness.
- Format:
- Creates a dictionary with group timestamps as keys and lists of filenames as values.
- Writes the dictionary to the JSON file in the specified directory:
- Uses
json.dump()
with indentation for readability.
- Uses
- Provides console output confirming the JSON file creation and its location.
Note: These methods work together to provide a comprehensive solution for renaming, grouping, and documenting screenshots. The rename_screenshots()
method can be used independently, while group_existing_files()
and write_grouped_files_to_json()
are often used in sequence to process already renamed files.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file mac-batch-screenshot-renamer-ai-1.0.0.tar.gz
.
File metadata
- Download URL: mac-batch-screenshot-renamer-ai-1.0.0.tar.gz
- Upload date:
- Size: 3.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4b000c8834cffa3b8bae9bb56fcac4b48934523c6d4fcbbb8fb80eac43c1c77e |
|
MD5 | 8bde6de9b7c74d903baa4018653847af |
|
BLAKE2b-256 | cc2fd91dfb49b077a48be3e2b7fee0f5fdf70aa790ae2fe8f2e614dc8b9152e4 |
File details
Details for the file mac_batch_screenshot_renamer_ai-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: mac_batch_screenshot_renamer_ai-1.0.0-py3-none-any.whl
- Upload date:
- Size: 3.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4a3abee47e84cbb6f9e1336381fe87615867e41700ee8a465843325562a961e8 |
|
MD5 | 421aa13147542dfdefa32760fbbe8d7d |
|
BLAKE2b-256 | a62c96c6a5f79d231906c1c6efc74ab44485d654ed058c5443c2507c0ac1a8b2 |