else/ascii/ascii_gui.pyw

166 lines
6.3 KiB
Python
Raw Normal View History

2014-12-02 00:37:23 +00:00
import os
import tkinter
import ascii
import threading
2014-12-02 01:43:34 +00:00
help_file = """The file or URL to ASCII.
2014-12-02 00:37:23 +00:00
If you only put a name, it must be in the same
folder as this program. Otherwise, include a
file path.
"""
help_resolution = """The characterpixel resolution.
Each ASCII character will represent xy pixels
of the original image.
Lower numbers produce higher detail.
"""
help_contrast = """The character smoothing.
At 1, the image may use all 95 characters.
At 2, the image may use every other character
At 3, every third character, etc.
Higher numbers produce flatter images."""
2014-12-04 08:34:01 +00:00
help_table = """The table of ASCII characters.
Full = all 95 characters
Min = 41 characters
Min may produce cleaner images from photos
Lower Smoothing to accommodate"""
2014-12-02 00:37:23 +00:00
help_blank = "\n\n\n\n"
class Ascii:
def ticker(self, status):
self.label_ticker.config(text=status)
def startthread(self):
2014-12-04 08:34:01 +00:00
self.processthread = threading.Thread(name="Ascii converter", target=self.start)
self.processthread.daemon=True
self.processthread.start()
2014-12-02 00:37:23 +00:00
def start(self):
self.enter_file.configure(bg="#fff")
self.enter_file.configure(fg="#000")
self.enter_resolution_x.configure(bg="#fff")
self.enter_resolution_y.configure(bg="#fff")
self.enter_contrast.configure(bg="#fff")
filename = self.enter_file.get()
xvalue = int(self.enter_resolution_x.get())
yvalue = int(self.enter_resolution_y.get())
contrast = int(self.enter_contrast.get())
tickfunction = self.ticker
try:
2014-12-04 08:34:01 +00:00
ascii.ascii(filename, xvalue, yvalue, contrast, tickfunction, self.asciitable)
2014-12-02 00:37:23 +00:00
except FileNotFoundError:
self.enter_file.configure(bg="#f00")
self.enter_file.configure(fg="#fff")
if 'http' in filename and '/' in filename:
filename = filename.split('/')[-1]
self.enter_file.delete(0, 'end')
self.enter_file.insert(0, filename)
def generatehelp(self, helptype, label):
def showhelp(self):
label.configure(text=helptype)
return showhelp
2014-12-04 08:34:01 +00:00
def toggletable(self):
if self.asciitable == ascii.asciitable:
self.asciitable = ascii.asciitable_min
self.button_asciitable.configure(text="Min")
else:
self.asciitable = ascii.asciitable
self.button_asciitable.configure(text="Full")
2014-12-02 01:41:21 +00:00
def clear(self):
self.enter_file.configure(bg="#fff")
self.enter_file.configure(fg="#000")
self.enter_file.delete(0, 'end')
self.enter_resolution_x.delete(0, 'end')
self.enter_resolution_x.insert(0, '8')
self.enter_resolution_y.delete(0, 'end')
2014-12-04 08:34:01 +00:00
self.enter_resolution_y.insert(0, '17')
2014-12-02 01:41:21 +00:00
self.enter_contrast.delete(0, 'end')
self.enter_contrast.insert(0, '8')
2014-12-04 08:34:01 +00:00
self.asciitable = ascii.asciitable
self.button_asciitable.configure(text="Full")
self.label_ticker.config(text="0.00")
2014-12-02 01:41:21 +00:00
2014-12-02 00:37:23 +00:00
def __init__(self):
self.t = tkinter.Tk()
self.t.wm_title("ASCII")
self.w = 400
self.h = 275
2014-12-04 08:34:01 +00:00
self.asciitable = ascii.asciitable
2014-12-02 00:37:23 +00:00
self.centerframe = tkinter.Frame(self.t, width=self.w, height=self.h)
self.centerframe.pack_propagate(0)
self.centerframe.pack(expand=True)
self.label_help = tkinter.Label(self.centerframe, text=help_blank)
self.label_file = tkinter.Label(self.centerframe, text="File:")
self.label_file.help = self.generatehelp(help_file, self.label_help)
self.label_resolution = tkinter.Label(self.centerframe, text="Resolution:")
self.label_resolution.help = self.generatehelp(help_resolution, self.label_help)
self.label_contrast = tkinter.Label(self.centerframe, text="Smoothing:")
self.label_contrast.help = self.generatehelp(help_contrast, self.label_help)
self.label_ticker = tkinter.Label(self.centerframe, text="0.00")
2014-12-04 08:34:01 +00:00
self.label_table = tkinter.Label(self.centerframe, text="Table: ")
self.label_table.help = self.generatehelp(help_table, self.label_help)
2014-12-02 00:37:23 +00:00
self.centerframe.help = self.generatehelp(help_blank, self.label_help)
self.label_file.bind("<Motion>", self.label_file.help)
self.label_resolution.bind("<Motion>", self.label_resolution.help)
self.label_contrast.bind("<Motion>", self.label_contrast.help)
self.centerframe.bind("<Motion>", self.centerframe.help)
2014-12-04 08:34:01 +00:00
self.label_table.bind("<Motion>", self.label_table.help)
2014-12-02 00:37:23 +00:00
self.enter_file = tkinter.Entry(self.centerframe, width=30)
self.enter_resolution_x = tkinter.Spinbox(self.centerframe, from_=1, to=9999, width=5)
self.enter_resolution_y = tkinter.Spinbox(self.centerframe, from_=1, to=9999, width=5)
self.enter_resolution_x.delete(0, 'end')
self.enter_resolution_y.delete(0, 'end')
self.enter_resolution_x.insert(0, 8)
2014-12-04 08:34:01 +00:00
self.enter_resolution_y.insert(0, 17)
2014-12-02 00:37:23 +00:00
self.enter_contrast = tkinter.Spinbox(self.centerframe, from_=1, to=95, width=2)
self.enter_contrast.delete(0, 'end')
self.enter_contrast.insert(0, 8)
self.button_start = tkinter.Button(self.centerframe, text="Go", command= self.startthread)
self.button_start.configure(bg="#76E22E", activebackground="#46E22E", relief="flat", width=15)
2014-12-02 01:41:21 +00:00
self.button_clear = tkinter.Button(self.centerframe, text="clear", command= self.clear)
self.button_clear.configure(bg="#e23939", activebackground="#b82e2e", relief="flat", width=4)
2014-12-04 08:34:01 +00:00
self.button_asciitable = tkinter.Button(self.centerframe, text="Full", command=self.toggletable)
self.button_asciitable.configure(bg="#6fd5f6", activebackground="#6fd5f6", relief="flat", width=4)
2014-12-02 00:37:23 +00:00
self.label_file.grid(row=0, column=0, sticky="e")
self.enter_file.grid(row=0, column=1, sticky="w", columnspan=88)
self.enter_file.focus_set()
self.label_resolution.grid(row=1, column=0, sticky="e")
self.enter_resolution_x.grid(row=1, column=1, sticky="w")
self.enter_resolution_y.grid(row=1, column=2, sticky="w")
self.label_contrast.grid(row=2, column=0, sticky="e")
self.enter_contrast.grid(row=2, column=1, sticky="w")
2014-12-04 08:34:01 +00:00
self.label_table.grid(row=3, column=0, sticky="e")
self.button_asciitable.grid(row=3, column=1, sticky="w")
self.label_ticker.grid(row=4, column=0, columnspan=88)
self.button_start.grid(row=5, column=0, columnspan=88)
self.button_clear.grid(row=5, column=87)
self.label_help.grid(row=6, column=0, columnspan=88)
2014-12-02 00:37:23 +00:00
self.screenwidth = self.t.winfo_screenwidth()
self.screenheight = self.t.winfo_screenheight()
self.windowwidth = self.w
self.windowheight = self.h
self.windowx = (self.screenwidth-self.windowwidth) / 2
self.windowy = ((self.screenheight-self.windowheight) / 2) - 27
self.geometrystring = '%dx%d+%d+%d' % (self.windowwidth, self.windowheight, self.windowx, self.windowy)
self.t.geometry(self.geometrystring)
self.t.mainloop()
asciigui = Ascii()