Added conversion to base64 file for binary file.

This commit is contained in:
cedricbonhomme 2010-10-05 07:25:00 +02:00
parent 15e25eec2b
commit 04984d5e88
3 changed files with 37 additions and 10 deletions

View file

@ -65,7 +65,7 @@ if __name__ == '__main__':
encoded_image_file = "./pictures/Lenna_enc.png"
# at this point don't exceed 255 characters
secret_message = "Parce que je le vaut bien!"
img1 = Image.open(original_image_file)
img_encoded = hide(img1, secret_message)

View file

@ -19,10 +19,12 @@ def hide(img, message):
index = 0
message = message + '~~~'
message_bits = tools.a2bits(message)
#message_bits = tools.a2bits(message)
message_bits = "".join(tools.a2bits_list(message))
npixels = width * height
if len(message_bits) > npixels * 3:
print """Too long message (%s > %s).""" % (len(message_bits), npixels * 3)
return """Too long message (%s > %s).""" % (len(message_bits), npixels * 3)
for row in range(height):
@ -70,10 +72,10 @@ def reveal(img):
if __name__ == '__main__':
# Point of entry in execution mode
original_image_file = "./pictures/montenach.png"
encoded_image_file = "./pictures/montenach_enc.png"
original_image_file = "./pictures/free.png"
encoded_image_file = "./pictures/free_enc.png"
secret_message = "Avec la technique LSB (Least Significant Bit) l'oeil humain (un normal ;-)) ne voit plus la difference"
with open("./lorem_ipsum.txt", "r") as f:
with open("./Portishead_-_Deep_Water.ogg_b64.txt", "r") as f:
secret_message = f.read()
img1 = Image.open(original_image_file)

View file

@ -2,11 +2,10 @@
# -*- coding: utf-8 -*-
def a2bits(chars):
"""
Converts a string to its bits representation as a string of 0's and 1's.
>>> a2bits("Hello World!")
'010010000110010101101100011011000110111100100000010101110110111101110010011011000110010000100001'
"""
@ -15,7 +14,7 @@ def a2bits(chars):
def a2bits_list(chars):
"""
Convert a string to its bits representation as a list of 0's and 1's.
>>> a2bits_list("Hello World!")
['01001000',
'01100101',
@ -50,9 +49,35 @@ def n_at_a_time(items, n, fillvalue):
"""
Returns an iterator which groups n items at a time.
Any final partial tuple will be padded with the fillvalue
>>> list(n_at_a_time([1, 2, 3, 4, 5], 2, 'X'))
[(1, 2), (3, 4), (5, 'X')]
"""
it = iter(items)
return its.izip_longest(*[it] * n, fillvalue=fillvalue)
return its.izip_longest(*[it] * n, fillvalue=fillvalue)
def binary2base64(binary_file, output_file):
"""
"""
# use mode = "rb" to read binary file
fin = open(binary_file, "rb")
binary_data = fin.read()
fin.close()
# encode binary to base64 string (printable)
b64_data = base64.b64encode(binary_data)
fout = open(output_file, "w")
fout.write(b64_data)
fout.close
def base642binary(b64_fname):
"""
"""
# read base64 string
fin = open(b64_fname, "r")
b64_str = fin.read()
fin.close()
# decode base64 string to original binary sound object
return base64.b64decode(b64_str)