简体   繁体   English

ValueError:int() 的无效文字,基数为 10: ' ' 之前工作时

[英]ValueError: invalid literal for int() with base 10: ' ' when it worked before

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.我的程序能够很好地插入,但提取它时出现此错误。

在此处输入图片说明

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.我也使用与前几天相同的 BMP 图像。 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 以确保该版本有效。

There are 3 files of code that is on my github.我的 github 上有 3 个代码文件。

https://github.com/am3ience/Steganography/blob/master/dcstego.py 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/dcimage.py

https://github.com/am3ience/Steganography/blob/master/dcutils.py https://github.com/am3ience/Steganography/blob/master/dcutils.py

The problem seems to be happening in dcutils.py in my read function.问题似乎发生在我的 read 函数中的 dcutils.py 中。 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])

if lsbByte_Array is an empty array then fileSize will be an empty string, which is obviously why int(fileSize) fails.如果lsbByte_Array是一个空数组,那么fileSize将是一个空字符串,这显然是int(fileSize)失败的原因。

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

As a side note, stuff like lsbByte_Array[0:len(lsbByte_Array) - 1]) can simply be written as lsbByte_Array[0:-1] .作为旁注,像lsbByte_Array[0:len(lsbByte_Array) - 1])可以简单地写成lsbByte_Array[0:-1]

I feel really dumb, I just needed to hear it from other people, it turns out in my infinite wisdom, I just tried opening images without actual hidden data in it.我觉得真的很傻,我只是想听别人说,事实证明,在我无限的智慧中,我只是尝试打开没有实际隐藏数据的图像。 But I did fix up my code as other users here advised me to.但是我确实按照这里的其他用户的建议修复了我的代码。 Thanks for everything guys!谢谢你们的一切!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError: int() 的无效文字,以 10 为基数:运行 unittest 时为“30.0” - ValueError: invalid literal for int() with base 10: '30.0' when running unittest ValueError: int() 以 10 为基数的无效文字:'' 询问输入时 - ValueError: invalid literal for int() with base 10: '' when asking for input ValueError:int() 的无效文字以 10 为基数:&#39; &#39; 转换数据类型时 - ValueError: invalid literal for int() with base 10: ' ' when convert type of data ValueError:int() 的无效文字,基数为 10:&#39;26.02.2018&#39; - ValueError: invalid literal for int() with base 10: '26.02.2018' 第8行:ValueError:int()以10为底的无效文字:“&#39; - Line 8: ValueError: invalid literal for int() with base 10: ' ' ValueError:int()以10为基的无效文字:“ skip” - ValueError: invalid literal for int() with base 10: 'skip' ValueError:以10为底的int()的无效文字:&#39;&#39; - ValueError: invalid literal for int() with base 10: '' ValueError:以10为底的int()的无效文字:&#39;SOH&#39; - ValueError: invalid literal for int() with base 10: 'SOH' 错误 - ValueError:基数为10的int()的文字无效:&#39;&#39; - Error - ValueError: invalid literal for int() with base 10: ' ' / feed /处的ValueError-以10为底的int()的无效文字 - ValueError at /feed/ - invalid literal for int() with base 10
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM