我正在尝试使用Python PIL为B& W图像编码基本的隐写算法。
使用示例图像我可以成功提取其中的隐藏图像,并隐藏其他图像以便随后提取它们。问题在于隐藏文本然后将其解压缩。
from PIL import Image
import matplotlib.pyplot as plt
import scipy.misc as sci
import numpy as np
import array
#CONVERTS IMAGE TO ARRAY OF BINARY 8-BIT NUMBER#
def getImgArray(image):
w,h = image.size
out = []
for x in range(w):
for y in range(h):
pixel = image.getpixel((y,x))
pixel = format(pixel, '08b')
out.append(pixel)
return out
def stringByteConverter(data, mode):
if (mode == "stringToByte"):
aux = map(ord,data.encode('utf8'))
aux = [format(char,'08b') for char in aux]
return aux
elif (mode == "byteToString"):
aux = [int(item,2) for item in data]
aux = "".join(map(chr, aux))
return aux
else:
print("Invalid mode. Use 'stringToByte' or 'byteToString'")
#GETS HIDDEN IMAGE AND RETURNS IT AS BYTE ARRAY REPRESENTING PIXELS#
def getHiddenImage(image):
buf = ""
width,height = image.size
img_aux = []
for x in range(width):
for y in range(height):
if(len(buf)<8):
pixel = image.getpixel((y,x))
pixel = format(pixel,'08b')
buf += pixel[-2:]
else:
img_aux.append(buf)
buf = ""
pixel = image.getpixel((y,x))
pixel = format(pixel,'08b')
buf += pixel[-2:]
return img_aux
#CONVERT ARRAY OF BYTES TO PNG IMG AND RETURNS PIL IMG OBJECT#
def saveImgArr(ImgArr, size, outputName):
pixels = np.empty(size)
iterator = 0
for i in range(size[0]):
for j in range(size[1]):
try:
pixels[i][j] = int(ImgArr[iterator],2)
iterator += 1
except IndexError:
break
aux = Image.fromarray(pixels)
aux = aux.convert("L")
aux.save(outputName+'.png', 'PNG')
return pixels
#HIDE IMAGE <src> IN OTHER IMAGE <img>#
def hideImg(src, img, output):
iterator = 0
src = src.convert("L")
srcArr = getImgArray(src)
imgArr = getImgArray(img)
for i in range(len(srcArr)):
buf = []
buf.append(srcArr[i][:2])
buf.append(srcArr[i][2:4])
buf.append(srcArr[i][4:6])
buf.append(srcArr[i][6:])
for j in range(4):
imgArr[iterator] = imgArr[iterator][:-2] + buf[j]
iterator += 1
saveImgArr(imgArr,img.size,output)
#HIDE STRING INSIDE IMG#
def hideText(img, string, outputName):
imgArr = getImgArray(img)
stringBytes = stringByteConverter(string, "stringToByte")
iterator = 0
for i in range(len(string)):
buf = []
buf.append(stringBytes[i][:2])
buf.append(stringBytes[i][2:4])
buf.append(stringBytes[i][4:6])
buf.append(stringBytes[i][6:])
for j in range(4):
imgArr[iterator] = imgArr[iterator][:-2] + '00'
imgArr[iterator] = imgArr[iterator][:-2] + buf[j]
iterator += 1
print(imgArr[:len(string)*4]) #test print
saveImgArr(imgArr,img.size,outputName)
temp = Image.open(outputName+'.png')
tempArr = getImgArray(temp)
print(tempArr[:len(string)*4]) #test print
def getHiddenText(img, msgSize):
buf = ''
width,height = img.size
output = []
counter = 0
for x in range(width):
for y in range(height):
if(counter < msgSize*4):
pixel = img.getpixel((y,x))
pixel = format(pixel,'08b')
buf += pixel[-2:]
counter += 1
output = stringByteConverter(buf, "byteToString")
return output
通过在hideText()函数中打印数据数组,我能够获得以下内容:
hideText(lena,'test',"lena_hidden_text")
['10100001','10100011','10100001','10100000','10100001', '10011110','10100001','10100001','10100101','10100011', '10100000','10011111','10011001','10100011','10011101', '10011000']
['10011110','10100000','10011110','10011101','10011110', '10011011','10011110','10011110','10100011','10100000', '10011101','10011100','10010101','10100000','10011001', '10010100']
hideText()调用获得的第一个向量与它应该完全一样,但在使用saveImgArr()保存图像并使用getImgArr()重新加载后,返回第二个向量,它完全不同。
我不能为我的生活找到问题。由于使用图像提取隐藏数据或隐藏数据,这些功能都非常有效。
我只能猜测我在某种程度上处理错误的文本字节。任何见解都将不胜感激。
答案 0 :(得分:0)
看起来可疑的一件事是saveImgArr:
aux = Image.fromarray(pixels)
aux = aux.convert("L")
要使用的mode的默认Image.fromarray是从输入的数据类型中推断出来的。
在您的情况下,输入的数据类型是numpy的默认数据类型(浮点数),因此Image将基于浮点数构建。因此,我会预测保存的png图像看起来不正确(只是一个空白图像,因为每个像素都会饱和到1.0)。
要纠正这个问题,您可以提供正确的数据类型来numpy,即更改:
pixels = np.empty(size)
到
pixels = np.empty(size,dtype='uint8')
或通过更改:
将模式显式提供给Image.fromarrayaux = Image.fromarray(pixels)
aux = aux.convert("L")
到
aux = Image.fromarray(pixels,mode='L')