Bug fix. Bad argument in *hide* function when used as module.

This commit is contained in:
cedricbonhomme 2011-03-30 22:11:33 +02:00
parent a46dc62cf8
commit a2cab3a9f5

View file

@ -30,11 +30,12 @@ from PIL import Image
import tools import tools
def hide(img, message): def hide(input_image_file, message):
""" """
Hide a message (string) in an image with the Hide a message (string) in an image with the
LSB (Least Significant Bit) technique. LSB (Least Significant Bit) technique.
""" """
img = Image.open(input_image_file)
encoded = img.copy() encoded = img.copy()
width, height = img.size width, height = img.size
index = 0 index = 0
@ -67,11 +68,12 @@ def hide(img, message):
return encoded return encoded
def reveal(img): def reveal(input_image_file):
""" """
Find a message in an image Find a message in an image
(with the LSB technique). (with the LSB technique).
""" """
img = Image.open(input_image_file)
width, height = img.size width, height = img.size
buff, count = 0, 0 buff, count = 0, 0
bitab = [] bitab = []
@ -96,6 +98,15 @@ def reveal(img):
return "".join(bitab)[len(str(limit))+1:] return "".join(bitab)[len(str(limit))+1:]
return "" return ""
def write(image, output_image_file):
"""
"""
try:
image.save(output_image_file)
except Exception, e:
# If hide() returns an error (Too long message).
print e
if __name__ == '__main__': if __name__ == '__main__':
# Point of entry in execution mode. # Point of entry in execution mode.
from optparse import OptionParser from optparse import OptionParser
@ -135,8 +146,7 @@ if __name__ == '__main__':
elif options.secret_message == "" and options.secret_file != "": elif options.secret_message == "" and options.secret_file != "":
secret = tools.binary2base64(options.secret_file) secret = tools.binary2base64(options.secret_file)
img = Image.open(options.input_image_file) img_encoded = hide(options.input_image_file, secret)
img_encoded = hide(img, secret)
try: try:
img_encoded.save(options.output_image_file) img_encoded.save(options.output_image_file)
except Exception, e: except Exception, e:
@ -144,8 +154,7 @@ if __name__ == '__main__':
print e print e
elif options.reveal: elif options.reveal:
img = Image.open(options.input_image_file) secret = reveal(options.input_image_file)
secret = reveal(img)
if options.secret_binary != "": if options.secret_binary != "":
data = tools.base642binary(secret) data = tools.base642binary(secret)
with open(options.secret_binary, "w") as f: with open(options.secret_binary, "w") as f: