簡體   English   中英

ValueError:int() 的無效文字,基數為 10: ' ' 之前工作時

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

我的程序有一些問題,基本上我要做的是速記,將圖像插入另一個圖像,然后提取秘密圖像。

我的程序能夠很好地插入,但提取它時出現此錯誤。

在此處輸入圖片說明

前幾天它工作正常,我能夠完美地閱讀和寫作。 我也使用與前幾天相同的 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

問題似乎發生在我的 read 函數中的 dcutils.py 中。 任何幫助都會很棒,我很難過。

#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)失敗的原因。

例如,您可能應該使用默認值

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]

我覺得真的很傻,我只是想聽別人說,事實證明,在我無限的智慧中,我只是嘗試打開沒有實際隱藏數據的圖像。 但是我確實按照這里的其他用戶的建議修復了我的代碼。 謝謝你們的一切!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM