隐秘通信的3种典型方式:

①将秘密信息隐于网络通信协议中。

②将秘密信息隐于数字签名等密码协议中 。

③将秘密信息隐于数字图像中。

第三种是利用图像或音频数据对人类感官系统的冗余。

隐藏域数字图像中的代码案例(python)

from PIL import Image

def makeImageEven(image):
"""
取得一个 PIL 图像并且更改所有值为偶数(使最低有效位为0)
"""
# 得到一个这样的列表:[(r,g,b,t),(r,g,b,t)...]
pixels = list(image.getdata())
# 更改所有值为偶数(魔法般的移位)
evenPixels = [(r >> 1 << 1, g >> 1 << 1, b >> 1 << 1, t >> 1 << 1) for [r, g, b, t] in pixels]
# 创建一个相同大小的图片副本
evenImage = Image.new(image.mode, image.size)
# 把上面的像素放入到图片副本
evenImage.putdata(evenPixels)
return evenImage def constLenBin(int):
"""
内置函数bin()的替代,返回固定长度的二进制字符串
"""
# 去掉bin()返回的二进制字符串中的'0b',并在左边补足'0'直到字符串长度为8
binary = "0" * (8 - (len(bin(int)) - 2)) + bin(int).replace('0b', '')
return binary def encodeDataInImage(image, data):
"""
将字符串编码到图片中
"""
# 获得最低有效位为 0 的图片副本
evenImage = makeImageEven(image)
# 将需要被隐藏的字符串转换成二进制字符串
binary = ''.join(map(constLenBin, bytearray(data, 'utf-8')))
if len(binary) > len(image.getdata()) * 4:
# 如果不可能编码全部数据,跑出异常
raise Exception("Error: Can't encode more than" + len(evenImage.getdata()) * 4 + " bits in this image. ")
# 将binary中的二进制字符串信息编码进像素里
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()))]
# 创建新图片以存放编码后的像素
encodedImage = Image.new(evenImage.mode, evenImage.size)
# 添加编码后的数据
encodedImage.putdata(encodedPixels)
return encodedImage def binaryToString(binary):
"""
从二进制字符串转为 UTF-8 字符串
"""
index = 0
string = []
rec = lambda x, i: x[2:8] + (rec(x[8:], i - 1) if i > 1 else '') if x else ''
fun = lambda x, i: x[i + 1:8] + rec(x[8:], i - 1)
while index + 1 < len(binary):
chartype = binary[index:].index('0') # 存放字符所占字节数,一个字节的字符会存为0
length = chartype * 8 if chartype else 8
string.append(chr(int(fun(binary[index:index + length], chartype), 2)))
index += length
return ''.join(string) 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])
# 找到数据截止处的索引
locationDoubleNull = binary.find('0000000000000000')
endIndex = locationDoubleNull + (
8 - (locationDoubleNull % 8)) if locationDoubleNull % 8 != 0 else locationDoubleNull
data = binaryToString(binary[0:endIndex])
return data str1 = '哆啦A梦的世界,Doraemon World!哆啦A梦的世界,Doraemon World!哆啦A梦的世界,Doraemon World!哆啦A梦的世界,Doraemon World!哆啦A梦的世界,Doraemon World!哆啦A梦的世界,Doraemon World!哆啦A梦的世界,Doraemon World!哆啦A梦的世界,Doraemon World!哆啦A梦的世界,Doraemon World!哆啦A梦的世界,Doraemon World!哆啦A梦的世界,Doraemon World!哆啦A梦的世界,Doraemon World!哆啦A梦的世界,Doraemon World!哆啦A梦的世界,Doraemon World!12345678901234567890123455678'
# 可以隐藏的数据量是有限的目前可以隐藏350个字符
print(len(str1))
# png为不失真图片,jpg为失真图片。jpg不可隐藏信息。
encodeDataInImage(Image.open("coffee.png"), str1).save('encodeImage.png')
print(decodeImage(Image.open("encodeImage.png")))

  

输出:

