Skip to content

Instantly share code, notes, and snippets.

@RcColes
Created August 27, 2018 13:15
Show Gist options
  • Save RcColes/88db8c3c6f84e0f68b2b3f7ca07dfc59 to your computer and use it in GitHub Desktop.
Save RcColes/88db8c3c6f84e0f68b2b3f7ca07dfc59 to your computer and use it in GitHub Desktop.
Decoded for CDPR livestream
import numpy as np
import matplotlib.pyplot as plot
import string
symbols = " " + string.ascii_letters + string.digits + "+" + "/"
# symbols = "S"
chars=[]
np.set_printoptions(threshold=np.nan)
def clamp(a):
b = a.flatten()
b = np.array(b, dtype=np.int32)
b += 128
b = np.clip(b, 0, 255)
b -= 254
b = np.clip(b, 0, 255)
b *= 255
b = np.reshape(b, a.shape)
return b
for sym in symbols:
try:
if sym == "/":
char = plot.imread("letters/?.png")[:,:,:3]
else:
char = plot.imread("letters/" + sym + ".png")[:,:,:3]
char *= 255
char = np.array(char, dtype=np.int32)
char = clamp(char)
chars.append(char)
except FileNotFoundError:
chars.append(np.zeros((40,20,3)))
# print(symbols)
file = "473.bmp"
char_w = 20
char_h = 40
def decode(a):
a = clamp(a)
low = 9999999
out = 99999
for c in range(len(chars)):
char = chars[c]
tmp = compare(a, char)
# print(symbols[c])
# print(tmp)
if tmp < low:
# print(str(tmp)+ " should be lower than " + str(low))
# print(c)
# print(symbols[c])
out = c
low = tmp
# print()
# print(out)
# print(symbols[out], end="")
# print(low)
# f = plot.figure()
# f.add_subplot(1,3,1)
# plot.imshow(a)
# f.add_subplot(1,3,2)
# plot.imshow(chars[out])
# f.add_subplot(1,3,3)
# plot.imshow(a - chars[out])
# plot.show()
return out
def compare(a, b):
# print(a)
# print(b)
# print((a - b))
return np.sum(np.abs(a.flatten() - b.flatten()))
im = plot.imread(file)
im = im[:,269:1709]
# print(im.shape)
string = ""
for j in range(0, im.shape[1], char_h):
for i in range(0, im.shape[0], char_w):
# for j in range(0, char_h, char_h):
# for i in range(0, 2 * char_w, char_w):
char = im[j:j+char_h, i:i+char_w]
if char.shape[0] == 0 or char.shape[1] == 0:
pass
else:
string += symbols[(decode(char))]
# plot.imsave("grid/" + str(j) +"x"+ str(i) + ".png", char)
print(string)
# plot.imshow(im)
# plot.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment