【问题标题】:Image steganography script not producing correct results图像隐写脚本没有产生正确的结果
【发布时间】:2019-07-04 17:41:17
【问题描述】:

我正在尝试使用 PIL 中的图像库在 python 3.7.2 中制作图像隐写脚本。我将一个图像隐藏在另一个图像中的脚本无法正常工作,一旦再次提取生成的文件 hidden.png,它会输出全黑图像或对比度较低且颜色不同的图像,具体取决于所选的位数。 (我的提取脚本已经过测试并且可以正常工作。)我通常选择 4 位,但在 7 时仍然无法正常工作。

这是我的隐藏脚本代码:

def hide(medium, secret_image, lsb):
    medimage = Image.open(medium).convert(mode="RGB") #open the medum
    secretimage = Image.open(secret_image).convert(mode="RGB") #open the secret image
    medimage = medimage.resize(secretimage.size) #resize the medium to be same size as secret
    acrossrow = 0 #start at first row
    downcol = 0 #start at first column
    secret = secretimage.load() #load pixels from secretimage
    med = medimage.load()
    while acrossrow < secretimage.height:
        downcol = 0 #stay first column until reach bottom row
        while downcol < secretimage.width:
            r, g, b, = secret[acrossrow,downcol] #r,g,b = the rgb of secret image pixel
            r = (r >> 8 - lsb) #shift amount of significant bits wanted to the end for hiding
            g = (g >> 8 - lsb)
            b = (b >> 8 - lsb)
            r1, g1, b1 = med[acrossrow,downcol] #more rgb values for medium
            r1 = r1 & (0b11111111 << lsb) #remove the last n amount of bits for replacing
            g1 = g1 & (0b11111111 << lsb)
            b1 = b1 & (0b11111111 << lsb)
            r1 = r1 | r #compare medium with secret, combining all 1s
            g1 = g1 | g
            b1 = b1 | g
            med[acrossrow,downcol] = (r1, g1, b1) #send new rgb values to medium
            downcol = downcol + 1 #go to next column
        acrossrow = acrossrow + 1 #go to next row
    medimage.save('hidden.png') #save modified image to new file
    medimage.show() #open and display new image

仅供参考:medium = 媒体的路径,secret_image = 秘密的路径,lsb 是我想从秘密图像隐藏到媒体中的位数。

我已经检查了我的代码,但看不到问题所在,如果有人可以帮助我,那就太好了。谢谢!

编辑:Here is a link to the full script if you want to test it or build on it. Here is the link to my test hidden.png That one uses 2 lsb Here is the link to the medium. Here is the link to the secret image. 对于链接的媒体和秘密图像,我使用 4 lsb。

【问题讨论】:

  • 请修正代码的缩进。 lsb 是什么?
  • @martineau 缩进是固定的。 lsb 是我选择在脚本的其他地方分析的“最低有效位”的数量。我通常选择4。
  • 您需要提供minimal reproducible example,包括测试图像的链接。我尝试使用我自己的图像运行您的代码,但无法重现您遇到的问题。我还发现您声称提取脚本已经过测试并且正在努力相信,因为您(显然)无法制作任何图像来对其进行测试。
  • @martineau 我有预先制作的图像进行测试。如果你愿意,我可以发给你吗?
  • 没有。不要亲自将它们发送给我——最好将它们上传到某个地方(例如imgur)并将它们的链接添加到您的问题中,因为这样其他人也可以获取它们的副本。不要忘记还包括一个hidden.png,其中包含您所说的结果。

标签: python image-processing python-imaging-library python-3.7 steganography


【解决方案1】:

我已经检查了我的代码,并通过将我的提取脚本从我的原始隐藏代码更改为:

def hide(medium, secret_image, lsb):
    medimage = Image.open(medium).convert(mode="RGB")
    secretimage = Image.open(secret_image).convert(mode="RGB")
    medimage = medimage.resize(secretimage.size)
    acrossrow = 0
    downcol = 0
    secret = secretimage.load()
    med = medimage.load()
    result = Image.new("RGB", medimage.size)
    r_pixels = result.load()
    while acrossrow < secretimage.height:
        downcol = 0
        while downcol < secretimage.width:
            m_r, m_g, m_b = med[acrossrow, downcol]
            s_r, s_g, s_b = secret[acrossrow, downcol]
            r = (m_r >> lsb << lsb) | (s_r >> (8 - lsb))
            g = (m_g >> lsb << lsb) | (s_g >> (8 - lsb))
            b = (m_b >> lsb << lsb) | (s_b >> (8 - lsb))
            r_pixels[acrossrow, downcol] = r, g, b
            downcol = downcol + 1
        acrossrow = acrossrow + 1
    result.save('testhid.png')
    result.show()
    result.close() 
    medimage.close()
    secretimage.close()

它现在可以完美地处理@martineau 发布的图像,其中两个都是黄色大脑。我更新后的代码的链接是here. 如果有人能解释为什么这有效而我的原始代码无效,那就太好了! 谢谢!

【讨论】:

    【解决方案2】:

    好吧,在检查了您添加的图像并运行了我自己的测试之后,据我所知,您的代码运行正常。对于我自己的测试,我使用sat.png 作为“中等”图像应用了hide() 函数brain.png 文件,并检查了生成的hidden.png 文件,链接代码中的extract() 函数从它生成(使用@ 987654328@4 的值)。

    是的,结果中的颜色与原始颜色略有不同,但这是意料之中的,因为使用的隐藏过程有效地将原始 (3 x 8) 的每像素 24 位 (3 x 8) 减少到 12 (3 * 4),因此对比度之类的事情会因此受到影响,这是可以理解的。

    以下是在我的图像编辑器中并排显示的图像的原始(左)和提取版本,显示了这些差异:

    我认为问题可能仅仅是因为你没有完全理解这种特殊的隐写技术是如何工作的。

    【讨论】:

    • 真的不知道你会怎么做。这种方法实际上需要将两个图像压缩在通常分配用于存储一个的相同数量的位中,因此必须有一些信息丢失。您可能能够以某种“更智能”的方式压缩隐藏图像,而不仅仅是折腾其像素的较低位。例如,将隐藏图像转换为 JPG 格式可能会生成更好看的压缩图像。 “隐藏”并存储在中等图像的低位中的数据可以是任何格式(即它不仅限于 PNG 图像的 n 位像素)。
    • 好的,谢谢。最后,我自己通过一些搜索和反复试验找到了解决方案。如果你想看看,我已经发布了。我仍然不确定这个新代码有什么不同......
    猜你喜欢
    • 1970-01-01
    • 2015-06-23
    • 2014-03-14
    • 2011-01-19
    • 1970-01-01
    • 1970-01-01
    • 2022-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多