【发布时间】: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