如何解决“str”对象没有属性“解码”错误
下面的函数首先将十六进制数解码为一个字符串,然后 map 函数将 ord
函数应用于所有解码的值并给我们一个元组,但是这个解码函数给出了这个错误 'str' object has no attribute 'decode'
:
def hex2rgb(hexcode):
return tuple(map(ord,hexcode[1:].decode( )))
我正在编写的代码与隐写术有关,其中部分代码显示了该函数的输入是如何生成的:
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,b,255))
digit += 1
我不明白为什么它没有解码。我也试过 .decode('hex')
或 .decode(hex)
但它仍然给出错误。
解决方法
你的方法不正确。因为您正在尝试解码已经解码的字符串。这就是错误出现的原因。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。