如何使用 Python 隱藏圖像中的數(shù)據(jù)

隱寫術是在任何文件中隱藏秘密數(shù)據(jù)的藝術。
秘密數(shù)據(jù)可以是任何格式的數(shù)據(jù),如文本甚至文件。簡而言之,隱寫術的主要目的是隱藏任何文件(通常是圖像、音頻或視頻)中的預期信息,而不實際改變文件的外觀,即文件外觀看起來和以前一樣。
在這篇文章中,我們將重點學習基于圖像的隱寫術,即在圖像中隱藏秘密數(shù)據(jù)。
但在深入研究之前,讓我們先看看圖像由什么組成:
像素是圖像的組成部分。
每個像素包含三個值:(紅色、綠色、藍色)也稱為 RGB 值。
每個 RGB 值的范圍從 0 到 255。
現(xiàn)在,讓我們看看如何將數(shù)據(jù)編碼和解碼到我們的圖像中。
有很多算法可以用來將數(shù)據(jù)編碼到圖像中,實際上我們也可以自己制作一個。在這篇文章中使用的一個很容易理解和實現(xiàn)的算法。
算法如下:
對于數(shù)據(jù)中的每個字符,將其 ASCII 值轉(zhuǎn)換為 8 位二進制 [1]。
一次讀取三個像素,其總 RGB 值為 3*3=9 個。前八個 RGB 值用于存儲一個轉(zhuǎn)換為 8 位二進制的字符。
比較相應的RGB值和二進制數(shù)據(jù)。如果二進制數(shù)字為 1,則 RGB 值將轉(zhuǎn)換為奇數(shù),否則為偶數(shù)。
第 9 個值確定是否應該讀取更多像素。如果有更多數(shù)據(jù)要讀取,即編碼或解碼,則第 9 個像素變?yōu)榕紨?shù);否則,如果我們想停止進一步讀取像素,那就讓它變得奇數(shù)。
重復這個過程,直到所有數(shù)據(jù)都被編碼到圖像中。
假設要隱藏的消息是‘Hii’。
消息是三個字節(jié),因此,對數(shù)據(jù)進行編碼所需的像素為 3 x 3 = 9。考慮一個 4 x 3 的圖像,總共有 12 個像素,這足以對給定的數(shù)據(jù)進行編碼。
[(27, 64, 164), (248, 244, 194), (174, 246, 250), (149, 95, 232),(188, 156, 169), (71, 167, 127), (132, 173, 97), (113, 69, 206),(255, 29, 213), (53, 153, 220), (246, 225, 229), (142, 82, 175)]
H 的 ASCII 值為 72 ,其二進制等效值為 01001000 。
讀取前三個像素。
(27, 64, 164), (248, 244, 194), (174, 246, 250)現(xiàn)在,將像素值更改為奇數(shù)為 1,偶數(shù)為 0,就像在二進制等效數(shù)據(jù)中一樣。
例如,第一個二進制數(shù)字是0,第一個 RGB 值是 27 ,它需要轉(zhuǎn)換為偶數(shù),這意味著 26 。類似地,64 被轉(zhuǎn)換為 63 因為下一個二進制數(shù)字是1 所以 RGB 值應該是奇數(shù)。
因此,修改后的像素為:
(26, 63, 164), (248, 243, 194), (174, 246, 250)由于我們必須對更多數(shù)據(jù)進行編碼,因此最后一個值應該是偶數(shù)。同樣,i可以在這個圖像中進行編碼。
通過執(zhí)行 +1 或 -1 使像素值成為奇數(shù)/偶數(shù)時,我們應該注意二進制條件。即像素值應大于或等于 0 且小于或等于 255 。
新圖像將如下所示:
[(26, 63, 164), (248, 243, 194), (174, 246, 250), (148, 95, 231),(188, 155, 168), (70, 167, 126), (132, 173, 97), (112, 69, 206),(254, 29, 213), (53, 153, 220), (246, 225, 229), (142, 82, 175)]
對于解碼,我們將嘗試找到如何逆轉(zhuǎn)之前我們用于數(shù)據(jù)編碼的算法。
同樣,一次讀取三個像素。前 8 個 RGB 值為我們提供了有關機密數(shù)據(jù)的信息,第 9 個值告訴我們是否繼續(xù)前進。
對于前八個值,如果值為奇數(shù),則二進制位為 1 ,否則為 0 。
這些位連接成一個字符串,每三個像素,我們得到一個字節(jié)的秘密數(shù)據(jù),這意味著一個字符。
現(xiàn)在,如果第 9 個值是偶數(shù),那么我們繼續(xù)一次讀取三個像素,否則,我們停止。
讓我們開始一次讀取三個像素。
考慮我們之前編碼的圖像。
[(26, 63, 164), (248, 243, 194), (174, 246, 250), (148, 95, 231),(188, 155, 168), (70, 167, 126), (132, 173, 97), (112, 69, 206),(254, 29, 213), (53, 153, 220), (246, 225, 229), (142, 82, 175)]
我們首先讀取三個像素:
[(26, 63, 164), (248, 243, 194), (174, 246, 250)讀取第一個值:26,它是偶數(shù),因此二進制位是 0 。類似地,對于 63 ,二進制位是 1 ,對于 164 它是 0 。這個過程一直持續(xù)到 8 個 RGB 值。
將所有二進制值連接后,我們最終得到二進制值:01001000。最終的二進制數(shù)據(jù)對應于十進制值 72,在 ASCII 中,它代表字符 H 。
由于第 9 個值是偶數(shù),我們重復上述步驟。當遇到的第 9 個值是奇數(shù)時,我們停止。
結(jié)果,我們得到了原始信息,即 Hii 。
上述算法的 Python 程序如下:
# Python program implementing Image Steganography# PIL module is used to extract# pixels of image and modify itfrom PIL import Image# Convert encoding data into 8-bit binary# form using ASCII value of charactersdef genData(data):# list of binary codes# of given datanewd = []for i in data:newd.append(format(ord(i), '08b'))return newd# Pixels are modified according to the# 8-bit binary data and finally returneddef modPix(pix, data):datalist = genData(data)lendata = len(datalist)imdata = iter(pix)for i in range(lendata):# Extracting 3 pixels at a timepix = [value for value in imdata.__next__()[:3] +imdata.__next__()[:3] +imdata.__next__()[:3]]# Pixel value should be made# odd for 1 and even for 0for j in range(0, 8):if (datalist[i][j] == '0' and pix[j]% 2 != 0):pix[j] -= 1elif (datalist[i][j] == '1' and pix[j] % 2 == 0):if(pix[j] != 0):pix[j] -= 1else:pix[j] += 1# pix[j] -= 1# Eighth pixel of every set tells# whether to stop ot read further.# 0 means keep reading; 1 means thec# message is over.if (i == lendata - 1):if (pix[-1] % 2 == 0):if(pix[-1] != 0):pix[-1] -= 1else:pix[-1] += 1else:if (pix[-1] % 2 != 0):pix[-1] -= 1pix = tuple(pix)yield pix[0:3]yield pix[3:6]yield pix[6:9]def encode_enc(newimg, data):w = newimg.size[0](x, y) = (0, 0)for pixel in modPix(newimg.getdata(), data):# Putting modified pixels in the new imagenewimg.putpixel((x, y), pixel)if (x == w - 1):x = 0y += 1else:x += 1# Encode data into imagedef encode():img = input("Enter image name(with extension) : ")image = Image.open(img, 'r')data = input("Enter data to be encoded : ")if (len(data) == 0):raise ValueError('Data is empty')newimg = image.copy()encode_enc(newimg, data)new_img_name = input("Enter the name of new image(with extension) : ")newimg.save(new_img_name, str(new_img_name.split(".")[1].upper()))# Decode the data in the imagedef decode():img = input("Enter image name(with extension) : ")image = Image.open(img, 'r')data = ''imgdata = iter(image.getdata())while (True):pixels = [value for value in imgdata.__next__()[:3] +imgdata.__next__()[:3] +imgdata.__next__()[:3]]# string of binary databinstr = ''for i in pixels[:8]:if (i % 2 == 0):binstr += '0'else:binstr += '1'data += chr(int(binstr, 2))if (pixels[-1] % 2 != 0):return data# Main Functiondef main():a = int(input(":: Welcome to Steganography ::\n""1. Encode\n2. Decode\n"))if (a == 1):encode()elif (a == 2):print("Decoded Word : " + decode())else:raise Exception("Enter correct input")# Driver Codeif __name__ == '__main__' :# Calling main functionmain()
程序中使用的模塊是 PIL ,它代表Python 圖像庫,它使我們能夠在 Python 中對圖像執(zhí)行操作。

數(shù)據(jù)編碼

數(shù)據(jù)解碼
輸入圖像

輸出圖像

局限性
該程序可能無法對 JPEG 圖像按預期處理,因為 JPEG 使用有損壓縮,這意味著修改像素以壓縮圖像并降低質(zhì)量,因此會發(fā)生數(shù)據(jù)丟失。
https://www.geeksforgeeks.org/program-decimal-binary-conversion/
https://www.geeksforgeeks.org/working-images-python/
https://dev.to/erikwhiting88/let-s-hide-a-secret-message-in-an-image-with-python-and-opencv-1jf5
A code along with the dependencies can be found here:?https://github.com/goelashwin36/image-steganography
END
各位伙伴們好,詹帥本帥搭建了一個個人博客和小程序,匯集各種干貨和資源,也方便大家閱讀,感興趣的小伙伴請移步小程序體驗一下哦!(歡迎提建議)
推薦閱讀
牛逼!Python常用數(shù)據(jù)類型的基本操作(長文系列第①篇)
牛逼!Python的判斷、循環(huán)和各種表達式(長文系列第②篇)
