Skip to content

Instantly share code, notes, and snippets.

@tomjnixon
Last active November 26, 2018 09:30
Show Gist options
  • Save tomjnixon/b29884d5120d145e5f26ce2453624836 to your computer and use it in GitHub Desktop.
Save tomjnixon/b29884d5120d145e5f26ce2453624836 to your computer and use it in GitHub Desktop.
scarf decode
import numpy as np
im_str = """
11111101 11111101 11111101
11111101 10001100 11000100
10110100 11011000 10101000
11101010 11111101 11000100
10111100 11100100 10111110
11111101 11000101 10110000
10101000 10111000 10101100
11011000 11001100 11111101
11101000 10111100 10101100
11011000 10001100 11111101
11110100 11101000 10110100
10001000 11111101 10101100
10011000 10110100 11011000
11111000 11011000 10101000
10011000 11111101 10110000
10100100 11111101 10101100
10010100 10111100 11101000
11111101 10011000 10101100
11000100 10110100 11110100
10011100 10111100 10100110
""".strip()
im = np.array([[c == '1' for c in line if c in ('0', '1')]
for line in im_str.split("\n")],
dtype=bool)
print(im.shape)
# correct three errors:
# - reading in the wrong direction vertically
# - reading in the wrong direction horizontally; this causes the msb to be on
# the wrong end, hence the roll
# - stitches are 0s not 1s, so invert
im = ~np.roll(im, -1, axis=1)[::-1, ::-1]
byte_arr = np.packbits(im, axis=1)
print(byte_arr.view("c").astype("U1"))
print(byte_arr.tobytes().decode("utf8"))
(20, 24)
[['M' 'a' 'c']
['h' 'i' 'n']
['e' 's' ' ']
['t' 'a' 'k']
['e' ' ' 'm']
['y' ' ' 's']
['u' 'r' 'p']
['r' 'i' 's']
['e' ' ' 'w']
['i' 't' 'h']
[' ' 'g' 'r']
['e' 'a' 't']
[' ' 'f' 'r']
['e' 'q' 'u']
['y' '.' ' ']
['A' 'l' 'a']
['n' ' ' 'T']
['u' 'r' 'i']
['n' 'g' ' ']
[' ' ' ' ' ']]
Machines take my surprise with great frequy. Alan Turing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment