Reading raw imagery with PIL and Python

2020-06-29 06:06发布

I have a 7GB image I got from NASA and I wanted to write a decoder using Python and PIL. The page from where I got the image states the following:

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 the documentation for writing an image decoder, but I don't have much experience dealing with images in Python and I'm at a total loss here.

3条回答
一纸荒年 Trace。
2楼-- · 2020-06-29 06:47

I deal a lot with raw images, some in 16bit and some in 8bit grayscale.

I have found that loading the raw image into a numpy array, then converting it to an image usually works.

If there is a problem with the byte order, then the numpy array.byteswap() command should take care of it before conversion to the PIL image object.

This code is taken from a program that works to read 8-bit raw images into 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)

In the second line, changing from uint8 to uint16 will load a 2-byte instead of a 1-byte raw image. In the third line, the image is cast into a 4-byte integer, as some of the PIL routines seem to work better with this type.

查看更多
▲ chillily
3楼-- · 2020-06-29 06:55

The problem you are encountering is that the files are 16-bit pixels when PIL only supports 8-bit pixels in the provided list, and 16-bit little endian from this mail on the same topic:

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

That was 4 years ago, and here is the same topic raised again this year:

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

查看更多
趁早两清
4楼-- · 2020-06-29 06:56

Found this looking in .../Imaging-1.1.7/PIL/Image.py of the sources for PIL v1.1.7, note the comment at the end about some "Experimental modes":

# --------------------------------------------------------------------
# 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...

}

So it looks like there's some support in it for 16-bit images.

UTSL - Use the Source Luke

查看更多
登录 后发表回答