Encode the English string information into the picture, and decode the information

First, define some important functions

1. Encoding function: encode a paragraph into a binary number and store it in the list. Returns a list of binary numbers and the number of rows in this list and the number of columns corresponding to each row.

def encode(s):      #编码函数
    n=len(s)
    l=list()       #编码数组
    num=list()
    k=0      #记录数组中字符的个数
    for i in range(n):
        l.append([])
        num.append(len(s[i]))
        for j in range(len(s[i])):
            l[i].append(bin(ord(s[i][j])).replace('0b',''))
    return l,num

2. Decoding function: Decode the list of stored binary numbers into a paragraph. Returns the decoded string.

def decode(l):      #解码函数
    s=list()
    for i in range(len(l)):
        s.append([])
        s[i]=''.join(chr(k) for k in [int(j,2) for j in l[i]])
    return s

3. Information hiding function: Given a picture and a list of binary numbers, store each binary number in the list of binary numbers in the last digit of the binary number corresponding to the picture pixel. A pixel can store 3 bits and a 7-bit number. Binary needs 3 pixels to store. Return the picture with hidden information.

def hide(pic,en):      #将已解码的字符串中的信息隐藏进图片
    pic=Image.open(pic).convert('RGB')      #将4通道RGBA像素转化为3通道RGB像素
    np=numpy.array(pic)
    long,width,dim=np.shape
    pic_array=np
    m=0      #记录赋值次数
    for i in range(len(en)):
        for j in range(len(en[i])*3):
            for k in range(dim):
                s=bin(pic_array[i][j][k]).replace('0b','')
                pic_array[i][j][k]=int(s[:-2]+en[i][j//3][m])
                m=(m+1)%7
                if m==0:
                    break
    pic.close()
    return Image.fromarray(pic_array)

4. Information extraction function: For a picture and a list of record strings, extract the last bit of the binary number corresponding to the pixel at the specified position, and splice it into a complete binary number according to the recorded information. , Returns the decoded information of the binary number list.

def extract(pic,num):
    l=list()
    np=numpy.array(pic)
    m=0
    for i in range(len(num)):
        l.append([])
        for j in range(num[i]*3):
            if j%3==0:
                l[i].append([])
            for k in range(3):      #dim=3
                s=bin(np[i][j][k]).replace('0b','')
                l[i][j//3]+=s[-1:]
                m=(m+1)%7
                if m == 0:
                    l[i][j//3]=''.join(l[i][j//3])
                    break
    return decode(l)

 

 

With the above functions, this problem has basically been solved, the interactive part of the program:

sentence=list(map(str,input('请输入一个句子:').split()))
en,num=encode(sentence)
print('编码后的字符串为:',en,num)
pic=hide('tree',en)
pic.show('隐藏信息的图片:')
print(' '.join(extract(pic,num)))

enter:

unknown person loves cs

Output:

请输入一个句子:unkonwn person loves cs
编码后的字符串为: [['1110101', '1101110', '1101011', '1101111', '1101110', '1110111', '1101110'], ['1110000', '1100101', '1110010', '1110011', '1101111', '1101110'], ['1101100', '1101111', '1110110', '1100101', '1110011'], ['1100011', '1110011']] [7, 6, 5, 2]
unkonwn person loves cs

 

Guess you like

Origin blog.csdn.net/jccc39/article/details/106188173