-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
195 lines (165 loc) · 7.83 KB
/
functions.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
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
# Required imports
from tkinter import *
from PIL import Image
from tkinter import filedialog
from PIL import Image
import img2pdf
# Convertion options
menu_options = {
1: 'From [JPG -> PNG]',
2: 'From [PNG -> JPG]',
3: 'From [PNG/JPG -> ICO]',
4: 'From [PNG/JPG -> PDF]',
5: 'From [PNG/JPG -> WEBP]',
6: 'From [WEBP -> PNG]',
7: 'From [WEBP -> JPG]',
8: 'From [ICO -> JPG]',
9: 'From [ICO -> PNG]',
}
def print_menu():
for key in menu_options.keys():
print (key, '--', menu_options[key] )
# Select file menu display
def converter_menu(answer):
global root
def on_enter(event):
button.config(cursor="hand2")
root = Tk()
root.title("File converter")
root.iconbitmap("images/logo.ico")
root.geometry("300x150")
Label(root, text= "Select your File", font=('Arial Rounded MT Bold',20)).pack(pady=20)
button = Button(root, text="Open a file", borderwidth=0, command=lambda: open_dialog_convertion(answer))
img = PhotoImage(file="images/buttonOF.png")
button.config(image=img)
button.pack()
button.bind("<Enter>", on_enter)
root.resizable(False,False)
root.attributes('-topmost',True)
root.mainloop()
# Main function
def open_dialog_convertion(answer):
global im1
global filename
# Executing the user´s input
# JPG TO PNG
if answer == "1":
print("\n··· [JPG -> PNG] Selected ···")
filename = filedialog.askopenfilename(initialdir="/", title="Select A File", filetypes=(("JPG files", "*.jpg"),("All files", "*.*")))
if filename.endswith(".jpg"):
im1 = Image.open(filename)
export_filename = filedialog.asksaveasfilename(title="Save PNG As", defaultextension=".png", filetypes=(("PNG files", "*.png"),("All files", "*.*")))
im1.save(export_filename)
print("\n··· Your image has been converted! ··· ")
root.destroy()
else:
print("Error - Something went wrong :(")
# PNG to JPG
elif answer == "2":
print("··· [PNG -> JPG] Selected ···")
filename = filedialog.askopenfilename(initialdir="/", title="Select A File", filetypes=(("PNG files", "*.png"),("All files", "*.*")))
if filename.endswith(".png"):
im1 = Image.open(filename)
export_filename = filedialog.asksaveasfilename(title="Save JPG As", defaultextension=".jpg", filetypes=(("JPG files", "*.jpg"),("All files", "*.*")))
im1.save(export_filename)
print("\n··· Your image has been converted! ··· ")
root.destroy()
else:
print("Error - Something went wrong :(")
# PNG/JPG to ICO
elif answer == "3":
print("··· [PNG/JPG -> ICO] Selected ···")
filename = filedialog.askopenfilename(initialdir="/", title="Select A File", filetypes=(("PNG files", "*.png"),("JPG files", "*.jpg")))
if filename.endswith(".png") or filename.endswith(".jpg"):
im1 = Image.open(filename)
export_filename = filedialog.asksaveasfilename(title="Save ICO As", defaultextension=".ico", filetypes=(("ICO files", "*.ico"),("All files", "*.*")))
im1.save(export_filename)
print("\n··· Your image has been converted! ··· ")
root.destroy()
else:
print("Error - Something went wrong :(")
# PDF convertion using img2pdf
elif answer == "4":
print("··· [PNG/JPG -> PDF] Selected ···")
print("\n - You can select multiple images -")
filenames = filedialog.askopenfilenames(
initialdir="/",
title="Select Files",
filetypes=(("PNG files", "*.png"), ("JPG files", "*.jpg"), ("All files", "*.*"))
)
if filenames:
export_filename = filedialog.asksaveasfilename(
title="Save PDF As",
defaultextension=".pdf",
filetypes=(("PDF files", "*.pdf"), ("All files", "*.*"))
)
if export_filename:
pdf_bytes = img2pdf.convert(filenames)
with open(export_filename, "wb") as f:
f.write(pdf_bytes)
print("\n··· Your images have been converted to PDF! ··· ")
root.destroy()
else:
print("Error - No file selected for saving.")
else:
print("Error - No files selected.")
# PNG/JPG to WEBP
elif answer == "5":
print("··· [PNG/JPG -> WEBP] Selected ···")
filename = filedialog.askopenfilename(initialdir="/", title="Select A File", filetypes=(("PNG files", "*.png"),("JPG files", "*.jpg")))
if filename.endswith(".png") or filename.endswith(".jpg"):
im1 = Image.open(filename)
export_filename = filedialog.asksaveasfilename(title="Save WEBP As", defaultextension=".webp", filetypes=(("WEBP files", "*.webp"),("All files", "*.*")))
im1.save(export_filename)
print("\n··· Your image has been converted! ··· ")
root.destroy()
else:
print("Error - Something went wrong :(")
# WEBP to PNG
elif answer == "6":
print("··· [WEBP -> PNG] Selected ···")
filename = filedialog.askopenfilename(initialdir="/", title="Select A File", filetypes=(("WEBP files", "*.webp"),("All files", "*.*")))
if filename.endswith(".webp"):
im1 = Image.open(filename)
export_filename = filedialog.asksaveasfilename(title="Save PNG As", defaultextension=".png", filetypes=(("PNG files", "*.png"),("All files", "*.*")))
im1.save(export_filename)
print("\n··· Your image has been converted! ··· ")
root.destroy()
else:
print("Error - Something went wrong :(")
# WEBP to JPG
elif answer == "7":
print("··· [WEBP -> JPG] Selected ···")
filename = filedialog.askopenfilename(initialdir="/", title="Select A File", filetypes=(("WEBP files", "*.webp"),("All files", "*.*")))
if filename.endswith(".webp"):
im1 = Image.open(filename)
export_filename = filedialog.asksaveasfilename(title="Save JPG As", defaultextension=".jpg", filetypes=(("JPG files", "*.jpg"),("All files", "*.*")))
im1.save(export_filename)
print("\n··· Your image has been converted! ··· ")
root.destroy()
else:
print("Error - Something went wrong :(")
# ICO to JPG
elif answer == "8":
print("··· [ICO -> JPG] Selected ···")
filename = filedialog.askopenfilename(initialdir="/", title="Select A File", filetypes=(("ICO files", "*.ico"),("All files", "*.*")))
if filename.endswith(".ico"):
im1 = Image.open(filename)
export_filename = filedialog.asksaveasfilename(title="Save JPG As", defaultextension=".jpg", filetypes=(("JPG files", "*.jpg"),("All files", "*.*")))
im1.save(export_filename)
print("\n··· Your image has been converted! ··· ")
root.destroy()
else:
print("Error - Something went wrong :(")
# ICO to PNG
elif answer == "9":
print("··· [ICO -> PNG] Selected ···")
filename = filedialog.askopenfilename(initialdir="/", title="Select A File", filetypes=(("ICO files", "*.ico"),("All files", "*.*")))
if filename.endswith(".ico"):
im1 = Image.open(filename)
export_filename = filedialog.asksaveasfilename(title="Save PNG As", defaultextension=".png", filetypes=(("PNG files", "*.png"),("All files", "*.*")))
im1.save(export_filename)
print("\n··· Your image has been converted! ··· ")
root.destroy()
else:
print("Error - Something went wrong :(")