Python PIL 图像拆分为 RGB

标签 python image image-processing python-imaging-library python-3.6

如何将图像拆分为 RGB 颜色以及为什么 split() 函数不起作用?

from PIL import Image
pil_image = Image.fromarray(some_image)
red, green, blue = pil_image.split()
red.show()

为什么 red.show() 以灰度而不是红色显示图像?

附言。使用 green.show()blue.show() 的情况相同。

最佳答案

我创建了一个脚本,它获取 RGB 图像,并通过抑制我们不想要的波段来为每个波段创建像素数据。

RGBR__ -> red.png

RGB_G_ -> green.png

RGB__B -> blue.png

from PIL import Image

img = Image.open('ra.jpg')
data = img.getdata()

# Suppress specific bands (e.g. (255, 120, 65) -> (0, 120, 0) for g)
r = [(d[0], 0, 0) for d in data]
g = [(0, d[1], 0) for d in data]
b = [(0, 0, d[2]) for d in data]

img.putdata(r)
img.save('r.png')
img.putdata(g)
img.save('g.png')
img.putdata(b)
img.save('b.png')

enter image description here enter image description here enter image description here enter image description here

关于Python PIL 图像拆分为 RGB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51325224/

相关文章:

java - Android:模糊位图,同时确保文本可读性

python - 使用Dlib/python检测额头点

image-processing - 实时跟踪多个简单物体的算法

python - 将像素从图像的裁剪部分转换为原始图像 OpenCV

python - Pylint 与 pytorch : Is there a way to tell pylint to look for module in different place?

python - 导入 mysql.connector ImportError : No module named connector

python - 对于 sys.argv[1 :]: argument list too long 中的 fi

jquery - 计算div的宽度并通过图像分布

c# - 应用下载的纹理后,PNG 文件在 Unity 中增长并通过 EncodeToPNG 转换回 PNG

python - 使用 Mechanize 和 python 的站点抓取下拉列表