So, I am supposed to be encoding and decoding hidden messages in images. I have perfect the decoding hidden messages part, but am a bit stuck on the encoding part (encoding a message by changing the red value in the pixel color to the unicode number of the letter. I thought I would use a for loop in the function to loop to each position in the string for the message, but that doesn't seem to work right. All I end up getting is the letter at the last position repeated to the end of the number of pixels. I am not sure what to do about that. Also, I want it to stop when it reaches the last letter/number/symbol in the string, but it doesn't do that either.
from image import * # ***** THIS IS WHERE YOUR FUNCTION DEFINITION SHOULD GO ***** def encode(im, secret): mySecret = im.copy() width = im.getWidth() height = im.getHeight() for row in range(0, height, 197): for col in range(width): t = 0 for i in range(1, 22, 1): (r, g, b) = im.getPixel2D(col, row) letter = secret[t] mySecret.setPixel2D(col, row, (ord(letter), g, b)) t = t + 1 mySecret.save("mysecret.png") def decode(im,imSecret): width = im.getWidth() height = im.getHeight() x = "" y = "" z = "" for col in range(width): for row in range(height): (r, g, b) = im.getPixel2D(col, row) (r1, g1, b1) = imSecret.getPixel2D(col, row) if r != r1: x = x + chr(r1) if g != g1: y = y + chr(g1) if b != b1: z = z + chr(b1) return x return y return z # *** The main part of the program *** # Create a FileImage object from a specified image file original = FileImage("nature_trail_2.png") secret = "This is a secret code!" encode1 = encode(original, secret) # Create FileImage objects for the secret message files secret1 = FileImage("secret.png") secret2 = FileImage("secret2.png") secret3 = FileImage("secret3.png") secret4 = FileImage("mysecret.png") #secret = raw_input("What do you want your secret to be: ") # Call your function here # *** YOU FILL THIS IN *** message1 = decode(original, secret1) message2 = decode(original, secret2) message3 = decode(original, secret3) message4 = decode(original, secret4) # Determine if there was a secret message inside the file. If so, print out # the message. If not, print out the statement "this file is clean." if message1 == "": print "This file is clean." else: print message1 if message2 == "": print "This file is clean." else: print message2 if message3 == "": print "This file is clean." else: print message3 if message4 == "": print "This file is clean." else: print message4