Python3 圖片隱寫
就是把圖片rgb值修改后輸出一張新的圖,肉眼看不出區(qū)別烈评,像11111111和11111110這兩個(gè)值所表示的藍(lán)色火俄,人眼幾乎無法區(qū)分。這個(gè)最低有效位就可以用來存儲顏色之外的信息讲冠,而且在某種程度上幾乎是檢測不到的瓜客。
code.png
結(jié)合了上次的圖片轉(zhuǎn)字符畫實(shí)驗(yàn)里頭用到的argparse 庫
https://www.shiyanlou.com/courses/370/labs/1191/document
其實(shí)不懂具體怎么調(diào)用模塊,模仿著argparse庫把這個(gè)圖片隱寫改成encode.py和decode.py兩份
然后運(yùn)行encode.py就可以對指定圖片藏進(jìn)密碼竿开,生成新圖code.png谱仪。使用方法:
$python3 encode.py 圖片名字.png 你個(gè)小垃圾
然后就會得到一張 code.png,用decode.py解碼得到隱藏信息:
$python3 decode.py code.png
你個(gè)小垃圾
encode.py 編碼
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from PIL import Image
import argparse
#命令行輸入?yún)?shù)處理
parser = argparse.ArgumentParser()
parser.add_argument('file') #輸入文件
parser.add_argument('text') #輸入字符串
#獲取參數(shù)
args = parser.parse_args()
IMG = args.file
TEXT = args.text
"""
取得一個(gè) PIL 圖像并且更改所有值為偶數(shù)(使最低有效位為 0)
"""
def makeImageEven(image):
pixels = list(image.getdata()) # 得到一個(gè)這樣的列表: [(r,g,b,t),(r,g,b,t)...]
evenPixels = [(r>>1<<1,g>>1<<1,b>>1<<1,t>>1<<1) for [r,g,b,t] in pixels] # 更改所有值為偶數(shù)(魔法般的移位)
evenImage = Image.new(image.mode, image.size) # 創(chuàng)建一個(gè)相同大小的圖片副本
evenImage.putdata(evenPixels) # 把上面的像素放入到圖片副本
return evenImage
"""
內(nèi)置函數(shù) bin() 的替代否彩,返回固定長度的二進(jìn)制字符串
"""
def constLenBin(int):
binary = "0"*(8-(len(bin(int))-2))+bin(int).replace('0b','') # 去掉 bin() 返回的二進(jìn)制字符串中的 '0b'疯攒,并在左邊補(bǔ)足 '0' 直到字符串長度為 8
return binary
"""
將字符串編碼到圖片中
"""
def encodeDataInImage(image, data):
evenImage = makeImageEven(image) # 獲得最低有效位為 0 的圖片副本
binary = ''.join(map(constLenBin,bytearray(data, 'utf-8'))) # 將需要被隱藏的字符串轉(zhuǎn)換成二進(jìn)制字符串
if len(binary) > len(image.getdata()) * 4: # 如果不可能編碼全部數(shù)據(jù), 拋出異常
raise Exception("Error: Can't encode more than " + len(evenImage.getdata()) * 4 + " bits in this image. ")
encodedPixels = [(r+int(binary[index*4+0]),g+int(binary[index*4+1]),b+int(binary[index*4+2]),t+int(binary[index*4+3])) if index*4 < len(binary) else (r,g,b,t) for index,(r,g,b,t) in enumerate(list(evenImage.getdata()))] # 將 binary 中的二進(jìn)制字符串信息編碼進(jìn)像素里
encodedImage = Image.new(evenImage.mode, evenImage.size) # 創(chuàng)建新圖片以存放編碼后的像素
encodedImage.putdata(encodedPixels) # 添加編碼后的數(shù)據(jù)
return encodedImage
if __name__ == '__main__':
encodeDataInImage(Image.open(IMG), TEXT).save('code.png')
decode.py 解碼
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from PIL import Image
import argparse
#命令行輸入?yún)?shù)處理
parser = argparse.ArgumentParser()
parser.add_argument('file') #輸入文件
#獲取參數(shù)
args = parser.parse_args()
IMG = args.file
"""
從二進(jìn)制字符串轉(zhuǎn)為 UTF-8 字符串
"""
def binaryToString(binary):
index = 0
string = []
rec = lambda x, i: x[2:8] + (rec(x[8:], i-1) if i > 1 else '') if x else ''
# rec = lambda x, i: x and (x[2:8] + (i > 1 and rec(x[8:], i-1) or '')) or ''
fun = lambda x, i: x[i+1:8] + rec(x[8:], i-1)
while index + 1 < len(binary):
chartype = binary[index:].index('0') # 存放字符所占字節(jié)數(shù)列荔,一個(gè)字節(jié)的字符會存為 0
length = chartype*8 if chartype else 8
string.append(chr(int(fun(binary[index:index+length],chartype),2)))
index += length
return ''.join(string)
"""
解碼隱藏?cái)?shù)據(jù)
"""
def decodeImage(image):
pixels = list(image.getdata()) # 獲得像素列表
binary = ''.join([str(int(r>>1<<1!=r))+str(int(g>>1<<1!=g))+str(int(b>>1<<1!=b))+str(int(t>>1<<1!=t)) for (r,g,b,t) in pixels]) # 提取圖片中所有最低有效位中的數(shù)據(jù)
# 找到數(shù)據(jù)截止處的索引
locationDoubleNull = binary.find('0000000000000000')
endIndex = locationDoubleNull+(8-(locationDoubleNull % 8)) if locationDoubleNull%8 != 0 else locationDoubleNull
data = binaryToString(binary[0:endIndex])
return data
if __name__ == '__main__':
print(decodeImage(Image.open(IMG)))
貌似把chr改成unichr就支持python2.7了好神奇