Created
May 7, 2020 13:43
-
-
Save StoneLabs/6e5f40421eee757bdd708e95a0243216 to your computer and use it in GitHub Desktop.
Binary string to pixel matrix visualization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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