如何使用PIL读取原始图像? [英] How to read a raw image using PIL?

查看:784
本文介绍了如何使用PIL读取原始图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个原始图像,其中每个像素对应一个16位无符号整数。我正在尝试使用PIL Image.fromstring()函数读取,如下面的代码所示:

I have a raw image where each pixel corresponds to a 16 bits unsigned integer. I am trying to read using the PIL Image.fromstring() function as in the following code:

if __name__ == "__main__":
    if (len(sys.argv) != 4):
        print 'Error: missing input argument'
        sys.exit()

    file = open(sys.argv[1], 'rb')
    rawData = file.read()
    file.close()

    imgSize = (int(sys.argv[2]), int(sys.argv[3]))

    # Use the PIL raw decoder to read the data.
    #   - the 'F;16' informs the raw decoder that we are reading a little endian, unsigned integer 16 bit data.
    img = Image.fromstring('L', imgSize, rawData, 'raw', 'F;16')

    im.save('out.png')

PIL文档通知fromstring()函数的第一个参数是'mode'。然而,看文档和谷歌搜索,我无法找到关于该论点的真正含义的细节(我相信它与色彩空间或类似的东西有关)。有谁知道我在哪里可以找到关于fromstring()函数的更详细的参考以及mode参数的含义?

The PIL documentation informs that the first argument of the fromstring() function is 'mode'. However, looking at the documentation and googling I wasn't able to find details about what that argument really means (I believe that it is related to the color space or something like that). Does anyone knows where I can find a more detailed reference about the fromstring() function and what the mode argument means?

推荐答案

具体文件见 http://effbot.org/imagingbook/concepts.htm


模式



图像模式定义类型
和深度图像中的一个像素。
当前版本支持以下
标准模式:

Mode

The mode of an image defines the type and depth of a pixel in the image. The current release supports the following standard modes:


  • 1(1位像素,黑白,每字节存储一个像素)

  • L(8位像素,黑白)

  • P(8位像素,映射到任意使用调色板的其他模式)

  • RGB(3x8位像素,真彩色)

  • RGBA(4x8位像素,真彩色透明)掩码)

  • CMYK(4x8位像素,分色)

  • YCbCr(3x8位像素,彩色视频格式)

  • I(32位有符号整数像素)

  • F(32位浮点像素)

  • 1 (1-bit pixels, black and white, stored with one pixel per byte)
  • L (8-bit pixels, black and white)
  • P (8-bit pixels, mapped to any other mode using a colour palette)
  • RGB (3x8-bit pixels, true colour)
  • RGBA (4x8-bit pixels, true colour with transparency mask)
  • CMYK (4x8-bit pixels, colour separation)
  • YCbCr (3x8-bit pixels, colour video format)
  • I (32-bit signed integer pixels)
  • F (32-bit floating point pixels)

PIL还为
a少数特殊模式提供有限支持,包括LA(L
with alpha),RGBX(真彩色带
填充)和RGBa(真彩色)与
预乘alpha)。

PIL also provides limited support for a few special modes, including LA (L with alpha), RGBX (true colour with padding) and RGBa (true colour with premultiplied alpha).

这篇关于如何使用PIL读取原始图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