简体   繁体   中英

'str' object has no attribute 'decode' error

The function below first decodes the hexadecimal number into a string and then the map function applies the ord function to all the decoded values and give us a tuple but this decode function is giving this error 'str' object has no attribute 'decode' :

def hex2rgb(hexcode):
    return tuple(map(ord, hexcode[1:].decode( )))

The code that I am writing is pertain to steganography here some part of the code which shows how the input to that function is generated:

binary = str2bin(message) + '1111111111111110' #converts string into binary and adds the delimeter at end to indicate message is ended

newData = [] #list to store the new pixels which contain the information

img = Image.open(filename)

if img.mode in ('RGBA'):
    img = img.convert('RGBA') #will convert the image into RGBA
    datas = img.getdata()  #will give all the pixel data
    digit = 0 #shows on which binary bit we are currently at      
    for item in datas:
        if (digit < len(binary)):
            newpix = encode(rgb2hex(item[0],item[1],item[2]),binary[digit])
            if newpix == None:
                newData.append(item)
            else:
                r, g, b = hex2rgb(newpix)
                newData.append((r,g,b,255))
                digit += 1

I don't understand why it is not decoding. I also tried .decode('hex') or .decode(hex) but it is still giving the error.

Your approach is incorrect. because you are trying to decode string that is already decoded. That's why the error is coming.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM