|
| 1 | +"""Txt2BMP 1.2.1 - Convert text to an image to spot hidden pareidolias. |
| 2 | +Copyright (C) 2022 Fonazza-Stent |
| 3 | +
|
| 4 | +This program is free software: you can redistribute it and/or modify |
| 5 | +it under the terms of the GNU General Public License as published by |
| 6 | +the Free Software Foundation, either version 3 of the License, or |
| 7 | +(at your option) any later version. |
| 8 | +
|
| 9 | +This program is distributed in the hope that it will be useful, |
| 10 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +GNU General Public License for more details. |
| 13 | +
|
| 14 | +You should have received a copy of the GNU General Public License |
| 15 | +along with this program. If not, see <https://www.gnu.org/licenses/>.""" |
| 16 | + |
| 17 | +import tkinter as tk |
| 18 | +import tkinter.ttk as ttk |
| 19 | +from tkinter.filedialog import asksaveasfilename |
| 20 | +from tkinter.filedialog import askopenfilename |
| 21 | +import os |
| 22 | +from tkinter import messagebox |
| 23 | +import shutil |
| 24 | +from PIL import ImageTk, Image |
| 25 | +import io |
| 26 | +import math |
| 27 | + |
| 28 | +#create main window |
| 29 | +def create_main_window(): |
| 30 | + global top |
| 31 | + global root |
| 32 | + 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==' |
| 33 | + |
| 34 | + root= tk.Tk() |
| 35 | + top= root |
| 36 | + top.geometry("260x80") |
| 37 | + top.title("Text to Image") |
| 38 | + top.resizable(0,0) |
| 39 | + favicon=tk.PhotoImage(data=img) |
| 40 | + root.wm_iconphoto(True, favicon) |
| 41 | + |
| 42 | +#create_buttons |
| 43 | +def create_buttons(): |
| 44 | + global convert_button |
| 45 | + convert_button=tk.Button(top) |
| 46 | + convert_button.place(x=90,y=20,height=40,width=80) |
| 47 | + convert_button.configure(text="Convert") |
| 48 | + convert_button.bind("<Button-1>",open_file) |
| 49 | + |
| 50 | +#open_file |
| 51 | +def open_file(event): |
| 52 | + global textfilename |
| 53 | + data=[('TXT', '*.txt')] |
| 54 | + textfilename=askopenfilename(filetypes=data, defaultextension=data) |
| 55 | + #if str(soundfile)!='': |
| 56 | + parse() |
| 57 | + convert() |
| 58 | + |
| 59 | +#parse |
| 60 | +def parse(): |
| 61 | + global contents |
| 62 | + with io.open(textfilename, 'r', encoding='utf-8') as file_object: |
| 63 | + contents='' |
| 64 | + eof=False |
| 65 | + while eof==False: |
| 66 | + try: |
| 67 | + char=file_object.read(1) |
| 68 | + except: |
| 69 | + char="?" |
| 70 | + contents=contents+char |
| 71 | + if char=='': |
| 72 | + eof=True |
| 73 | + |
| 74 | +#valid text length |
| 75 | +def text_length(textfile): |
| 76 | + global textlen |
| 77 | + textlen=0 |
| 78 | + textvar=contents |
| 79 | + textvarlen=len(textvar) |
| 80 | + textfile.seek(0) |
| 81 | + for n in range (1,textvarlen): |
| 82 | + char=textfile.read(1) |
| 83 | + asciicode=ord(char) |
| 84 | + if (asciicode>64 and asciicode<91) or (asciicode>96 and asciicode<123): |
| 85 | + textlen=textlen+1 |
| 86 | + |
| 87 | +#generate_header |
| 88 | +def generate_header(textlen): |
| 89 | + global headerbytes |
| 90 | + global width |
| 91 | + global heigth |
| 92 | + global bmpsize |
| 93 | + ratio=(textlen)/12 |
| 94 | + squareroot=math.sqrt(ratio) |
| 95 | + width=int((squareroot*4)/1.3333) |
| 96 | + heigth=int((squareroot*3)/1.3333) |
| 97 | + headerbytes=io.BytesIO() |
| 98 | + headerfile=open("26col2.bmp",'rb') |
| 99 | + bytesread=headerfile.read(2) |
| 100 | + headerbytes.write(bytesread) |
| 101 | + bmpsize=128+textlen |
| 102 | + filesize=width*heigth+240 |
| 103 | + byteappend=filesize.to_bytes(4,'little') |
| 104 | + headerbytes.write(byteappend) |
| 105 | + value=0 |
| 106 | + value=value.to_bytes(2,'little') |
| 107 | + headerbytes.write(value) |
| 108 | + value=0 |
| 109 | + value=value.to_bytes(2,'little') |
| 110 | + headerbytes.write(value) |
| 111 | + value=240 |
| 112 | + value=value.to_bytes(4,'little') |
| 113 | + headerbytes.write(value) |
| 114 | + value=40 |
| 115 | + value=value.to_bytes(4,'little') |
| 116 | + headerbytes.write(value) |
| 117 | + headerbytes.write(width.to_bytes(4,'little')) |
| 118 | + headerbytes.write(heigth.to_bytes(4,'little')) |
| 119 | + headerfile.seek(26) |
| 120 | + bytesread=headerfile.read(8) |
| 121 | + headerbytes.write(bytesread) |
| 122 | + filesize=width*heigth+240 |
| 123 | + sizebyte=filesize.to_bytes(4,'little') |
| 124 | + headerbytes.write(sizebyte) |
| 125 | + headerfile.seek(38) |
| 126 | + bytesread=headerfile.read(206) |
| 127 | + headerbytes.write(bytesread) |
| 128 | + |
| 129 | + |
| 130 | +#convert_bitrate |
| 131 | +def convert(): |
| 132 | + global headerbytes |
| 133 | + global width |
| 134 | + global heigth |
| 135 | + global bmpsize |
| 136 | + textfile=contents |
| 137 | + textlen=len(contents) |
| 138 | + generate_header(textlen) |
| 139 | + tempfile=open("tempfile.bmp",'wb') |
| 140 | + headerbytes.seek(0) |
| 141 | + headerread=headerbytes.read() |
| 142 | + tempfile.write(headerread) |
| 143 | + for pixels in range (1,textlen): |
| 144 | + char=textfile[pixels] |
| 145 | + asciicode=ord(char) |
| 146 | + if (asciicode>64 and asciicode<91): |
| 147 | + code=asciicode-64 |
| 148 | + pixarray=code.to_bytes(1,'big') |
| 149 | + tempfile.write(pixarray) |
| 150 | + elif (asciicode>96 and asciicode<123): |
| 151 | + code=asciicode-96 |
| 152 | + pixarray=code.to_bytes(1,'big') |
| 153 | + tempfile.write(pixarray) |
| 154 | + tempfile.close() |
| 155 | + data=[('BMP','*.bmp')] |
| 156 | + bmpfilesavename=asksaveasfilename(filetypes=data, defaultextension=data) |
| 157 | + shutil.copy("tempfile.bmp",bmpfilesavename) |
| 158 | + os.remove("tempfile.bmp") |
| 159 | + imgfileshow=Image.open(bmpfilesavename) |
| 160 | + imgfileshow.show() |
| 161 | + |
| 162 | + |
| 163 | +#main |
| 164 | +def main(): |
| 165 | + create_main_window() |
| 166 | + create_buttons() |
| 167 | + |
| 168 | +main() |
| 169 | +root.mainloop() |
0 commit comments