PIL图像模式I为灰度?

2024-03-29 02:11:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用整数格式而不是(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决定如何将“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()

Tags: andofthe图像imagenewmode格式
1条回答
网友
1楼 · 发布于 2024-03-29 02:11:22

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

相关问题 更多 >