Spaces:
Sleeping
Sleeping
Delete twords.py
Browse files
twords.py
DELETED
|
@@ -1,108 +0,0 @@
|
|
| 1 |
-
from PIL import Image
|
| 2 |
-
import math
|
| 3 |
-
class Twords:
|
| 4 |
-
def __init__(self):
|
| 5 |
-
self.char_dict={}
|
| 6 |
-
self.rev_dict={}
|
| 7 |
-
for i in range(256):
|
| 8 |
-
print (chr(i))
|
| 9 |
-
self.char_dict[chr(i)]=i
|
| 10 |
-
self.rev_dict[i]=chr(i)
|
| 11 |
-
self.char_keys=list(self.char_dict.keys())
|
| 12 |
-
self.rev_keys=list(self.rev_dict.keys())
|
| 13 |
-
|
| 14 |
-
def create_image(self,inp):
|
| 15 |
-
in_len=len(inp)
|
| 16 |
-
print(in_len)
|
| 17 |
-
dim=(math.sqrt(in_len))
|
| 18 |
-
print(dim)
|
| 19 |
-
int_dim=math.ceil(dim)
|
| 20 |
-
print(int_dim)
|
| 21 |
-
img = Image.new(mode = "RGB", size = (int_dim, int_dim),
|
| 22 |
-
color = (144, 144, 144))
|
| 23 |
-
pixels = img.load()
|
| 24 |
-
im_width=img.size[0]
|
| 25 |
-
im_height=img.size[1]
|
| 26 |
-
print(f'{im_width} :: {im_height}')
|
| 27 |
-
pixel_box=[]
|
| 28 |
-
pixel_list={}
|
| 29 |
-
cnt =0
|
| 30 |
-
go=True
|
| 31 |
-
if go:
|
| 32 |
-
for i in range(im_width):
|
| 33 |
-
if go:
|
| 34 |
-
for j in range(im_height):
|
| 35 |
-
if go:
|
| 36 |
-
r,g,b = inp[cnt]
|
| 37 |
-
pixels[i,j] = ((r,g,b))
|
| 38 |
-
cnt+=1
|
| 39 |
-
if cnt>=in_len:
|
| 40 |
-
go=False
|
| 41 |
-
print(img)
|
| 42 |
-
return img
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
def token_words(self,inp):
|
| 46 |
-
num_list=[]
|
| 47 |
-
char_list=list(inp)
|
| 48 |
-
#print(char_list)
|
| 49 |
-
#print(len(char_list))
|
| 50 |
-
for char in char_list:
|
| 51 |
-
try:
|
| 52 |
-
num_list.append(self.char_dict[char])
|
| 53 |
-
except Exception as e:
|
| 54 |
-
print("Error")
|
| 55 |
-
print(e)
|
| 56 |
-
print(f"Can't find::{char}::")
|
| 57 |
-
sort_list=[]
|
| 58 |
-
out_list=[]
|
| 59 |
-
cnt=1
|
| 60 |
-
for i, ea in enumerate(num_list):
|
| 61 |
-
#print(f'::{ea}')
|
| 62 |
-
if cnt <=3:
|
| 63 |
-
#print(f'::{ea}')
|
| 64 |
-
sort_list.append(ea)
|
| 65 |
-
cnt+=1
|
| 66 |
-
else:
|
| 67 |
-
out_list.append(sort_list)
|
| 68 |
-
sort_list=[ea]
|
| 69 |
-
cnt=2
|
| 70 |
-
if sort_list:
|
| 71 |
-
if len(sort_list)==1:
|
| 72 |
-
sort_list.append(144)
|
| 73 |
-
sort_list.append(144)
|
| 74 |
-
if len(sort_list)==2:
|
| 75 |
-
sort_list.append(144)
|
| 76 |
-
if len(sort_list)==3:
|
| 77 |
-
print(f"Max Len:: {sort_list}")
|
| 78 |
-
out_list.append(sort_list)
|
| 79 |
-
#print(out_list)
|
| 80 |
-
img=self.create_image(out_list)
|
| 81 |
-
word_box=out_list
|
| 82 |
-
rev_box=out_list
|
| 83 |
-
return img, img
|
| 84 |
-
|
| 85 |
-
def decode_im(self,img):
|
| 86 |
-
pixels = img.load() # create the pixel map
|
| 87 |
-
im_width=img.size[0]
|
| 88 |
-
im_height=img.size[1]
|
| 89 |
-
print(f'{im_width} :: {im_height}')
|
| 90 |
-
cnt =0
|
| 91 |
-
go=True
|
| 92 |
-
out_string=""
|
| 93 |
-
if go:
|
| 94 |
-
for i in range(im_width): # for every col:
|
| 95 |
-
if go:
|
| 96 |
-
for j in range(im_height):
|
| 97 |
-
if go:
|
| 98 |
-
out=pixels[i,j] # set the colour accordingly
|
| 99 |
-
r,g,b=out
|
| 100 |
-
if r in self.rev_keys and r!=144:
|
| 101 |
-
out_string+=self.rev_dict[r]
|
| 102 |
-
if g in self.rev_keys and g!=144:
|
| 103 |
-
out_string+=self.rev_dict[g]
|
| 104 |
-
if b in self.rev_keys and b!=144:
|
| 105 |
-
out_string+=self.rev_dict[b]
|
| 106 |
-
else:
|
| 107 |
-
pass
|
| 108 |
-
return out_string
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|