【问题标题】:TypeError: ord() expected a character, but string of length 0 foundTypeError: ord() 期望一个字符,但找到长度为 0 的字符串
【发布时间】:2017-12-26 21:37:09
【问题描述】:

我得到了这个学校项目,其目标是拍照,应用 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 个级别的颜色来搜索同一 lign 上的相同像素,并计算它遇到它的次数。最后,它存储在比 RGB 字节多的两个字节中:a 和 b。 a 是像素数。 b 是 255 个像素堆栈的数量。 (因为我将数据编码为 8 位并且图片通常大于 255*255 大小)

【问题讨论】:

  • 该错误表明您的一个f2.read(1) 调用返回了一个空字符串;当您到达文件末尾时会发生这种情况。
  • 那么,我该如何预防呢?
  • 没有实际的文件输入数据,很难说。您的文件包含的信息不如您预期的那么多。
  • 此外,我想分享文件数据2,向大家展示它是如何卡住的。有谁知道我可以存储该二进制文件的网站吗?
  • 您真的很想研究struct module,并避免如此多的单个字节读取和ord() 调用。使用该模块从 2 个字节中读取整数是微不足道的。

标签: python image ord


【解决方案1】:

您的文件太短,您的数据已用完。 file.read(..) 在找到 EOF 时返回一个空字符串。也许您剩余的字节数应该保留为 0?

您读取数据的效率极低。使用struct module 将数据解压缩为整数,并创建一个由 (R, G, B) 三元组组成的长 numpy 数组,然后重塑数据以形成图像矩阵:

import numpy as np
import struct

def lecture_fichier_compresse():
    width, height = 800, 962
    with open("data2.txt", 'rb') as rle_data:
        image = np.zeros((width * height, 3), dtype="uint8")
        pos = 0
        # decompress RLE data; 2 byte counter followed by 3 RGB bytes
        # read until the file is done
        for data in iter(lambda: rle_data.read(5), ''):
            if len(data) < 5:
                # not enough data to store another RLE RGB chunk
                break
            count, r, g, b = struct.unpack('>HBBB', data)
            image[pos:pos + count, :] = [[r, g, b]] * count
            pos += count
    # reshape continuous stream into an image matrix
    image = image.reshape((height, width, 3))
    imsave("Copie_compresse.bmp",im3)
    return image

也就是说,使用您的示例文件数据,我似乎无法构建一个连贯的图像。解压缩会生成 695046 像素的数据,这不会产生连贯的矩形图像(数字因子的最高短维度是 66,因此非常拉长)。即使最后允许丢失数据,我似乎也找不到任何能产生连贯图像的纵横比。结论是您的输入数据不完整或不正确。

【讨论】:

  • 谢谢,不过这对我来说太高级了。
  • @Oni_chan:它做了完全相同的工作,只是效率更高,并且考虑到文件不包含足够的数据。
  • 但是,既然您这么说,文件确实太短了。我只需要一种方法让它在遇到最后一包数据时停止读取
  • @Oni_chan:图片尺寸明显不对,图片行不对齐。
  • 它的尺寸是 962*800 垂直然后水平
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-23
  • 2021-10-08
  • 1970-01-01
相关资源
最近更新 更多