【发布时间】:2017-02-21 04:08:58
【问题描述】:
在学习 numpy 的过程中,我编写了 LSB(隐写术)加密的代码:
def str2bits_nparray(s):
return np.array(map(int, (''.join(map('{:07b}'.format, bytearray(s))))), dtype=np.bool)
def LSB_encode(img, msg, channel):
msg_bits = str2bits_nparray(msg)
xor_mask = np.zeros_like(img, dtype=np.bool)
xor_mask[:, :, channel].flat[:len(msg_bits)] = np.ones_like(msg_bits, dtype=np.bool)
img[xor_mask] = img[xor_mask] >> 1 << 1 | msg_bits
msg = 'A' * 1000
img_name = 'screenshot.png'
chnl = 2
img = imread(img_name)
LSB_encode(img, msg, chnl)
代码工作正常,但是当我尝试制作chnl = [2, 1] 时:
xor_mask[:, :, channel].flat[:len(msg_bits)] = np.ones_like(msg_bits, dtype=np.bool)
不使用
为xor_mask 赋值
xor_mask[:, :,[2, 1]].flat[:len(msg_bits)]
有没有办法解决这个问题?
我尝试了通过通道进行 for 循环的解决方案:
for ch in channel:
xor_mask[:, :, ch].flat[:len(msg_bits)] = np.ones_like(msg_bits, dtype=np.bool)
但这不是我想要的
xor_mask[:, :,[2, 1]].flat[:len(msg_bits)] = np.ones_like(msg_bits, dtype=np.bool)
【问题讨论】:
标签: python arrays python-2.7 numpy slice