如何将图像拆分为 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 图像,并通过抑制我们不想要的波段来为每个波段创建像素数据。
RGB
到 R__
-> 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')
关于Python PIL 图像拆分为 RGB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51325224/