Python PIL 图像模式我是灰度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32192671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:12:02  来源:igfitidea点击:

PIL Image mode I is grayscale?

pythonpython-imaging-library

提问by Miguel

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

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

  • 1(1位像素,黑白,每字节一个像素存储)
  • L(8 位像素,黑白)
  • P(8 位像素,使用调色板映射到任何其他模式)
  • RGB(3x8 位像素,真彩色)
  • RGBA(4x8 位像素,带透明蒙版的真彩色)
  • CMYK(4x8 位像素,分色)
  • YCbCr(3x8 位像素,彩色视频格式)
  • I(32 位有符号整数像素)
  • 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 决定如何将“红色”转换为“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()

采纳答案by Michiel Overtoom

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