python实现图片隐藏信息技术的更多相关文章

  1. Python实现图像信息隐藏

    Python实现图像信息隐藏 之前学习密码学的时候老师有提到过『信息隐藏』,现在用图像的方法尝试一下.思想是:把信息藏到RGB通道中的B通道,然后利用奇偶性可以恢复过来 原理 从源图中提取文字图像信息 ...

  2. Python提取图片的ROI

    图像处理经常需要提取图片的ROI,本文使用Python提取图片的ROI. 使用的Module是PIL (Pillow),一个图像处理库,用到的函数为类 Image 中的 crop 方法. 函数原型为: ...

  3. python 读取图片的尺寸、分辨率

    #需要安装PIL模块 #encoding=gbk#--------------------------------------------------------------------------- ...

  4. python 对比图片相似度

    最近appium的使用越来越广泛了,对于测试本身而言,断言同样是很重要的,没有准确的断言那么就根本就不能称之为完整的测试了.那么目前先从最简单的截图对比来看.我这里分享下python的图片相似度的代码 ...

  5. python在图片上画矩形

    python在图片上画矩形 image_path = '' image = cv2.imread(image_path) first_point = (100, 100) last_point = ( ...

  6. Python读取图片尺寸、图片格式

    Python读取图片尺寸.图片格式 需要用到PIL模块,使用pip安装Pillow.Pillow是从PIL fork过来的Python 图片库. from PIL import Image im = ...

  7. Python下图片的高斯模糊化的优化

    资源下载 #本文PDF版下载 Python下图片的高斯模糊化的优化(或者单击我博客园右上角的github小标,找到lab102的W6目录下即可) #本文代码下载 高斯模糊(一维)优化代码(和本文方法集 ...

  8. 孤荷凌寒自学python第二十四天python类中隐藏的私有方法探秘

    孤荷凌寒自学python第二十四天python类中隐藏的私有方法探秘 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 今天发现了python的类中隐藏着一些特殊的私有方法. 这些私有方法不管我 ...

  9. 鼠标单击到 img行的时候图片隐藏方案

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

随机推荐

  1. adb shell dumpsys 命令

    Android开发中,常常可以用adb shell dumpsys这条命令来dump出系统运行时的状态信息,例如可以这样来察看某个应用的内存使用信息 adb shell dumpsys meminfo ...

  2. LRU 算法

    LRU算法 很多Cache都支持LRU(Least Recently Used)算法,LRU算法的设计原则是:如果一个数据在最近一段时间没有被访问到,那么在将来它被访问的可能性也很小.也就是说,当限定 ...

  3. SpringBoot------个性化启动Banner设置

    1.添加Banner.txt文件 . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ ...

  4. [Linux] ssh-key 公钥文件格式

    SSH 协议(Secure Shell 协议)最初在 1995 年由芬兰的 Tatu Ylönen 设计开发,由 IETF(Internet Engineering Task Force)的网络工作小 ...

  5. TSPL学习笔记(2):过程和变量绑定

    变量的引用 语法: variable 返回: variable的值 如果在某个范围内存在对某个标识符的变量绑定,那么当这个标识符以表达式的形式出现的时候被认为是其所绑定变量的值. 在引用一个标识符的时 ...

  6. callback 模式

    回调,是一种机制,同时也是一种设计模式. 我们定义一个函数,让能够回调 import _products from './products.json' const TIMEOUT = 100 cons ...

  7. 01List.ashx(班级列表动态页面)

    01List.html <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <he ...

  8. html学习_认识html

    1.HTML骨架 <html>----根标签 <head>---头标签 </head> <body>---主体标签 </body> < ...

  9. 泡泡一分钟:A Multi-Position Joint Particle Filtering Method for Vehicle Localization in Urban Area

    A Multi-Position Joint Particle Filtering Method for Vehicle Localization in Urban Area 城市车辆定位的多位置联合 ...

  10. centos 断网

    不知道为什么.....开启服务后就是断线 查询 ip addr 故障状态 正常状态 配置中关闭网络管理 命令:chkconfig NetworkManager off 配置中开启网络管理 命令: ch ...