我正在编写一个Python脚本来隐藏图像中的数据。它基本上隐藏了.PNG中每个像素的RGB图中红色的最后两位中的位。该脚本适用于小写字母,但产生一个完整的错误。它产生了这个错误:
Traceback(最近一次调用最后一次):文件 " E:\ Python \ Steganography \ main.py",第65行,in 打印(取消隐藏(' coded-img.png'))文件" E:\ Python \ Steganography \ main.py",第60行,取消隐藏 message = bin2str(二进制)文件" E:\ Python \ Steganography \ main.py",第16行,在bin2str中 return n.to_bytes((n.bit_length()+ 7)// 8,' big')。decode()UnicodeDecodeError:' utf-8'编解码器不能将字节0x80解码到位 6:无效的起始字节
这是我的代码:
from PIL import Image
def str2bin(message):
binary = bin(int.from_bytes(message.encode('utf-8'), 'big'))
return binary[2:]
def bin2str(binary):
n = int(binary, 2)
return n.to_bytes((n.bit_length() + 7) // 8, 'big').decode()
def hide(filename, message):
image = Image.open(filename)
binary = str2bin(message) + '00000000'
data = list(image.getdata())
newData = []
index = 0
for pixel in data:
if index < len(binary):
pixel = list(pixel)
pixel[0] >>= 2
pixel[0] <<= 2
pixel[0] += int('0b' + binary[index:index+2], 2)
pixel = tuple(pixel)
index += 2
newData.append(pixel)
print(binary)
image.putdata(newData)
image.save('coded-'+filename, 'PNG')
def unhide(filename):
image = Image.open(filename)
data = image.getdata()
binary = '0'
index = 0
while binary[-8:] != '00000000':
binary += bin(data[index][0])[-2:]
index += 1
binary = binary[:-1]
print(binary)
print(index*2)
message = bin2str(binary)
return message
hide('img.png', 'alpha.')
print(unhide('coded-img.png'))
请帮忙。谢谢!
答案 0 :(得分:2)
您的代码至少存在两个问题。
第一个问题是您的编码可能会错位1位,因为bin()
函数的输出中不包含前导空位:
>>> bin(int.from_bytes('a'.encode('utf-8'), 'big'))[2:]
'1100001'
# This string is of odd length and (when joined with the terminating '00000000')
# turns into a still odd-length '110000100000000' which is then handled by your
# code as if there was an extra trailing zero (so that the length is even).
# During decoding you compensate for that effect with the
#
# binary = binary[:-1]
#
# line. The latter is responsible for your stated problem when the binary
# representation of your string is in fact of even length and doesn't need
# the extra bit as in the below example:
>>> bin(int.from_bytes('.'.encode('utf-8'), 'big'))[2:]
'101110'
通过预先添加一个额外的空位(如果需要),你最好将二进制字符串补充到偶数长度。
另一个问题是,在恢复隐藏消息时,通过将一个(部分恢复的)符号的前导位连接到另一个符号的尾随位,可以过早地满足停止条件binary[-8:] == '00000000'
。例如,在以下情况下可能会发生这种情况
符号 @ (ASCII码= 64,即6个低位未设置),后跟任何ASCII码值小于64的字符(即未设置2个最高位的字符) );
空格字符(ASCII码= 32,即4个低位未设置),后跟换行符/换行符(ASCII码= 10,即未设置4个高位)。
< / LI>您可以通过要求在最后8位看起来全部未设置时解码完整字节来修复该错误:
while not (len(binary) % 8 == 0 and binary[-8:] == '00000000'):
# ...