我得到了这个学校项目,其目标是拍照,应用 RLE 方法将其压缩为二进制文件 (.txt),并节省一些空间。在挣扎了3天之后,我现在遇到了麻烦。
def lecture_fichier_compresse():
f2=open("data2.txt",'rb') #open a binary file in reading mod.
im3=zeros((962,800,3),dtype="uint8") #create picture which size is 962*800 where every pixels are coded on 3 bytes.
d=0#pixel counter
i=0#lign indexation
j=0#column indexation
b=ord(f2.read(1))# the informations are read a first time
a=ord(f2.read(1))
rouge=ord(f2.read(1))
vert=ord(f2.read(1))
bleu=ord(f2.read(1))
while i!=im3.shape[0]: #as long as it doesn't reach the final lign
if d<=(a+b*255):
im3[i,j,0] = rouge
im3[i,j,1] = vert
im3[i,j,2] = bleu
d+=1
j+=1
if j==im3.shape[1]:
j=0
i+=1
else: #resets pixel counter and starts reading next informations
d=0
b=ord(f2.read(1))
a=ord(f2.read(1))
rouge=ord(f2.read(1))
vert=ord(f2.read(1))
bleu=ord(f2.read(1))
f2.close()
imsave("Copie_compresse.bmp",im3)
return im3
imshow(lecture_fichier_compresse());show()
当我执行 pgrm 时,它给了我标题中写的这个错误。这是我觉得不可能纠正的事情,因为它是用十六进制写的。
这里有更多信息:这里的像素不是使用字节编码的,就像我们通常对 .bmp 等格式所做的那样。在这里,RLE 将通过比较 3 个级别的颜色来搜索相同像素上的相同像素,并计算遇到它的次数。最后,它存储在比 RGB 字节多的两个字节中:a 和 b。a 是像素数。b 是 255 个像素堆栈的数量。(因为我将数据编码为 8 位并且图片通常大于 255*255 大小)