[英]'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( )))
我正在編寫的代碼與隱寫術有關,這里有一部分代碼顯示了如何生成 function 的輸入:
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
我不明白為什么它不解碼。 我也試過.decode('hex')
或.decode(hex)
但它仍然給出錯誤。
你的方法不正確。 因為您正在嘗試解碼已經解碼的字符串。 這就是錯誤來的原因。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.