本文介绍了Python PIL图像拆分为RGB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将图像拆分为RGB颜色,为什么split()
功能不起作用?
How to split image to RGB colors and why doesn't split()
function work?
from PIL import Image
pil_image = Image.fromarray(some_image)
red, green, blue = pil_image.split()
red.show()
为什么red.show()
以灰度而不是红色显示图像?
Why does red.show()
shows image in greyscale instead of red scale?
PS.使用green.show()
和blue.show()
的情况相同.
PS. The same situation using green.show()
and blue.show()
.
推荐答案
我创建了一个获取RGB图像的脚本,并通过抑制我们不想要的波段来创建每个波段的像素数据.
I've created a script that takes an RGB image, and creates the pixel data for each band by suppressing the bands we don't want.
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!