用PIL和Python阅读原始图像

2024-04-26 18:47:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一张7GB的图片,我想用Python和PIL编写一个解码器。我从那里得到的图片上写着:

The data are formatted as a single-channel 16-bit integer (two byte, long) signed raw binary file, with big-endian byte order and no header.

Here's用于编写图像解码器的文档,但我没有太多用Python处理图像的经验,在这里我完全不知所措。在


Tags: the图像datapilaschannelbit图片
3条回答

您遇到的问题是,当PIL在提供的列表中仅支持8位像素时,文件是16位像素,而这封邮件中关于同一主题的16位little endian:

http://osdir.com/ml/python.image/2006-11/msg00021.html

那是4年前的事了,今年又提出了同样的话题:

http://mail.python.org/pipermail/image-sig/2010-April/006166.html

PIL v1.1.7的源代码中发现了这一点,请注意末尾关于一些“实验模式”的注释:

#                                   
# Modes supported by this version

_MODEINFO = {
    # NOTE: this table will be removed in future versions.  use
    # getmode* functions or ImageMode descriptors instead.

    # official modes
    "1": ("L", "L", ("1",)),
    "L": ("L", "L", ("L",)),
    "I": ("L", "I", ("I",)),
    "F": ("L", "F", ("F",)),
    "P": ("RGB", "L", ("P",)),
    "RGB": ("RGB", "L", ("R", "G", "B")),
    "RGBX": ("RGB", "L", ("R", "G", "B", "X")),
    "RGBA": ("RGB", "L", ("R", "G", "B", "A")),
    "CMYK": ("RGB", "L", ("C", "M", "Y", "K")),
    "YCbCr": ("RGB", "L", ("Y", "Cb", "Cr")),

    # Experimental modes include I;16, I;16L, I;16B, RGBa, BGR;15, and
    # BGR;24.  Use these modes only if you know exactly what you're
    # doing...

}

所以它看起来对16位图像有一些支持。在

UTSL-使用来源Luke

我处理很多原始图像,有些是16位的,有些是8位的灰度。在

我发现将原始图像加载到numpy数组中,然后将其转换为图像通常是有效的。在

如果字节顺序有问题,那么numpy数组.byteswap()命令应该在转换为PIL image对象之前处理它。在

此代码取自一个程序,该程序将8位原始图像读入PIL:

scene_infile = open(scene_infile_fullname,'rb')
scene_image_array = fromfile(scene_infile,dtype=uint8,count=rows*columns)
scene_image = Image.frombuffer("I",[columns,rows],
                                     scene_image_array.astype('I'),
                                     'raw','I',0,1)

在第二行中,从uint8更改为uint16将加载2字节而不是1字节的原始图像。在第三行中,图像被转换成4字节的整数,因为有些PIL例程似乎可以更好地处理这种类型。在

相关问题 更多 >