图像分解python,Python PIL图像拆分为RGB

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

Why does red.show() shows image in greyscale instead of red scale?

PS. The same situation using green.show() and blue.show().

解决方案

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 to R__ -> red.png

RGB to _G_ -> green.png

RGB to __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')

398845e7147c7f48efa3c528ca5a8ca7.png

c04579770222f5196be7471ec1f9c322.png

b1367519f02d21a5bc902ed2397f2625.png

KbSEw.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值