-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenamer.py
109 lines (85 loc) · 3.22 KB
/
renamer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import os
from openai import OpenAI
from PIL import Image
from io import BytesIO
import base64
def find_latest_screenshot(directory):
"""Find the most recently added screenshot in the directory."""
all_files = [
os.path.join(directory, f) for f in os.listdir(directory) if f.endswith(".png")
]
latest_file = max(all_files, key=os.path.getctime)
print(f"Found latest file: {latest_file}")
return latest_file
def analyze_image(image_path):
"""Analyze the image using GPT-4 API and return the description."""
client = OpenAI()
with Image.open(image_path) as img:
IMG_RES = 512
W, H = img.size
img = img.resize((IMG_RES, int(IMG_RES * H / W)))
buffer = BytesIO()
img.save(buffer, format="PNG")
img_base64 = base64.b64encode(buffer.getvalue()).decode("utf-8")
response = client.chat.completions.create(
model="gpt-4-vision-preview",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "This is a screenshot of something on my screen",
},
{
"type": "image_url",
"image_url": f"data:image/png;base64,{img_base64}",
},
{
"type": "text",
"text": "give a short, concise and descriptive file name for this image using at most 3 words. Use only small letters and use _ between words. Do not add any extension.",
},
],
}
],
max_tokens=300,
)
return response.choices[0].message.content.strip()
def rename_image(image_path, description):
"""Rename the image file based on the description."""
directory, filename = os.path.split(image_path)
new_filename = "_".join(description.split()) + ".png"
new_path = os.path.join(directory, new_filename)
os.rename(image_path, new_path)
return new_path
def has_been_processed(filename, processed_log):
"""Check if a file has already been processed."""
if os.path.exists(processed_log):
with open(processed_log, "r") as log:
if filename in log.read():
return True
return False
def mark_as_processed(filename, processed_log):
"""Mark a file as processed by logging its name."""
with open(processed_log, "a") as log:
log.write(filename + "\n")
def main():
# Modify these paths to your own
DIRS = [
"/Users/syedamaan/Desktop/screenshots",
"/Users/syedamaan/Desktop/projects/smart_renamer/processed.log",
]
screenshots_dir = DIRS[0]
processed_log = DIRS[1]
latest_screenshot = find_latest_screenshot(screenshots_dir)
if not has_been_processed(latest_screenshot, processed_log):
description = analyze_image(latest_screenshot)
new_path = rename_image(latest_screenshot, description)
mark_as_processed(new_path, processed_log)
print(
f"Renamed '{latest_screenshot}' to '{new_path}' and added to processed log"
)
if __name__ == "__main__":
import sys
print(sys.executable)
main()