Skip to content

Instantly share code, notes, and snippets.

@evansneath
Created May 28, 2012 15:51
Show Gist options
  • Save evansneath/2819816 to your computer and use it in GitHub Desktop.
Save evansneath/2819816 to your computer and use it in GitHub Desktop.
msg_to_image - converts binary string to matrix form using its prime factors
# name: msg_to_img
#
# description: a simple program for converting a binary string of values
# and decyphering that string into a matrix form using its factors
#
# author: evan sneath
# date: 5/28/2012
# license: This software licensed under the Open Software License version 3.0:
# http://www.opensource.org/licenses/OSL-3.0
def msg_to_img(msg, picturize=False):
factor, dim = [], []
# if the user wants a more picture-like output replace '0' with ' ', '1' with 'X'
if picturize == True:
msg = msg.replace('0', ' ')
msg = msg.replace('1', 'X')
# get factors
for i in range(len(msg) / 2 + 1):
if i != 0 and i != 1 and len(msg) % i == 0:
factor.append(i)
# find reasonable dimension for the found factors
if len(factor) == 0:
print 'msg length has no factors, stopping...'
return
elif len(factor) == 1:
print 'double factor, continuing...'
dim = [factor[0], factor[0]]
elif len(factor) == 2:
print 'two main factors, continuing...'
dim = factor
else:
print 'too many factors, stopping...'
return
print '\nimage orientation 1 (' + str(dim[0]) + ' x ' + str(dim[1]) + '):\n'
# print the potential image
for i in range(dim[0]):
print msg[i*dim[1]:(i+1)*dim[1]]
print '\nimage orientation 2 (' + str(dim[1]) + ' x ' + str(dim[0]) + '):\n'
# print the transferse of the image
for i in range(dim[1]):
print msg[i*dim[0]:(i+1)*dim[0]]
return
if __name__ == '__main__':
msg = '10001011111100010100001000101000010001010000100010100001000101000011111011111'
msg_to_img(msg, picturize=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment