python实现图像隐写
Steganography is the art of hiding secret data in any file.
隐秘术是在任何文件中隐藏秘密数据的艺术。
The secret data can be data of any format like text or even a file. In a nutshell, the main motive of steganography is to hide the intended information within any file, usually an image, audio, or video, without actually changing the external appearance of the file, i.e. it should look the same as before.
秘密数据可以是任何格式的数据,例如文本,甚至是文件。 简而言之,隐写术的主要目的是在任何文件(通常是图像,音频或视频)中隐藏预期的信息,而无需实际更改文件的外观,即,其外观应与以前相同。
In this blog, we will be focussing on learning image-based steganography, i.e. hiding secret data in an image.
在此博客中,我们将专注于学习基于图像的隐写术,即在图像中隐藏秘密数据。
But before diving a little deeper into it, let’s look at what an image comprises of.
但是在深入探讨之前,让我们先看看图像的组成。
- Pixels are the building blocks of an image. 像素是图像的基础。
- Every pixel contains three values: (red, green, blue) also known as RGB values. 每个像素包含三个值:(红色,绿色,蓝色)也称为RGB值。
- Every RGB value ranges from 0 to 255. 每个RGB值的范围是0到255。
This much information is enough to get started.
这些足够的信息足以入门。
Now, let’s look at how we can encode and decode data into our image.
现在,让我们看看如何将数据编码和解码为图像。
编码方式 (Encoding)
There are a lot of algorithms that can be used to encode data into the image, and in fact, you can also make one yourself. The one being used in this blog is easy to understand and implement, as well.
有很多算法可用于将数据编码到图像中,实际上,您也可以自己制作。 本博客中使用的一个也易于理解和实施。
The algorithm is as follows:
算法如下:
- For each character in the data, its ASCII value is taken and converted into 8-bit binary [1]. 对于数据中的每个字符,均采用其ASCII值并将其转换为8位二进制[1]。
- Three pixels are read at a time having a total of 3*3=9 RGB values. The first eight RGB values are used to store one character that is converted into an 8-bit binary. 一次读取三个像素,总共具有3 * 3 = 9 RGB值。 前八个RGB值用于存储一个字符,该字符将转换为8位二进制数。
- The corresponding RGB value and binary data are compared. If the binary digit is 1 then the RGB value is converted to odd and, otherwise, even. 比较相应的RGB值和二进制数据。 如果二进制数字为1,则RGB值将转换为奇数,否则将转换为偶数。
- The ninth value determines if more pixels should be read or not. If there is more data to be read, i.e. encoded or decoded, then the ninth pixel changes to even. Otherwise, if we want to stop reading pixels further, then make it odd. 第九个值确定是否应读取更多像素。 如果还有更多数据要读取(即编码或解码),则第九个像素变为偶数。 否则,如果我们想停止进一步读取像素,则将其设为奇数。
Repeat this process until all the data is encoded into the image.
重复此过程,直到所有数据都编码到图像中。
例 (Example)
Suppose the message to be hidden is ‘Hii’
.
假设要隐藏的消息是'Hii'
。
The message is of three bytes, therefore, the pixels required to encode the data are 3 x 3 = 9. Consider a 4 x 3 image with a total of 12 pixels, which are sufficient to encode the given data.
该消息为三个字节,因此,对数据进行编码所需的像素为3 x 3 =9。请考虑一个4 x 3的图像,总共12个像素,足以对给定的数据进行编码。
[(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)]