Skip to content

Instantly share code, notes, and snippets.

@pawnlord
Created April 25, 2024 14:56
Show Gist options
  • Save pawnlord/0da6d859642a54c1ee870dd4c23308e3 to your computer and use it in GitHub Desktop.
Save pawnlord/0da6d859642a54c1ee870dd4c23308e3 to your computer and use it in GitHub Desktop.
import cv2
import numpy as np
import sys
colors = [" ", "____", "/**/", "iiii", "aaaa", "aaaa"]
multiplier = len(colors) - 1
def read_greyscale(imagename):
img = cv2.imread(imagename)
img = img.astype(np.float64)
img = (img[:,:,0] + img[:,:,1] + img[:,:,2])/3
img = img.astype(np.uint8)
return img
def read_tokens(c_file):
with open(c_file, "r") as file:
lines = file.readlines()
prep = r""
code_start = 0
for i, line in enumerate(lines):
if len(line) == 0:
continue;
if line[0] == '#':
prep += line
else:
code_start = i
break
lines = lines[code_start:]
c_code = "\n".join(lines)
c_code = c_code.replace("\r", "").replace("\n", "").replace("\t", "")
while True:
last = c_code
c_code = c_code.replace(" ", " ");
if c_code == last:
break;
# print(c_code)
print(c_code)
tokens = [c + " " for c in c_code.split(" ")]
return prep, tokens
def make_sections(img):
sections = []
remaining_chars = []
sections_vert = np.array_split(img, 60)
for a in sections_vert:
sections_hori = np.array_split(a, 50, axis=1)
last = " "
left = []
for b in sections_hori:
avg = np.average(b)
c = colors[int(5 * avg / 255)]
# print(int(len(colors) * avg / 255))
if c == "____":
i = len(left) - 1
while i >= 0 and left[i] > 0:
left[i] += 4
i -= 1
left.append(4)
else:
left.append(0)
# print(c, end="")
# print(colors[int(5 * avg / 255)], end="")
last = c
remaining_chars.append(left)
sections.append(list(sections_hori))
# print()
return sections, remaining_chars
def generate_turkstra(sections, remaining_chars, tokens):
turkstra = ""
for i in range(len(sections)):
vert = sections[i]
j = 0
while j < len(vert):
hori = vert[j]
if len(tokens) > 1 and remaining_chars[i][j] > len(tokens[0]):
l = 0
tok = tokens[0]
remaining = remaining_chars[i][j]
while len(tokens) > 1 and l <= remaining:
l += len(tok)
turkstra += tok
del tokens[0]
tok = tokens[0]
j += (l//4)
turkstra += " " * (4 - (l % 4)) if l % 4 != 0 else ""
k = 0
else:
avg = np.average(hori)
c = colors[int(multiplier * avg / 255)]
turkstra += c
j += 1
turkstra += "\n"
return turkstra
# print(tokens)
def define_tokens(turkstra):
unique = set()
id_chars = ["_", "a", "i"]
for l in turkstra.split():
last = ""
token = []
for c in l:
if c in id_chars and token != []:
token.append(c)
elif not c in id_chars and token != []:
print(token)
# print(c, last, token, c != last)
token_str = "".join(token)
# if not token_str in unique:
unique.add(token_str)
token = []
if c in id_chars and last in id_chars and token == []:
token = [last, c]
last = c
# print(c, last, token, c != last)
token_str = "".join(token)
# if not token_str in unique:
unique.add(token_str)
token = []
unique.remove("")
return unique
if len(sys.argv) < 2:
print("Need image")
exit()
img = read_greyscale(sys.argv[1])
prep, tokens = read_tokens("hw13.c")
sections, remaining_chars = make_sections(img)
turkstra = generate_turkstra(sections, remaining_chars, tokens)
unique = define_tokens(turkstra)
for token in unique:
prep += "#define " + token + "\n"
print(prep)
print(turkstra)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment