http://war.163.com/10/0712/17/6BDLNUB90001123L.html

1.每张图片都有RGB三个通道，每个像素点的值为3个8位的R/G/B值。

2.把每个RGB值的最后两位&11111100(0xFC)移掉，因为最后两位对图片的影响不大，肉眼基本不可见

3.把要隐藏的图片的RGB值都除以85(255/3=85)，这样正好可以通过2bits(00-11,0-3)来表示图片

4.把得到的值填到之前的2个空bit中

5.等解码的时候只要把之前在原图中藏进去的2位提取出来(&0x3)再＊85就可以得到原图信息

import Image,ImageChops

def waterMark(originFile,markFile):
origin = Image.open(originFile)
mark = Image.open(markFile)
size = mark.size #record the original size of the marked image
mark = mark.resize(origin.size) #set the pics in same size

source1=origin.split() #split the pic in 3 channel
source2=mark.split()

image=[(),(),()]
waterMark=[(),(),()]
for x in [0,1,2]: #loop in 3 channel, RGB
image[x]=source1[x].point(lambda i:i & 0xFC) #remove the last 2 bits of the original image, 0xFC=11111100
waterMark[x]=source2[x].point(lambda i:i/85) #reduce the RGB value of each channel, 255/3=85

mark=Image.merge("RGB",waterMark)
origin=Image.merge("RGB",image)

return result,size

def deCode(waterMark,size):
#waterMark=Image.open(waterMark)
source = waterMark.split()
originSize=size
#print originSize

mark=[(),(),()]
for x in [0,1,2]:
mark[x]=source[x].point(lambda i:(i & 0x3)*85) #get back the RGB value of the marked image

result=Image.merge("RGB",mark)
result = result.resize(originSize)

return result

if __name__ == '__main__':
img1,size = waterMark("avatar2.jpg","green.jpg")
img2 = deCode(img1,size)

img1.show() #original image with watermark
img2.show() #embeded image