-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
252 lines (203 loc) · 9.86 KB
/
main.py
File metadata and controls
252 lines (203 loc) · 9.86 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File: convert_webp_image_gui.py
import os
import logging
import threading
import tkinter as tk
from tkinter import filedialog, messagebox, scrolledtext
from tkinter import ttk
from PIL import Image, UnidentifiedImageError
from datetime import datetime
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
def convert_webp_image(webp_path, output_dir, output_format, overwrite=False):
"""
Convert a .webp image to specified format and save it to the specified output directory.
Args:
webp_path (str): Path to the .webp file to be converted.
output_dir (str): Directory where the converted file will be saved.
output_format (str): The desired output format ('jpeg', 'png', 'gif', 'tiff', 'pdf').
overwrite (bool): Whether to overwrite existing files.
Returns:
bool: True if conversion was successful, False otherwise.
"""
try:
if not os.path.isfile(webp_path):
logging.error(f"File not found: {webp_path}")
return False
logging.info(f"Opening image: {webp_path}")
img = Image.open(webp_path)
if img.format.lower() != 'webp':
logging.error(f"File format is not .webp: {webp_path}")
return False
original_size = img.size
logging.info(f"Original image size: {original_size}")
img = img.convert("RGB")
output_filename = os.path.splitext(os.path.basename(webp_path))[0] + f'.{output_format}'
output_path = os.path.join(output_dir, output_filename)
if os.path.exists(output_path):
if overwrite:
logging.info(f"Overwriting existing file: {output_path}")
else:
logging.info(f"File already exists and overwrite is disabled: {output_path}")
return True
if output_format == 'pdf':
img.save(output_path, 'PDF', resolution=100.0)
else:
img.save(output_path, output_format.upper())
# Verify the created file to avoid false positives
if not os.path.isfile(output_path):
logging.error(f"Failed to create {output_path}")
return False
logging.info(f"Saved {output_format.upper()} image to: {output_path}")
converted_img = Image.open(output_path)
converted_size = converted_img.size
if original_size == converted_size:
logging.info(f"Size verified for {output_path}: {converted_size}")
else:
logging.warning(f"Size mismatch for {output_path}: original {original_size}, converted {converted_size}")
return True
except FileNotFoundError:
logging.error(f"File not found error: {webp_path}")
except UnidentifiedImageError:
logging.error(f"Cannot identify image file: {webp_path}")
except PermissionError:
logging.error(f"Permission denied for file {webp_path}")
except OSError as e:
logging.error(f"OS error for file {webp_path}: {e}")
except Exception as e:
logging.error(f"Unexpected error for {webp_path}: {e}")
return False
class WebpImageConverterApp:
def __init__(self, root):
self.root = root
self.root.title("WebP Image Converter")
self.source_files = []
self.output_dir = ''
self.overwrite = tk.BooleanVar()
self.output_format = tk.StringVar(value="jpeg")
self.cancel_flag = threading.Event()
self.create_widgets()
def create_widgets(self):
# Source files selection
self.source_label = tk.Label(self.root, text="Source Files:")
self.source_label.grid(row=0, column=0, padx=10, pady=5)
self.source_button = tk.Button(self.root, text="Select Files", command=self.select_source_files)
self.source_button.grid(row=0, column=1, padx=10, pady=5)
# Output directory selection
self.output_label = tk.Label(self.root, text="Output Directory:")
self.output_label.grid(row=1, column=0, padx=10, pady=5)
self.output_button = tk.Button(self.root, text="Select Directory", command=self.select_output_directory)
self.output_button.grid(row=1, column=1, padx=10, pady=5)
# Output format selection
self.format_label = tk.Label(self.root, text="Output Format:")
self.format_label.grid(row=2, column=0, padx=10, pady=5)
self.format_combobox = ttk.Combobox(self.root, textvariable=self.output_format, state='readonly')
self.format_combobox['values'] = ('jpeg', 'png', 'gif', 'tiff', 'pdf')
self.format_combobox.grid(row=2, column=1, padx=10, pady=5)
# Overwrite option
self.overwrite_check = tk.Checkbutton(self.root, text="Overwrite Existing Files", variable=self.overwrite)
self.overwrite_check.grid(row=3, column=0, columnspan=2, padx=10, pady=5)
# Convert button
self.convert_button = tk.Button(self.root, text="Convert", command=self.start_conversion)
self.convert_button.grid(row=4, column=0, padx=10, pady=5)
# Cancel button
self.cancel_button = tk.Button(self.root, text="Cancel", command=self.cancel_conversion)
self.cancel_button.grid(row=4, column=1, padx=10, pady=5)
# Clear log button
self.clear_log_button = tk.Button(self.root, text="Clear Log", command=self.clear_log)
self.clear_log_button.grid(row=5, column=0, columnspan=2, padx=10, pady=5)
# Progress bar
self.progress = ttk.Progressbar(self.root, orient="horizontal", length=300, mode="determinate")
self.progress.grid(row=6, column=0, columnspan=2, padx=10, pady=5)
# Log area
self.log_area = scrolledtext.ScrolledText(self.root, width=50, height=10, state='disabled')
self.log_area.grid(row=7, column=0, columnspan=2, padx=10, pady=5)
def select_source_files(self):
try:
files = filedialog.askopenfilenames(filetypes=[("WebP files", "*.webp")])
if files:
valid_files = [f for f in files if f.lower().endswith('.webp')]
invalid_files = set(files) - set(valid_files)
if invalid_files:
messagebox.showwarning("Invalid Files",
f"These files are not .webp and were skipped: {', '.join(invalid_files)}")
self.source_files = valid_files
self.log(f"Selected source files: {', '.join(self.source_files)}")
except Exception as e:
self.log(f"Error selecting files: {e}")
def select_output_directory(self):
try:
directory = filedialog.askdirectory()
if directory:
self.output_dir = directory
self.log(f"Selected output directory: {self.output_dir}")
except Exception as e:
self.log(f"Error selecting output directory: {e}")
def start_conversion(self):
self.cancel_flag.clear()
self.convert_thread = threading.Thread(target=self.convert_files)
self.convert_thread.start()
def convert_files(self):
if not self.source_files:
messagebox.showerror("Error", "No source files selected.")
self.log("Conversion failed: No source files selected.")
return
if not self.output_dir:
messagebox.showerror("Error", "No output directory selected.")
self.log("Conversion failed: No output directory selected.")
return
success_count = 0
error_count = 0
total_files = len(self.source_files)
self.progress["maximum"] = total_files
for index, webp_file in enumerate(self.source_files):
if self.cancel_flag.is_set():
self.log("Conversion cancelled by user.")
messagebox.showinfo("Cancelled", "Conversion process was cancelled.")
break
try:
if convert_webp_image(webp_file, self.output_dir, self.output_format.get(), self.overwrite.get()):
success_count += 1
self.log(f"Successfully converted: {webp_file}")
else:
error_count += 1
self.log(f"Failed to convert: {webp_file}")
except Exception as e:
error_count += 1
self.log(f"Error converting {webp_file}: {e}")
self.progress["value"] = index + 1
self.root.update_idletasks()
self.log(f"Conversion completed: {success_count} successful, {error_count} errors")
if not self.cancel_flag.is_set():
messagebox.showinfo("Conversion Completed",
f"Conversion completed: {success_count} successful, {error_count} errors")
self.auto_save_log()
def cancel_conversion(self):
self.cancel_flag.set()
self.log("Cancellation requested...")
def log(self, message):
self.log_area.config(state='normal')
self.log_area.insert(tk.END, message + "\n")
self.log_area.yview(tk.END)
self.log_area.config(state='disabled')
def auto_save_log(self):
try:
log_content = self.log_area.get("1.0", tk.END)
if log_content.strip():
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
log_filename = f"conversion_log_{timestamp}.txt"
log_path = os.path.join(self.output_dir, log_filename)
with open(log_path, 'w') as log_file:
log_file.write(log_content)
self.log(f"Log automatically saved to {log_path}")
except Exception as e:
self.log(f"Error saving log: {e}")
def clear_log(self):
self.log_area.config(state='normal')
self.log_area.delete("1.0", tk.END)
self.log_area.config(state='disabled')
self.log("Log cleared.")
if __name__ == "__main__":
root = tk.Tk()
app = WebpImageConverterApp(root)
root.mainloop()