|
| 1 | +import tkinter as tk |
| 2 | +import tkinter.ttk as ttk |
| 3 | +from tkinter.filedialog import asksaveasfilename |
| 4 | +from tkinter.filedialog import askopenfilename |
| 5 | +import os |
| 6 | +from tkinter import messagebox |
| 7 | +import shutil |
| 8 | +from PIL import ImageTk, Image |
| 9 | +import io |
| 10 | +import math |
| 11 | + |
| 12 | +#create main window |
| 13 | +def create_main_window(): |
| 14 | + global top |
| 15 | + global root |
| 16 | + img=b'iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAAAAABWESUoAAABJWlDQ1BJQ0MgcHJvZmlsZQAAKJGdkM1Kw0AUhb9WqVUUBEVBXGThtuDGrtz4g8FFobYRjK7SSYvFJIYkpfgGfRN9mC4EwUfwARRceya6cGE2DlzOx+Xec2YG6k5k4nxxH+KkyNzekX/lXztLbzRYZ5km24HJ007/zKPyfL5Ss/rSsl7Vc3+eRjjMjXSuSkyaFVA7FLenRWpZxead1zsRz8ROGCeh+Em8F8ahZbvbi6OJ+fG0t1kdJpd921ft4nJOhy4OAyaMiShoSRN1TmlzIHXJCHggx0gjhupNNVNwK8rl5HIs8kS6TUXeTpnXVcpAHmN52YR7YnnaPOz/fq99XJSbta15GmRB2VpQ1UcjeH+ENR82nmHlpiKr+fttFTPtcuafb/wCwG5QURqfn2MAAAAJcEhZcwAALiMAAC4jAXilP3YAAAAHdElNRQfmBhAJCy+UZC8dAAAAGXRFWHRDb21tZW50AENyZWF0ZWQgd2l0aCBHSU1QV4EOFwAAAZhJREFUOMvNk09IVFEUh78Xb4aExMVUEGEimOKiyFy0iQTRhWQtWlSLJITXMsxdbUJSCdqIgTs3KaggiIQaErkYoyLd2Az4jwqJFOSFpSM6/96vxXNwrjO2zW917r3f5RzuPccS/+YY/1+w98Odxa+rqdMllSHT0B5bgxf8Dev5D2WREZYauD6yuO6uTD3i5LiXI0RKQ6/jfuh9aaTPOyCsXTof3b+08YApU/Ae8zE7769rlTMLbpYwT6cM3gM4H1IZoYdlU0gOTYw+C/Hkjy94tbVp5eK+wIkJSb9pk9JJg5Sk9Cu6haR1Xkrj5vsNS1Kyhe82/OwjBmUjhlAOYDvdYTRXRvOo8hMvdmz3XnD2sgVvO/b+5t2np0DrTYBg3aT9JjJdDXCmOdMAp5qAs/4iUcidiwkdyvaJh/bY3QAA0wNGkbfqASKxGvvG52QA4Pg5QygA2Okquko/4cMS7LYzKNyqihkv33E66tCWkKXo7fn71XZOs26EJ+ltCoK01lWcr52v9HyTJEtAfDN3AANF/kRYR2A2/wLo0KVETuSETgAAAABJRU5ErkJggg==' |
| 17 | + |
| 18 | + root= tk.Tk() |
| 19 | + top= root |
| 20 | + top.geometry("260x80") |
| 21 | + top.title("Text to Image") |
| 22 | + top.resizable(0,0) |
| 23 | + favicon=tk.PhotoImage(data=img) |
| 24 | + root.wm_iconphoto(True, favicon) |
| 25 | + |
| 26 | +#create_buttons |
| 27 | +def create_buttons(): |
| 28 | + global convert_button |
| 29 | + convert_button=tk.Button(top) |
| 30 | + convert_button.place(x=90,y=20,height=40,width=80) |
| 31 | + convert_button.configure(text="Convert") |
| 32 | + convert_button.bind("<Button-1>",open_file) |
| 33 | + |
| 34 | +#open_file |
| 35 | +def open_file(event): |
| 36 | + global textfilename |
| 37 | + data=[('TXT', '*.txt')] |
| 38 | + textfilename=askopenfilename(filetypes=data, defaultextension=data) |
| 39 | + #if str(soundfile)!='': |
| 40 | + convert() |
| 41 | + |
| 42 | +#valid text length |
| 43 | +def text_length(textfile): |
| 44 | + global textlen |
| 45 | + textlen=0 |
| 46 | + textvar=textfile.read() |
| 47 | + textvarlen=len(textvar) |
| 48 | + textfile.seek(0) |
| 49 | + for n in range (1,textvarlen): |
| 50 | + char=textfile.read(1) |
| 51 | + asciicode=ord(char) |
| 52 | + if (asciicode>64 and asciicode<91) or (asciicode>96 and asciicode<123): |
| 53 | + textlen=textlen+1 |
| 54 | + |
| 55 | +#generate_header |
| 56 | +def generate_header(textlen): |
| 57 | + global headerbytes |
| 58 | + global width |
| 59 | + global heigth |
| 60 | + global bmpsize |
| 61 | + ratio=(textlen)/12 |
| 62 | + squareroot=math.sqrt(ratio) |
| 63 | + width=int((squareroot*4)/1.3333) |
| 64 | + heigth=int((squareroot*3)/1.3333) |
| 65 | + headerbytes=io.BytesIO() |
| 66 | + headerfile=open("26col2.bmp",'rb') |
| 67 | + bytesread=headerfile.read(2) |
| 68 | + headerbytes.write(bytesread) |
| 69 | + bmpsize=128+textlen |
| 70 | + filesize=width*heigth+240 |
| 71 | + byteappend=filesize.to_bytes(4,'little') |
| 72 | + headerbytes.write(byteappend) |
| 73 | + value=0 |
| 74 | + value=value.to_bytes(2,'little') |
| 75 | + headerbytes.write(value) |
| 76 | + value=0 |
| 77 | + value=value.to_bytes(2,'little') |
| 78 | + headerbytes.write(value) |
| 79 | + value=240 |
| 80 | + value=value.to_bytes(4,'little') |
| 81 | + headerbytes.write(value) |
| 82 | + value=40 |
| 83 | + value=value.to_bytes(4,'little') |
| 84 | + headerbytes.write(value) |
| 85 | + headerbytes.write(width.to_bytes(4,'little')) |
| 86 | + headerbytes.write(heigth.to_bytes(4,'little')) |
| 87 | + headerfile.seek(26) |
| 88 | + bytesread=headerfile.read(8) |
| 89 | + headerbytes.write(bytesread) |
| 90 | + filesize=width*heigth+240 |
| 91 | + sizebyte=filesize.to_bytes(4,'little') |
| 92 | + headerbytes.write(sizebyte) |
| 93 | + headerfile.seek(38) |
| 94 | + bytesread=headerfile.read(206) |
| 95 | + headerbytes.write(bytesread) |
| 96 | + |
| 97 | + |
| 98 | +#convert_bitrate |
| 99 | +def convert(): |
| 100 | + global headerbytes |
| 101 | + global width |
| 102 | + global heigth |
| 103 | + global bmpsize |
| 104 | + textfile=open(textfilename,'r') |
| 105 | + text_length(textfile) |
| 106 | + generate_header(textlen) |
| 107 | + tempfile=open("tempfile.bmp",'wb') |
| 108 | + headerbytes.seek(0) |
| 109 | + headerread=headerbytes.read() |
| 110 | + tempfile.write(headerread) |
| 111 | + textfile.seek(0) |
| 112 | + for pixels in range (1,textlen): |
| 113 | + char=textfile.read(1) |
| 114 | + asciicode=ord(char) |
| 115 | + if (asciicode>64 and asciicode<91): |
| 116 | + code=asciicode-64 |
| 117 | + pixarray=code.to_bytes(1,'big') |
| 118 | + tempfile.write(pixarray) |
| 119 | + elif (asciicode>96 and asciicode<123): |
| 120 | + code=asciicode-96 |
| 121 | + pixarray=code.to_bytes(1,'big') |
| 122 | + tempfile.write(pixarray) |
| 123 | + tempfile.close() |
| 124 | + data=[('BMP','*.bmp')] |
| 125 | + bmpfilesavename=asksaveasfilename(filetypes=data, defaultextension=data) |
| 126 | + shutil.copy("tempfile.bmp",bmpfilesavename) |
| 127 | + textfile.close() |
| 128 | + os.remove("tempfile.bmp") |
| 129 | + imgfileshow=Image.open(bmpfilesavename) |
| 130 | + imgfileshow.show() |
| 131 | + |
| 132 | + |
| 133 | +#main |
| 134 | +def main(): |
| 135 | + create_main_window() |
| 136 | + create_buttons() |
| 137 | + |
| 138 | +main() |
| 139 | +root.mainloop() |
0 commit comments