From 35299d8d05aae3190db38958c4482e62aa6011f9 Mon Sep 17 00:00:00 2001 From: cedricbonhomme Date: Sat, 2 Oct 2010 09:48:27 +0200 Subject: [PATCH] Little modifications in the structure. Added comments. --- LSB.py | 14 ++++++-------- basic.py | 57 +++++++++++++++++++++++++++++--------------------------- 2 files changed, 36 insertions(+), 35 deletions(-) diff --git a/LSB.py b/LSB.py index 164d8e0..e8487d2 100644 --- a/LSB.py +++ b/LSB.py @@ -9,7 +9,7 @@ import tools from PIL import Image -def encode_image(img, message): +def hide(img, message): """ Hide a message (string) in an image with the LSB (Less Significant Bit) technic. @@ -59,7 +59,7 @@ def encode_image(img, message): return encoded -def decode_image(img): +def reveal(img): """ Find a message in an encoded image (with the LSB technic). @@ -86,16 +86,14 @@ if __name__ == '__main__': # Point of entry in execution mode original_image_file = "./pictures/Lenna.png" encoded_image_file = "Lenna_enc.png" + secret_message = "Avec la technique LSB (Least Significant Bit) l'oeil humain (un normal ;-)) ne voit plus la difference!" - img = Image.open(original_image_file) - - secret_msg = "Avec la technique LSB (Least Significant Bit) l'oeil humain (un normal ;-)) ne voit plus la difference!" - img_encoded = encode_image(img, secret_msg) - + img1 = Image.open(original_image_file) + img_encoded = hide(img1, secret_message) if img_encoded: # Save it img_encoded.save(encoded_image_file) # Test it img2 = Image.open(encoded_image_file) - print(decode_image(img2)) \ No newline at end of file + print reveal(img2) \ No newline at end of file diff --git a/basic.py b/basic.py index 7713d6d..3b8c72a 100644 --- a/basic.py +++ b/basic.py @@ -1,14 +1,18 @@ # -*- coding: utf-8 -*- +__author__ = "Cedric Bonhomme" +__version__ = "$Revision: 0.1 $" +__date__ = "$Date: 2010/10/01 $" + from PIL import Image -def encode_image(img, msg): +def hide(img, message): """ use the red portion of an image (r, g, b) tuple to - hide the msg string characters as ASCII values + hide the message string characters as ASCII values the red value of the first pixel is used for length of string """ - length = len(msg) + length = len(message) # limit length of message to 255 if length > 255: return False @@ -19,11 +23,11 @@ def encode_image(img, msg): for row in range(height): for col in range(width): (r, g, b) = img.getpixel((col, row)) - # first value is length of msg + # first value is length of message if row == 0 and col == 0 and index < length: asc = length elif index <= length: - c = msg[index -1] + c = message[index -1] asc = ord(c) else: asc = r @@ -31,15 +35,14 @@ def encode_image(img, msg): index += 1 return encoded - -def decode_image(img): +def reveal(img): """ check the red portion of an image (r, g, b) tuple for hidden message characters (ASCII values) the red value of the first pixel is used for length of string """ width, height = img.size - msg = "" + message = "" index = 0 for row in range(height): for col in range(width): @@ -48,25 +51,25 @@ def decode_image(img): if row == 0 and col == 0: length = r elif index <= length: - msg += chr(r) + message += chr(r) index += 1 - return msg + return message +if __name__ == '__main__': + # Point of entry in execution mode + original_image_file = "./pictures/Lenna.png" + 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) -original_image_file = "./pictures/Lenna.png" -encoded_image_file = "./pictures/Lenna_enc.png" -img = Image.open(original_image_file) - - -# at this point don't exceed 255 characters -secret_msg = "Parce que je le vaut bien!" -img_encoded = encode_image(img, secret_msg) - -if img_encoded: - # save it ... - img_encoded.save(encoded_image_file) - # test it ... - img2 = Image.open(encoded_image_file) - print(decode_image(img2)) -else: - print("text too long! (don't exeed 255 characters)") + if img_encoded: + # Save it + img_encoded.save(encoded_image_file) + # Test it + img2 = Image.open(encoded_image_file) + print reveal(img2) + else: + print("text too long! (don't exeed 255 characters)") \ No newline at end of file