LSB水印嵌入算法
from PIL import Image
# 补足8位
def plus(_str):
        return _str.zfill(8)
#获取水印图片的每个像素值的rbg并将其转换为二进制形式,然后合并为字符串输出
def get_maskRGB():
    mask=Image.open('C:\\Users\\14486\\Desktop\\测试文件\\左\\watermask.png')
    mask=mask.convert('RGB')
    _str=""
    #获取水印的每个像素值
    for i in range(mask.size[0]):
        for j in range(mask.size[1]):
            rgb=mask.getpixel((i,j))#rgb值是用三元组保存的
            _str+=plus(bin(rgb[0]).replace('0b',''))  #bin()将十进制转化为二进制,并在转换的数字前加‘0b’   plus()补足到8位
            _str+=plus(bin(rgb[1]).replace('0b',''))
            _str+=plus(bin(rgb[2]).replace('0b',''))
    return _str
def LBS_Encode(im,_str):
    #im = Image.open('C:\\Users\\14486\\Desktop\\测试文件\\左\\ImgDemo.png')
    count=0
    _strlen=len(_str)
    for i in range(im.size[0]):
        for j in range(im.size[1]):
            rgb=im.getpixel((i,j))
            if count==_strlen:
                break
            r=rgb[0]
            g=rgb[1]
            b=rgb[2]
            r=(r-r%2)+int(_str[count])
            count+=1
            if count==_strlen:
                im.putpixel((i,j),(r,g,b))
                break
            g=(g-g%2)+int(_str[count])
            count+=1
            if count==_strlen:
                im.putpixel((i,j),(r,g,b))
                break
            b=(b-b%2)+int(_str[count])
            count+=1
            if count==_strlen:
                im.putpixel((i,j),(r,g,b))
                break
            if count%3==0:
                im.putpixel((i, j), (r, g, b))
    im.save('C:\\Users\\14486\\Desktop\\测试文件\\左\\嵌入水印后的ImgDemo.png')
#para1需要提取水印的图片,para2水印图片的二进制字符串的长度
def LBS_Decode(im,length):
    #im=Image.open('C:\\Users\\14486\\Desktop\\测试文件\\左\\ImgDemo.png')
    #mask=Image.open('C:\\Users\\14486\\Desktop\\测试文件\\左\\watermask.png')
    width=im.size[0]
    height=im.size[1]
    #计数器
    count=0
    wt=""
    for i in range(width):
        for j in range(height):
            rgb=im.getpixel((i,j))
            #依次获取rgb并计数
            if count%3==0:
                count+=1
                wt+=str(rgb[0]%2)  #对十进制取余可得二进制最有一位
                if count==length:
                    break
            if count%3==1:
                count+=1
                wt+=str(rgb[1]%2)
                if count==length:
                    break
            if count%3==2:
                count+=1
                wt+=str(rgb[2]%2)
                if count==length:
                    break
            if count == length:
                break
    return wt
#将其还原为rgb值
def showImg(wt):
    list=[]
    for i in range(0,len(wt),8):
        list.append(int(wt[i:i+8],2))#int(,base=2)  将二进制转换为十进制
    #绘制水印图片
    img_out=Image.new("RGB",(50,50))
    flag=0
    for m in range(0,50):
        for n in range(0,50):
            img_out.putpixel((m,n),(list[flag],list[flag+1],list[flag+2]))
            flag+=3
    img_out.show()
if __name__ == '__main__':
    _str = get_maskRGB()
    im = Image.open('C:\\Users\\14486\\Desktop\\测试文件\\左\\嵌入水印后的ImgDemo.png')
    #LBS_Encode(im,_str)
    wt= LBS_Decode(im,length=len(_str))
    showImg(wt)测试图片:

水印图片:

嵌入水印后的图片:


 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号