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

@ -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,7 +2,6 @@
# -*- coding: utf-8 -*-
def a2bits(chars):
"""
Converts a string to its bits representation as a string of 0's and 1's.
@ -56,3 +55,29 @@ def n_at_a_time(items, n, fillvalue):
"""
it = iter(items)
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)