Created
September 21, 2012 20:54
-
-
Save sash13/3763835 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Image | |
| def getBit(num, pos): | |
| return (num & (1 << pos)) >> pos | |
| def numToBit(inN): | |
| out = [] | |
| for i in xrange(8): | |
| out.append(getBit(inN,i)) | |
| return out | |
| def bitToNum(in_array): | |
| out =0 | |
| for i in xrange(len(in_array)): | |
| if in_array[i]: | |
| out+=pow(2,i) | |
| return out | |
| def loadImg(file_name): | |
| out = Image.open(file_name).convert('RGB') | |
| pixels = out.load() | |
| return out, pixels, out.size | |
| def writeChar(pixs, cursor, char): | |
| char = numToBit(ord(char)) | |
| r = numToBit(pixs[cursor[0],cursor[1]][0]) | |
| g = numToBit(pixs[cursor[0],cursor[1]][1]) | |
| b = numToBit(pixs[cursor[0],cursor[1]][2]) | |
| r[0] = char[0] | |
| r[1] = char[1] | |
| g[0] = char[2] | |
| g[1] = char[3] | |
| g[2] = char[4] | |
| b[0] = char[5] | |
| b[1] = char[6] | |
| b[2] = char[7] | |
| r = bitToNum(r) | |
| g = bitToNum(g) | |
| b = bitToNum(b) | |
| updatedPix = (r, g, b) | |
| pixs[cursor[0],cursor[1]] = (r, g, b) | |
| def readChar(pixs, cursor): | |
| char_array = [] | |
| r = numToBit(pixs[cursor[0],cursor[1]][0]) | |
| g = numToBit(pixs[cursor[0],cursor[1]][1]) | |
| b = numToBit(pixs[cursor[0],cursor[1]][2]) | |
| char_array.append(r[0]) | |
| char_array.append(r[1]) | |
| char_array.append(g[0]) | |
| char_array.append(g[1]) | |
| char_array.append(g[2]) | |
| char_array.append(b[0]) | |
| char_array.append(b[1]) | |
| char_array.append(b[2]) | |
| return chr(bitToNum(char_array)) | |
| def isEncript(pixs): | |
| if readChar(pixs, (0,0)) == '/': | |
| return 1 | |
| else: | |
| return 0 | |
| def encode(in_image, in_text): | |
| img, pixels, size = loadImg(in_image) | |
| len_text = len(in_text) | |
| if isEncript(pixels): | |
| return 1 | |
| if len_text > (size[0]*size[1]-3): | |
| return 2 | |
| writeChar(pixels, (0,0), '/') | |
| for i in xrange(3): | |
| writeChar(pixels, (0,i+1), chr(len_text)) | |
| k=0 | |
| for i in range(3,size[1]-3): | |
| for j in range(size[0]): | |
| if k < len_text: | |
| print in_text[k] | |
| writeChar(pixels, (j,i), in_text[k]) | |
| k+=1 | |
| return img | |
| def decode(in_image): | |
| pixs = in_image.load() | |
| size = in_image.size | |
| if (not isEncript(pixs)): | |
| return 'Error' | |
| out='' | |
| max_len=ord(readChar(pixs, (0,1))) | |
| for i in range(3,size[1]-3): | |
| for j in range(size[0]): | |
| if len(out) >= max_len: | |
| break | |
| out+=readChar(pixs, (j,i)) | |
| return out | |
| img = encode('in.png', 'Aeyzrfr gsdfgsd dgs ///') | |
| img.show() | |
| print decode(img) | |
| img.save('out.png') | |
| ''' | |
| print numToBit(40) | |
| print bitToNum(numToBit(40))''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment