def encode_ext(image, message, num_bits):
    message_bit = message_to_bits(message)
    len_image = SimpleImage.get_width(image)*SimpleImage.get_height(image)*3
    len_message = len(message_bit)
    if len_image > len_message:
        message_bit += (len_image - len_message) * "0"
        new_image = change_lsb_ext (image,message_bit, num_bits)
    elif len_image < len_message:
        new_image = change_lsb_ext(image,message_bit[0:len_image], num_bits)
    else:
        new_image = change_lsb_ext(image,message_bit, num_bits) 
    return new_image
def decode_to_file(image_name, num_bits, filename):
    image = sim.read_image(image_name)
    message = decode_ext(image, num_bits)
    file = open(filename,'w')
    file.write(message)
    file.close
    return message 
def encode(image,message):
    message_bit = message_to_bits(message)
    len_image = SimpleImage.get_width(image)*SimpleImage.get_height(image)*3
    #To calculate the no of intensity values in the image, 2 helper
    #functions predefined in the SimpleImage.py module are used. 
    #The get_width and get_height functions return the no of columns 
    #and rows in the image respectively. The number of intensity values
    #is the product of the no of rows, no of columns and the integer 3 
    #(because each pixel has 3 intensity values).
    len_message = len(message_bit)
    #The length of the message string is also calculated.
    if len_image > len_message:
        message_bit += (len_image - len_message) * "0"
        #If there are more intensity values in the image than bits in
        #the binary string, then, the LSBs of the remaining intensity
        #values are set to 0. To achieve this, the message_bit string
        #is concatenated with (len_image - len_message)number of zeroes('0').
        new_image = change_lsb (image,message_bit)
        #The change_lsb function is called and it encodes the message_bit
        #(that has been concatenated with 0s) into the image and returns
        #a new image which is assigned to a variable called new_image.
    elif len_image < len_message:
        #If there are more bits in the binary string than intensity
        #values in the image, the function encodes as many bits of the
        #binary string as possible. To achieve this, message_bit is sliced
        #and only a certain no of bits(certain no is equal to no of
        #intensity values)are encoded into the image.
        new_image = change_lsb(image,message_bit[0:len_image])
        #The change_lsb function is called and it encodes the message_bit
        #sliced from 0 to the number of intensity values into the image.
        #It returns the new image to a variable called new_image.
    else:
        new_image = change_lsb(image,message_bit) 
        #In the case where no of bits and the no of intensity values
        #are equal, change_lsb returns a new image without having to make
        #any changes to the string message_bit.
    return new_image        
def unscramblePILimage(PILimage):
    image = sim.to_rectangle(PILimage)
    return sim.to_flat(unscramble(image))
def decodePILimage(PILimage):
    image = sim.to_rectangle(PILimage)
    return decode(image)
def encodePILimage(PILimage, message):
    image = sim.to_rectangle(PILimage)
    output = encode(image, message)
    return sim.to_flat(output)
def decode_direct(image_name, num_bits):
    image = sim.read_image(image_name)
    message = decode_ext(image,num_bits)
    return message
def encode_direct(image_name,message,num_bits,coded_image_name):
    image = sim.read_image(image_name)
    encoded_image = encode_ext(image,message,num_bits)
    sim.write_image(encoded_image, coded_image_name)
    return None