PIL图像模式I是灰度?

时间:2021-09-08 22:56:01

I'm trying to specify the colours of my image in Integer format instead of (R,G,B) format. I assumed that I had to create an image in mode "I" since according to the documentation:

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

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)
  • 1(1位像素,黑白,每字节存储1个像素)
  • L (8-bit pixels, black and white)
  • L(8位像素,黑白)
  • P (8-bit pixels, mapped to any other mode using a colour palette)
  • P(8位像素,使用调色板映射到任何其他模式)
  • RGB (3x8-bit pixels, true colour)
  • RGB (3x8位像素,真色)
  • RGBA (4x8-bit pixels, true colour with transparency mask)
  • RGBA (4x8位像素,带有透明遮罩的真彩色)
  • CMYK (4x8-bit pixels, colour separation)
  • CMYK (4x8位像素,分色)
  • YCbCr (3x8-bit pixels, colour video format)
  • YCbCr (3x8位像素,彩色视频格式)
  • I (32-bit signed integer pixels)
  • I(32位符号整数像素)
  • F (32-bit floating point pixels)
  • F(32位浮点像素)

However this seems to be a grayscale image. Is this expected? Is there a way of specifying a coloured image based on a 32-bit integer? In my MWE I even let PIL decide how to convert "red" to the "I" format.

然而,这似乎是一个灰度图像。这是预期的吗?是否有一种方法可以指定基于32位整数的彩色图像?在我的MWE中,我甚至让PIL决定如何将“red”转换为“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()

1 个解决方案

#1


4  

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

是否有一种方法可以指定基于32位整数的彩色图像?

Yes, use the RGB format for that, but instead use an integer instead of "red" as the color argument:

是的,使用RGB格式,而是使用整数而不是“红色”作为颜色参数:

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

#1


4  

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

是否有一种方法可以指定基于32位整数的彩色图像?

Yes, use the RGB format for that, but instead use an integer instead of "red" as the color argument:

是的,使用RGB格式,而是使用整数而不是“红色”作为颜色参数:

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