ValueError:基为10的int()的文本无效,当它工作时b

2024-05-21 00:17:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我的程序有些问题,基本上我要做的是速记,把一个图像插入另一个图像,然后提取出秘密图像。在

我的程序可以很好地插入,但提取它时会出现这个错误。在

enter image description here

前几天还不错,我读写都很好。我也在使用和前几天一样的BMP图像。有人知道这个问题吗?我没有改变任何代码的工作状态,我甚至上传到github以确保版本正常工作。在

我的github上有3个代码文件。在

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在我的read函数中。任何帮助都会很棒的,我被难住了。在

#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)

Tags: thetoinpy图像githubfordata
2条回答
fileSize = ''.join(binascii.unhexlify('%x' % int(b,2)) for b in 
                   lsbByte_Array[0:len(lsbByte_Array) - 1])

如果lsbByte_Array是空数组,那么fileSize将是空字符串,这显然就是int(fileSize)失败的原因。在

例如,您可能应该使用默认值

^{pr2}$

作为补充说明,像lsbByte_Array[0:len(lsbByte_Array) - 1])这样的东西可以简单地写成lsbByte_Array[0:-1]。在

我觉得自己真的很傻,我只是想听听别人的意见,结果在我无限的智慧里,我只是试着打开没有隐藏数据的图片。但我确实按照其他用户的建议修改了代码。谢谢大家!在

相关问题 更多 >