-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_convert.py
More file actions
181 lines (160 loc) · 6.1 KB
/
image_convert.py
File metadata and controls
181 lines (160 loc) · 6.1 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import sys
import json
import os
from PIL import Image
import glob
SUPPORTED_FORMATS = {
'PNG': 'PNG',
'JPG': 'JPEG',
'JPEG': 'JPEG',
'WEBP': 'WEBP',
'GIF': 'GIF'
}
def validate_format(format_str):
if not format_str:
return 'PNG'
format_upper = format_str.upper()
if format_upper not in SUPPORTED_FORMATS:
raise ValueError(f"Unsupported format: {format_str}. Supported formats: {', '.join(SUPPORTED_FORMATS.keys())}")
return SUPPORTED_FORMATS[format_upper]
def find_single_file(filename):
search_paths = [
os.getcwd(),
os.path.expanduser("~/Downloads"),
os.path.expanduser("~/Pictures"),
os.path.expanduser("~/Documents")
]
for path in search_paths:
try:
for root, _, files in os.walk(path):
if filename in files:
return [os.path.join(root, filename)]
except Exception:
continue
return []
def find_batch_files(folder_path, pattern):
try:
full_pattern = os.path.join(folder_path, pattern)
return glob.glob(full_pattern)
except Exception as e:
raise ValueError(f"Error searching folder {folder_path}: {str(e)}")
def cleanup_files(files):
for file in files:
try:
os.remove(file)
except Exception as e:
print(f"Failed to remove {file}: {str(e)}")
async def func(args):
try:
filename = args.get('filename')
if not filename:
return json.dumps({"error": "No filename provided"})
try:
output_format = validate_format(args.get('format'))
except ValueError as e:
return json.dumps({"error": str(e)})
# Handle batch conversion
if args.get('batch'):
folder_path = args.get('folder')
if not folder_path:
return json.dumps({"error": "Folder path required for batch conversion"})
if not os.path.exists(folder_path):
return json.dumps({"error": f"Folder not found: {folder_path}"})
locations = find_batch_files(folder_path, filename)
else:
locations = find_single_file(filename)
if not locations:
return json.dumps({"error": f"No files found matching {filename}"})
if len(locations) > 1 and not args.get('batch'):
locations_list = "\n".join(f"{i+1}. {loc}" for i, loc in enumerate(locations))
return json.dumps({
"prompt": {
"type": "select",
"message": f"Found multiple matches. Convert all? (y/n)\n{locations_list}",
"options": ["y", "n"]
}
})
converted = []
errors = []
original_files = []
for input_path in locations:
try:
with Image.open(input_path) as img:
original_format = img.format
if original_format not in SUPPORTED_FORMATS.values():
errors.append(f"Skipped {input_path}: Format {original_format} not supported")
continue
output_path = os.path.splitext(input_path)[0] + '.' + output_format.lower()
if output_format == 'JPEG':
img = img.convert('RGB')
img.save(output_path, output_format)
converted.append(os.path.basename(input_path))
original_files.append(input_path)
except Image.UnidentifiedImageError:
errors.append(f"Skipped {input_path}: Could not identify image format")
except Exception as e:
errors.append(f"Failed {input_path}: {str(e)}")
if converted:
return json.dumps({
"prompt": {
"type": "select",
"message": f"Converted {len(converted)} files. Delete original files? (y/n)",
"options": ["y", "n"],
"context": {
"files": original_files,
"converted": converted,
"errors": errors
}
}
})
result = {
"message": f"Converted {len(converted)} files to {output_format.lower()}"
}
if converted:
result["converted"] = converted
if errors:
result["errors"] = errors
return json.dumps(result)
except Exception as e:
return json.dumps({"error": f"Error: {str(e)}"})
async def handle_cleanup_response(response, context):
if response.lower() == 'y':
cleanup_files(context.get('files', []))
return json.dumps({
"message": f"Converted {len(context['converted'])} files and cleaned up originals",
"converted": context['converted'],
"errors": context.get('errors', [])
})
return json.dumps({
"message": f"Converted {len(context['converted'])} files",
"converted": context['converted'],
"errors": context.get('errors', [])
})
object = {
"name": "image_convert",
"description": "Convert images between formats (PNG, JPG, WEBP, GIF). Supports batch conversion with wildcards (*.jpg).",
"parameters": {
"type": "object",
"properties": {
"filename": {
"type": "string",
"description": "Filename or pattern (e.g., image.jpg or *.jpg)"
},
"format": {
"type": "string",
"enum": list(SUPPORTED_FORMATS.keys()),
"description": "Output format"
},
"batch": {
"type": "boolean",
"description": "Convert all matching files in specified folder",
"default": False
},
"folder": {
"type": "string",
"description": "Folder path for batch conversion"
}
},
"required": ["filename"]
}
}