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

查看:33
本文介绍了如何使用 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() 函数的第一个参数是模式".但是,查看文档和谷歌搜索我无法找到有关该参数真正含义的详细信息(我相信它与色彩空间或类似的东西有关).有谁知道在哪里可以找到有关 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 位浮点像素)

PIL 还提供有限的支持一些特殊模式,包括 LA(L带 alpha), RGBX (真彩色带padding) 和 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天全站免登陆