我想读取二进制文件(如 .bmp 或 .jpg)并将每个二进制数据转换为 ascii 并进行乘法(如 ascii * 2)并转换回二进制格式并写入新文件。
我写的程序如下
filename = input("enter file to read: ")
readfile = filename + ".bmp"
writefilebmp = filename + '1' + ".bmp"
fr = open(readfile,"rb");
fwbmp = open(writefilebmp,"wb");
bytes_read = fr.read(1024)
for b in bytes_read:
print(b,end='')
data = b*2;
fwbmp.write(data)
fr.close()
fwbmp.close()
但我收到以下错误
TypeError: 'int' 不支持缓冲区接口
注意:一旦我解决了,我将修改代码以跳过 bmp 或 jpeg 标头
修改的
filename = input("enter file to read: ")
readfile = filename + ".bmp"
writefilebmp = filename + '1' + ".bmp"
fr = open(readfile,"rb");
fwbmp = open(writefilebmp,"wb");
bytes_read = fr.read(20)
for b in bytes_read:
print(b,end='')
data = b*2;
fwbmp.write(bytes(data))
fr.close()
fwbmp.close()