def hide(input_image_file, message, generator_function): """ Hide a message (string) in an image with the LSB (Least Significant Bit) technique. """ img = Image.open(input_image_file) img_list = list(img.getdata()) width, height = img.size index = 0 message = str(len(message)) + ":" + message #message_bits = tools.a2bits(message) message_bits = "".join(tools.a2bits_list(message)) npixels = width * height if len(message_bits) > npixels * 3: return """Too long message (%s > %s).""" % (len(message_bits), npixels * 3) generator = getattr(generators, generator_function)() while index + 3 <= len(message_bits): generated_number = generator.next() (r, g, b) = img_list[generated_number] # Change the Least Significant Bit of each colour component. r = tools.setlsb(r, message_bits[index]) g = tools.setlsb(g, message_bits[index + 1]) b = tools.setlsb(b, message_bits[index + 2]) # Save the new pixel img_list[generated_number] = (r, g, b) index += 3 # create empty new image of appropriate format encoded = Image.new('RGB', (img.size)) # insert saved data into the image encoded.putdata(img_list) return encoded
def hide(input_image_file, message, generator_function): """ Hide a message (string) in an image with the LSB (Least Significant Bit) technique. """ img = Image.open(input_image_file) img_list = list(img.getdata()) width, height = img.size index = 0 message = str(len(message)) + ":" + message #message_bits = tools.a2bits(message) message_bits = "".join(tools.a2bits_list(message)) npixels = width * height if len(message_bits) > npixels * 3: raise Exception("""The message you want to hide is too long (%s > %s).""" % (len(message_bits), npixels * 3)) generator = getattr(generators, generator_function)() while index + 3 <= len(message_bits) : generated_number = next(generator) (r, g, b) = img_list[generated_number] # Change the Least Significant Bit of each colour component. r = tools.setlsb(r, message_bits[index]) g = tools.setlsb(g, message_bits[index+1]) b = tools.setlsb(b, message_bits[index+2]) # Save the new pixel img_list[generated_number] = (r, g , b) index += 3 # create empty new image of appropriate format encoded = Image.new('RGB', (img.size)) # insert saved data into the image encoded.putdata(img_list) return encoded
def hide(input_image_file, message): """ Hide a message (string) in an image with the LSB (Least Significant Bit) technique. """ img = Image.open(input_image_file) encoded = img.copy() width, height = img.size index = 0 message = str(len(message)) + ":" + message #message_bits = tools.a2bits(message) message_bits = "".join(tools.a2bits_list(message)) print message_bits npixels = width * height if len(message_bits) > npixels * 3: raise Exception( """The message you want to hide is too long (%s > %s).""" % (len(message_bits), npixels * 3)) for row in range(height): for col in range(width): if index + 3 <= len(message_bits): # Get the colour component. (r, g, b) = img.getpixel((col, row)) # Change the Least Significant Bit of each colour component. r = tools.setlsb(r, message_bits[index]) g = tools.setlsb(g, message_bits[index + 1]) b = tools.setlsb(b, message_bits[index + 2]) # Save the new pixel encoded.putpixel((col, row), (r, g, b)) index += 3 return encoded
def hide(input_image_file, message): """ Hide a message (string) in an image with the LSB (Least Significant Bit) technique. """ img = Image.open(input_image_file) encoded = img.copy() width, height = img.size index = 0 message = str(len(message)) + ":" + message #message_bits = tools.a2bits(message) message_bits = "".join(tools.a2bits_list(message)) print message_bits npixels = width * height if len(message_bits) > npixels * 3: raise Exception("""The message you want to hide is too long (%s > %s).""" % (len(message_bits), npixels * 3)) for row in range(height): for col in range(width): if index + 3 <= len(message_bits) : # Get the colour component. (r, g, b) = img.getpixel((col, row)) # Change the Least Significant Bit of each colour component. r = tools.setlsb(r, message_bits[index]) g = tools.setlsb(g, message_bits[index+1]) b = tools.setlsb(b, message_bits[index+2]) # Save the new pixel encoded.putpixel((col, row), (r, g , b)) index += 3 return encoded
def hide(img, message): """ Hide a message (string) in an image with the LSB (Least Significant Bit) technique. """ encoded = img.copy() width, height = img.size index = 0 message = message + '~~~' message_bits = tools.a2bits(message) npixels = width * height if len(message_bits) > npixels * 3: return """Too long message (%s > %s).""" % (len(message_bits), npixels * 3) for row in range(height): for col in range(width): if index + 3 <= len(message_bits): # Get the colour component. (r, g, b) = img.getpixel((col, row)) # Change the Least Significant Bit of each colour component. r = tools.setlsb(r, message_bits[index]) g = tools.setlsb(g, message_bits[index + 1]) b = tools.setlsb(b, message_bits[index + 2]) # Save the new pixel encoded.putpixel((col, row), (r, g, b)) index += 3 return encoded