Python中的编码错误
正在尝试进行编码过程,但出现错误行: 请你看看我的全部功能,为了得到它。我觉得它不够大 正在尝试将标头添加到文件数据:Python中的编码错误,python,Python,正在尝试进行编码过程,但出现错误行: 请你看看我的全部功能,为了得到它。我觉得它不够大 正在尝试将标头添加到文件数据: #Add the header to the file data headerdata = struct.pack("4s"+\ "I"+\ str(Header.MAX_FORMAT_LENGTH)+"s",header.magicnum, header
#Add the header to the file data
headerdata = struct.pack("4s"+\
"I"+\
str(Header.MAX_FORMAT_LENGTH)+"s",header.magicnum, header.size, header.fformat)
filebytes = headerdata + data
出现错误:
def encode(image, data, filename, encryption=False, password=""):
im = Image.open(image)
px = im.load()
#Create a header
header = Header()
header.size = len(data)
header.fformat = "" if (len(filename.split(os.extsep))<2)\
else filename.split(os.extsep)[1]
#Add the header to the file data
headerdata = struct.pack("4s"+\
"I"+\
str(Header.MAX_FORMAT_LENGTH)+"s",header.magicnum, header.size, header.fformat)
filebytes = headerdata + data
#Optional encryption step
if encrypt:
if password:
filebytes = encrypt(filebytes, password,\
padding=im.width*im.height - len(filebytes))
else:
print ("Password is empty, encryption skipped")
#Ensure the image is large enough to hide the data
if len(filebytes) > im.width*im.height:
print ("Image too small to encode the file. \
You can store 1 byte per pixel.")
exit()
for i in range(len(filebytes)):
coords = (i%im.width, i/im.width)
byte = ord(filebytes[i])
px[coords[0], coords[1]] = encode_in_pixel(byte, px[coords[0],\
coords[1]])
im.save("output.png", "PNG")
str(Header.MAX_FORMAT_LENGTH)+“s”,Header.magicnum,Header.size,Header.fformat)
struct.error:“s”的参数必须是字节对象
我试图改变它:(这一行,addin'b')
还有一个错误:
def encode(image, data, filename, encryption=False, password=""):
im = Image.open(image)
px = im.load()
#Create a header
header = Header()
header.size = len(data)
header.fformat = "" if (len(filename.split(os.extsep))<2)\
else filename.split(os.extsep)[1]
#Add the header to the file data
headerdata = struct.pack("4s"+\
"I"+\
str(Header.MAX_FORMAT_LENGTH)+"s",header.magicnum, header.size, header.fformat)
filebytes = headerdata + data
#Optional encryption step
if encrypt:
if password:
filebytes = encrypt(filebytes, password,\
padding=im.width*im.height - len(filebytes))
else:
print ("Password is empty, encryption skipped")
#Ensure the image is large enough to hide the data
if len(filebytes) > im.width*im.height:
print ("Image too small to encode the file. \
You can store 1 byte per pixel.")
exit()
for i in range(len(filebytes)):
coords = (i%im.width, i/im.width)
byte = ord(filebytes[i])
px[coords[0], coords[1]] = encode_in_pixel(byte, px[coords[0],\
coords[1]])
im.save("output.png", "PNG")
str(Header.MAX_FORMAT_LENGTH)+b's',Header.magicnum,Header.size,Header.fformat)类型错误:必须是str,而不是字节
整个functon:
def encode(image, data, filename, encryption=False, password=""):
im = Image.open(image)
px = im.load()
#Create a header
header = Header()
header.size = len(data)
header.fformat = "" if (len(filename.split(os.extsep))<2)\
else filename.split(os.extsep)[1]
#Add the header to the file data
headerdata = struct.pack("4s"+\
"I"+\
str(Header.MAX_FORMAT_LENGTH)+"s",header.magicnum, header.size, header.fformat)
filebytes = headerdata + data
#Optional encryption step
if encrypt:
if password:
filebytes = encrypt(filebytes, password,\
padding=im.width*im.height - len(filebytes))
else:
print ("Password is empty, encryption skipped")
#Ensure the image is large enough to hide the data
if len(filebytes) > im.width*im.height:
print ("Image too small to encode the file. \
You can store 1 byte per pixel.")
exit()
for i in range(len(filebytes)):
coords = (i%im.width, i/im.width)
byte = ord(filebytes[i])
px[coords[0], coords[1]] = encode_in_pixel(byte, px[coords[0],\
coords[1]])
im.save("output.png", "PNG")
def encode(图像、数据、文件名、加密=False、密码=”):
im=图像。打开(图像)
px=im.load()
#创建标题
header=header()
header.size=len(数据)
header.fformat=”“if(len(filename.split(os.extsep))im.width*im.height:
打印(“图像太小,无法对文件进行编码\
每像素可以存储1字节。”)
退出()
对于范围内的i(len(filebytes)):
坐标=(i%im.width,i/im.width)
字节=ord(文件字节[i])
px[coords[0],coords[1]]=以像素(字节,px[coords[0])编码\
coords[1]])
保存(“output.png”、“png”)
您的原始代码是正确的,只是header.magicnum的类型是意外的。您的代码片段应为
#Add the header to the file data
headerdata = struct.pack("4s"+\
"I"+\
str(Header.MAX_FORMAT_LENGTH)+"s","{:04d}".format(header.magicnum).encode('UTF-8'), header.size, header.fformat)
filebytes = headerdata + data
或者其他一些合适的格式代码和编码,将header.magicnum
转换为您期望的结果。code
既然你说它们都是弦,给你
headerdata = struct.pack("4s"+\
"I"+\
str(Header.MAX_FORMAT_LENGTH)+"s",header.magicnum.encode(), int(header.size), header.fformat.encode())
这应该适用于您想要的格式和类型
解释
根据第7.1.2.2节,我们可以找到作为以下格式字符参数所需的类型:
-----------------------------------------
|Formatting Character | Type (in python)|
-----------------------------------------
|s | integer |
-----------------------------------------
|I | bytes |
-----------------------------------------
由于要格式化的数据类型为str
,因此我们需要对其进行更改
让我们先做一个str
to和integer
,因为这是最简单的
>>> x = '123'
>>> type(x)
str
>>> y = int(x)
>>> type(y)
int
简单,我们只需要在字符串上调用int()
下一步是将字符串转换为字节。我们使用stringsencode()
方法来实现这一点
您的错误与其他参数有关,而不是与格式字符串有关。它是指格式字符串中的“s”,它需要一个字节对象。请查看第7.1.2.2节header.magicnum、header.size和header的类型。fformat@jacoblaw应该是str
…无法获取。您的答案中是否缺少“``一词?是的,已修复。是否有其他错误,urghhh((((它写:ValueError:Unknown format code'd'for type'str'
如果header.magicnum已经是一个字符串,那么只需使用header.magicnum.encode('UTF-8')
;这将把字符串转换成字节
对象(并去掉“{:04d}.format()
部分)。这样做smth code>str(Header.MAX_FORMAT_LENGTH)+“s”,Header.magicnum.encode('UTF-8'),Header.size,Header.fformat)struct.error:'s'的参数必须是一个字节对象
在一个地方走动。如果它是str
,尝试编码
,它为什么需要字节对象?@x20添加了解释