我想讀二進制文件(如.BMP或.JPG),每個二進制數據轉換爲ASCII,做乘法(如ASCII * 2)和轉化回到二進制格式並寫入一個新文件。ASCII轉換成二進制和寫入二進制文件 - Python 3.x都有
我寫的程序如下
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()
,但我發現下面的錯誤
類型錯誤:「詮釋」不支持緩衝接口
注意:一旦我該溶液中,我將修改代碼跳過BMP或JPEG頭
MODIF ied
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()
你希望通過這樣做來達到什麼目的? –
我確實有所有像素都非常接近黑色(20最大)的要求,我需要做的這個像素倍增... – Sathya
這是不是東西,會使用更容易[枕頭?] (https://pypi.python.org/pypi/Pillow/2.0.0)(考慮到你正在處理圖像文件和像素數據...) –