问题描述
我的程序遇到一些问题,基本上我想做的是速记技术,将图像插入另一个图像,然后提取秘密图像.
I'm having some issues with my program, basically what I'm trying to do is Stenography, insert an image into another image and then extract the secret image.
我的程序能够很好地插入,但是将其解压缩会出现此错误.
My program is able to insert just fine, but extracting it I get this error.
前几天一切正常,我能够完美地阅读和书写.我也使用与前几天相同的BMP图像.有人知道这个问题吗?我没有从其工作状态更改任何代码,甚至将其上传到github以确保该版本有效.
It was working fine the other day, I was able to read and write perfectly. I am also using the same BMP images as the other day as well. Anyone know the issue? I didn't change any of the code from it's working state, I even uploaded to github to make sure that version worked.
我的github上有3个代码文件.
There are 3 files of code that is on my github.
https://github.com/am3ience/Steganography/blob/master /dcstego.py
https://github.com/am3ience/Steganography/blob/master /dcimage.py
https://github.com/am3ience/Steganography/blob/master /dcutils.py
问题似乎在我的读取功能的dcutils.py中发生.任何帮助都将是惊人的,我很沮丧.
The problem seems to be happening in dcutils.py in my read function. Any help would be amazing, i'm stumped.
#examine the lsb of each pixel, grouping into bytes
#check for nulls to signify if we are dealing with data or header info
#bytes determined to be data result in the hidden file
#---------------------------------------------------------------
def read(mainimage, output, password):
lsbByte_Array = []
dataString = ""
secretFileName = ""
lsbString = ""
count = 0#iterator
headerReceived=0#flags
sizeReceived=0
imageObject = dcimage.openFile(mainimage)
pixels = imageObject.load()
imageWidth, imageHeight = imageObject.size
#cycle through each pixel
for x in range(imageWidth):
for y in range(imageHeight):
r, g, b = pixels[x, y]
#trim so we are dealing with only the least significant bit
redPixel = str(bin(r)[2:].zfill(8))[7]
greenPixel = str(bin(g)[2:].zfill(8))[7]
bluePixel = str(bin(b)[2:].zfill(8))[7]
secretBits = [redPixel, greenPixel, bluePixel]
#for each of rgb
for i in range(0,3):
#check if our flags are set
if (headerReceived == 0 or sizeReceived == 0):
lsbString += secretBits[i]
#verify each byte
if len(lsbString) == 8:
lsbByte_Array.append(lsbString)
#check if we have received a NULL byte
if lsbString == "00000000":
if headerReceived == 0:
#convert the the bit array into an ascii String
#set flag when header and size was received
fileName = ''.join(binascii.unhexlify('%x' % int(b,2)) for b in lsbByte_Array[0:len(lsbByte_Array) - 1])
print "File name: " + str(fileName)
headerReceived = 1
elif sizeReceived == 0:
fileSize = ''.join(binascii.unhexlify('%x' % int(b,2)) for b in lsbByte_Array[0:len(lsbByte_Array) - 1])
print "File size: " + fileSize
sizeReceived=1
#reset the values
lsbByte_Array = []
lsbString = ""
#once headers received, resulting data is hidden data
elif (headerReceived == 1 and sizeReceived == 1):
if int(count) < int(fileSize):
#keep appending secret bits to the dataString until depleted
dataString += secretBits[i]
count += 1
else:
#send to have hidden file created
return dcimage.saveImage(output, dataString)
推荐答案
fileSize = ''.join(binascii.unhexlify('%x' % int(b,2)) for b in
lsbByte_Array[0:len(lsbByte_Array) - 1])
如果lsbByte_Array
是一个空数组,那么fileSize
将是一个空字符串,这显然是int(fileSize)
失败的原因.
if lsbByte_Array
is an empty array then fileSize
will be an empty string, which is obviously why int(fileSize)
fails.
您可能应该使用默认值,例如
You should probably use a default value, for example
fileSize = ''.join(binascii.unhexlify('%x' % int(b,2)) for b in
lsbByte_Array[0:len(lsbByte_Array) - 1]) or 0
请注意,像lsbByte_Array[0:len(lsbByte_Array) - 1])
之类的东西可以简单地写为lsbByte_Array[0:-1]
.
As a side note, stuff like lsbByte_Array[0:len(lsbByte_Array) - 1])
can simply be written as lsbByte_Array[0:-1]
.
这篇关于ValueError:以int()为底数为10的int()无效文字,当它在之前工作时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!