def decode(image): """ takes an encoded image and returns the message that was encoded inside that image. Arguments: image -- the image to be encoded. Image must be in the format generated by the SimpleImage module (i.e. a list of lists of tuples, where each tuple represents a pixel) """ output = '' curr_chunk = '' for row in image: for pixel in row: for num in pixel: curr_chunk += bits.get_bit(num,0) if len(curr_chunk) == BYTE_LENGTH: if curr_chunk == ZERO_BYTE: # found a byte 00000000, so no more message to # decode return output else: output += bits.bits_to_char(curr_chunk) curr_chunk = '' return output
def extractor(line,position): bit_str = '' for pixel in line: for intensity in pixel: bit_str += bits.get_bit(intensity,position) line_str = bits.bits_to_message(bit_str) line_n = int(line_str) return line_n
def decode_ext(image, num_bits): message_bit = '' for row in image: for a in range(len(image[0])): for b in range(len(image[0][0])): pixel_int = (row[a][b]) for count in range(num_bits): message_bit += bits.get_bit(pixel_int, count) message = bits_to_message(message_bit) return message
def decode(image): message_bit = '' for row in image: for a in range(len(image[0])): for b in range(len(image[0][0])): #The nested for loops help access the elements inside #the tuples that are the intensity values. pixel_int = (row[a][b]) #The intensity value is assigned to a variable. message_bit += bits.get_bit(pixel_int, 0) #The get_bit function in the bits.py module is called here #to access the last bit of the intensity value represented #in base 2. The last_bit would be '0' or '1'. The last bits #of all the intensity values are concatenated and stored #in a variable called message_bit. message = bits_to_message(message_bit) #The function bits_to_message is used to recover the original message. return message
def decode(image): """ Takes an image as its argument and returns a string of ASCII characters as its result. Decoding process: - Iterate over each intensity value in each pixel and do the following: - Convert intensity value of image to binary - Index LSB of intensity value and append it to a growing list of bits - After this operation, a string of bits is obtained, a part of which (or all of it) is the bitstream of the coded message. - Decode the entire string of bits using the bit_to_message function Inputs: image: a two-dimensional list of pixels, in zero-based row-major order. Each pixel is a three element tuple of integers in the range [0,255] Result: A string of ASCII characters, decoded from image using steganography scheme described above. Examples: >>> decode(encode(test_image,'')) '' >>> decode(encode(test_image,'hi')) 'hi' >>> decode(encode(test_image,'hello')) 'he' """ bitstream = '' for row in image: for pixel in row: for intensity in pixel: # Use get_bit function from bits.py library # to select the LSB of each intensity value bitstream += bits.get_bit(intensity,0) # Decode message using bits_to_message function message = bits.bits_to_message(bitstream) return message
def decode_ext(image, num_bits): """ Very slight change from original decode function; addition of extra nested 'for' loop allows iteration over one intensity value to extract multiple code bits, as determined by num_bits input """ if not(0 < num_bits <= 8): print ('Number of bits must be an integer between 1 and 8\ inclusive') return None bitstream = '' for row in image: for pixel in row: for intensity in pixel: # Extra 'for' loop so more than one bit can be # selected from each intensity value for pos in range(num_bits): bitstream += bits.get_bit(intensity,pos) message = bits.bits_to_message(bitstream) return message
from PIL import Image import numpy as np import sys, bits src_img = Image.open(sys.argv[1]) pixels = np.array(src_img) out_buffer = [] x = 0 y = 0 for i in range(int(sys.argv[2])): buffer = 0 for j in range(8): pixel = pixels[x][y] bit = bits.get_bit(pixel[0], bits.LSB) buffer = bits.set_bit(buffer, j, bit) x += 1 if x >= src_img.width: x = 0 y += 1 out_buffer.append(buffer) fh = open(sys.argv[3], "wb") fh.write(bytes(out_buffer))
import sys, bits def array_image_save(array, image_path): image = Image.fromarray(array) image.save(image_path) src_img = Image.open(sys.argv[1]) pixels = np.array(src_img) fh = open(sys.argv[2], "rb") binary_data = fh.read() x = 0 y = 0 for i in range(len(binary_data)): buffer = binary_data[i] for j in range(8): pixel = pixels[x][y] bit = bits.get_bit(buffer, j) pixel[0] = bits.set_bit(pixel[0], bits.LSB, bit) pixels[x][y] = pixel x += 1 if x >= src_img.width: x = 0 y += 1 array_image_save(pixels, sys.argv[3]) print("Encoded %s bytes" % len(binary_data))