While learning numpy, I wrote code which doing LSB(steganography) encryption:
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)
Code works fine, but when i'm trying to made chnl = [2, 1] this line:
xor_mask[:, :, channel].flat[:len(msg_bits)] = np.ones_like(msg_bits, dtype=np.bool)
doesnt assign value to xor_mask with
xor_mask[:, :,[2, 1]].flat[:len(msg_bits)]
Is there way to fix this?
I tryed solution with for-loop over channels:
for ch in channel:
xor_mask[:, :, ch].flat[:len(msg_bits)] = np.ones_like(msg_bits, dtype=np.bool)
But this is doing not that i want from
xor_mask[:, :,[2, 1]].flat[:len(msg_bits)] = np.ones_like(msg_bits, dtype=np.bool)