Skip to content

Instantly share code, notes, and snippets.

@StoneLabs
Created May 7, 2020 13:43
Show Gist options
  • Save StoneLabs/6e5f40421eee757bdd708e95a0243216 to your computer and use it in GitHub Desktop.
Save StoneLabs/6e5f40421eee757bdd708e95a0243216 to your computer and use it in GitHub Desktop.
Binary string to pixel matrix visualization
# Simple script to visualize a binary string as a pixel matrix
# Example input: "01100001110101011100010[...]"
# Note that the data length should be some n^2 to produce a square image
import numpy as np
from math import sqrt
file = open('input.txt', 'r')
cont = file.read()[:-1]
data = [_char == '1' for _char in cont]
edge = int(sqrt(len(data)))
data = np.array(data).reshape(edge, edge)
import matplotlib.pyplot as plt
plt.imshow(data, aspect=1, interpolation='none')
plt.savefig('out.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment