PIL 图片模式 I 是灰度?

PIL Image mode I is grayscale?

我正在尝试以整数格式而不是 (R,G,B) 格式指定图像的颜色。我假设我必须以“I”模式创建图像,因为根据 documentation:

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-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)

然而,这似乎是一张灰度图像。这是预期的吗?有没有办法根据 32 位整数指定彩色图像?在我的 MWE 中,我什至让 PIL 决定如何将“红色”转换为“I”格式。


MWE

from PIL import Image

ImgRGB=Image.new('RGB', (200,200),"red") # create a new blank image
ImgI=Image.new('I', (200,200),"red") # create a new blank image
ImgRGB.show()
ImgI.show()

Is there a way of specifying a coloured image based on a 32-bit integer?

是的,为此使用 RGB 格式,但使用整数而不是 "red" 作为颜色参数:

from PIL import Image

r, g, b = 255, 240, 227
intcolor = (b << 16 ) | (g << 8 ) | r                                       
print intcolor # 14938367
ImgRGB = Image.new("RGB", (200, 200), intcolor)
ImgRGB.show()