PIL图像模式我是灰度的吗

PIL图像模式我是灰度的吗

本文介绍了PIL图像模式我是灰度的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以整数格式而不是(R,G,B)格式指定图像的颜色.我假设必须根据"文档:

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:

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

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.

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

推荐答案

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

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

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

这篇关于PIL图像模式我是灰度的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 07:16