-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverter.py
72 lines (57 loc) · 2.65 KB
/
converter.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
# customtkinter was imported as ctk for an easier approach
import customtkinter as ctk
from customtkinter import filedialog
from PIL import Image
ctk.set_appearance_mode("dark")
ctk.set_default_color_theme("dark-blue")
# Function to convert the image format
def convert_image():
if selected_image:
output_format = format_var.get()
input_image = Image.open(selected_image)
output_image = selected_image.replace(selected_image.split(".")[-1], output_format.lower())
input_image.save(output_image)
status_label.configure(text=f"Image converted to {output_format}")
else:
status_label.configure(text="Please select an image first")
# Function to update the selected image
def select_image():
global selected_image
selected_image = filedialog.askopenfilename(filetypes=[("Image Files", "*.jpg;*.jpeg;*.webp;*.png;*.bmp;*.avif")])
if selected_image:
convert_button.configure(state=ctk.NORMAL)
status_label.configure(text=f"Selected Image: {selected_image}")
# Remove the selected format from the dropdown if it's in the list
format_var.set("Output Format")
file_extension = selected_image.split(".")[-1]
if file_extension.lower() in format_options:
format_options.remove(file_extension.lower())
format_combobox.configure(values=format_options)
else:
convert_button.configure(state=ctk.DISABLED)
# Create the main tkinter window
root = ctk.CTk()
root.iconbitmap("sic.ico")
root.title("Simple Image Converter")
root.geometry("475x310")
root.resizable(False, False)
# Global variable to store the selected image path
selected_image = None
# Create and configure widgets and packs
frame = ctk.CTkFrame(master=root)
frame.pack(pady=20, padx=20, fill="both", expand=True)
select_button = ctk.CTkButton(master=frame, text="Select Image", command=select_image)
select_button.pack(pady=20, padx=10)
status_label = ctk.CTkLabel(master=frame, text="Please select an image.")
status_label.pack(pady=5, padx=10)
format_var = ctk.StringVar(master=frame)
format_var.set("Output Format")
format_options = ["jpg", "webp", "png", "bmp", "avif", "jpeg"]
format_combobox = ctk.CTkComboBox(master=frame, variable=format_var, values=format_options)
format_combobox.pack(pady=12, padx=10)
convert_button = ctk.CTkButton(master=frame, text="Convert", command=convert_image, state=ctk.DISABLED)
convert_button.pack(pady=12, padx=10)
made_by = ctk.CTkLabel(master=frame, text="Made by 0xRezoc on GitHub", font=("Arial", 10))
made_by.pack(pady=24, padx=10)
# Start the customtkinter main loop
root.mainloop()