-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyASCII_GUI.py
More file actions
executable file
·194 lines (154 loc) · 7.31 KB
/
PyASCII_GUI.py
File metadata and controls
executable file
·194 lines (154 loc) · 7.31 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
from PyASCII import *
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
import os
from time import time
#### GUI ####
file_path = ""
def select_file():
global file_path
file_path = filedialog.askopenfilename(title="Select a File.")
if file_path:
try:
path_label.config(text=file_path)
except Exception as e:
messagebox.showerror("Error", f"An error occurred while processing the file: {e}")
else:
messagebox.showwarning("Warning", "No file was selected.")
def validate_numeric_input(P):
if P.isdigit() or P == "":
return True
else:
return False
def on_go():
start_time = time()
resolution = int(entry_resolution.get())
filt = selected_option.get()
if filt == 'Default':
filt = None
contrast = float(contrast_spinbox.get())
sharpness = float(sharpness_spinbox.get())
threads = int(threads_slider.get())
output_path = None if entry_output.get() == "" else entry_output.get()
def get_resource_path(relative_path):
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.getcwd(), relative_path)
try:
sprite_sheet_path = get_resource_path("sprite_sheet.png")
sprite_sheet_image = Image.open(sprite_sheet_path)
global sprite_height, sprite_width
sprite_height = 8
sprite_width = 8
sprites = load_sprites(sprite_sheet_image=sprite_sheet_image,
sprite_width=sprite_width,
sprite_height=sprite_height,
monochrome_filter=filt)
except FileNotFoundError:
messagebox.showerror("FileNotFoundError", "Sprite Sheet 'sprite_sheet.png' not found!")
return
try:
if is_gif(file_path=file_path):
gif_buffer = gif_processing(gif_path=file_path,
sprites=sprites,
contrast=contrast,
sharpness=sharpness,
resolution=resolution)
output_file = output_path if output_path is not None else "PyASCII_Gif.gif"
with open(output_file, "wb") as f:
f.write(gif_buffer.getvalue())
elif is_image(file_path=file_path):
ascii_image = image_processing(image_path=file_path,
sprites=sprites,
contrast=contrast,
sharpness=sharpness,
resolution=resolution)
output_file = output_path if output_path is not None else "PyASCII_Image.png"
ascii_image.save(output_file)
elif is_video(file_path=file_path):
ascii_video = video_processing(video_path=file_path,
threads=threads,
sprites=sprites,
contrast=contrast,
sharpness=sharpness,
resolution=resolution)
w, h = ascii_video.size
w = w if w % 2 == 0 else w + 1
h = h if h % 2 == 0 else h + 1
ascii_video = ascii_video.resize((w, h))
output_file = output_path if output_path is not None else "PyASCII_Video.mp4"
ascii_video.write_videofile(output_file,
codec="libx264",
audio_codec="aac",
threads=threads,
preset="slow",
ffmpeg_params=["-pix_fmt", "yuv420p"])
end_time = time()
execution_time = end_time - start_time
messagebox.showinfo("Success!", f"Processing time: {execution_time:.4f} seconds")
except ValueError:
messagebox.showerror("ValueError", "Input file does not have a valid format!")
sys.exit(1)
except FileNotFoundError:
messagebox.showerror("FileNotFoundError", f"Input file {file_path} not found!")
sys.exit(1)
if __name__ == "__main__":
if getattr(sys, 'frozen', False):
import multiprocessing
multiprocessing.freeze_support()
filters = load_filters()
filters['Default'] = None
root = tk.Tk()
root.title("PyASCII")
root.geometry("300x550")
# Frame para alinhar os widgets à esquerda
left_frame = tk.Frame(root)
left_frame.grid(row=0, column=0, padx=10, pady=10, sticky='nsew')
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
# Botão de seleção de arquivo
btn_media = tk.Button(left_frame, text="Select an Image/Video", command=select_file)
btn_media.grid(row=0, column=0, padx=10, pady=10, sticky='w')
# Caminho do arquivo selecionado
path_label = tk.Label(left_frame, text="", wraplength=250)
path_label.grid(row=1, column=0, padx=10, pady=5, sticky='w')
# Resolução
resolution_label = tk.Label(left_frame, text="Resolution", font=("Helvetica", 10, "bold"))
resolution_label.grid(row=2, column=0, padx=10, pady=(5, 0), sticky='w')
entry_resolution = tk.Entry(left_frame, validate='key', validatecommand=(root.register(validate_numeric_input), '%P'))
entry_resolution.grid(row=3, column=0, padx=10, pady=5, sticky='w')
# Filtros
filters_label = tk.Label(left_frame, text="Filters", font=("Helvetica", 10, "bold"))
filters_label.grid(row=4, column=0, padx=10, pady=(5, 0), sticky='w')
options = list(filters.keys())
selected_option = tk.StringVar(root)
selected_option.set(options[10])
option_menu = tk.OptionMenu(left_frame, selected_option, *options)
option_menu.grid(row=5, column=0, padx=10, pady=5, sticky='w')
# Contraste com Spinbox
contrast_label = tk.Label(left_frame, text="Contrast (1.0 - 2.0)", font=("Helvetica", 10, "bold"))
contrast_label.grid(row=6, column=0, padx=10, pady=(5, 0), sticky='w')
contrast_spinbox = tk.Spinbox(left_frame, from_=1.0, to=2.0, increment=0.1)
contrast_spinbox.grid(row=7, column=0, padx=10, pady=5, sticky='w')
# Sharpness com Spinbox
sharpness_label = tk.Label(left_frame, text="Sharpness (1.0 - 2.0)", font=("Helvetica", 10, "bold"))
sharpness_label.grid(row=8, column=0, padx=10, pady=(5, 0), sticky='w')
sharpness_spinbox = tk.Spinbox(left_frame, from_=1.0, to=2.0, increment=0.1)
sharpness_spinbox.grid(row=9, column=0, padx=10, pady=5, sticky='w')
# Threads com Slider
threads_label = tk.Label(left_frame, text="Threads", font=("Helvetica", 10, "bold"))
threads_label.grid(row=10, column=0, padx=10, pady=(5, 0), sticky='w')
threads_slider = tk.Scale(left_frame, from_=1, to=os.cpu_count(), resolution=1, orient="horizontal")
threads_slider.grid(row=11, column=0, padx=10, pady=5, sticky='w')
# Saída
output_label = tk.Label(left_frame, text="Output Path", font=("Helvetica", 10, "bold"))
output_label.grid(row=12, column=0, padx=10, pady=(5, 0), sticky='w')
entry_output = tk.Entry(left_frame)
entry_output.grid(row=13, column=0, padx=10, pady=5, sticky='w')
# Botão de execução
btn_go = tk.Button(left_frame, text="Go!", command=on_go)
btn_go.grid(row=14, column=0, padx=10, pady=20)
left_frame.grid_rowconfigure(14, weight=1)
left_frame.grid_columnconfigure(0, weight=1)
root.mainloop()