1 回答

TA貢獻1848條經(jīng)驗 獲得超6個贊
這是一個XY 問題。您不需要將像素轉(zhuǎn)換為二進制或使用顯式方法提取任何特定位,因為這樣您就必須再次將其全部拼接起來。你可以直接用按位運算做你想做的事,因為“二進制”和十進制是同一個數(shù)字的兩種表示。ANDs 和SHIFTs 的組合將允許您將整數(shù)的任何部分歸零,或隔離特定范圍的位
例如,
>> (107 >> 3) & 7
5
因為
Decimal: 107 >> 3 = 13 & 7 = 5
Binary : 01101011 >> 3 = 00001101 & 00000111 = 00000101
|-| |-|
we want
these 3
現(xiàn)在,假設(shè)您的信息是世界“你好”。您可以像這樣方便地將每個字節(jié)分成四部分。
secret = b'hello'
bits = []
for byte in secret:
for i in range(6, -1, -2):
bits.append((byte >> i) & 3)
bits = np.array(bits)
由于每個bits元素包含兩位,因此值的范圍可以在 0 到 3 之間。如果您考慮二進制中的字母 'h',即 '01|10|10|00',您可以看到它的前幾個值是bits如何1、2、2、0 等
為了利用 numpy 中的矢量化操作,我們應(yīng)該展平我們的圖像數(shù)組,我假設(shè)它的形狀為 (height, width, 3)。
np.random.seed(0)
img = np.random.randint(0, 255, (1600, 1200, 3)).astype(np.uint8)
shape = img.shape
# this specific type of flattening puts the pixels in your desired order, i.e.,
# pixel (0, 0) red-green-blue, pixel (0, 1) red-green-blue, etc
flat_img = img.reshape(-1).copy()
現(xiàn)在嵌入很簡單
length = len(bits)
flat_img[:length] = (flat_img[:length] & 252) + bits
stego_img = flat_img.reshape(shape)
添加回答
舉報