将 Ycbcr 的不同频道保存为单独的图像 | Python

Save Different Channels of Ycbcr as seperate images | Python

我需要对 Ycbcr 颜色的各个通道应用一些转换 space。

我有一个 tiff 格式的图像作为源,我需要将其转换为 ycbcr 颜色 space。我无法将不同的频道成功保存为单独的图像。我只能使用此代码提取发光通道:

import numpy
import Image as im
image = im.open('1.tiff')
ycbcr = image.convert('YCbCr')

B = numpy.ndarray((image.size[1], image.size[0], 3), 'u1', ycbcr.tobytes())
im.fromarray(B[:,:,0], "L").show()

有人可以帮忙吗。

谢谢

这是我的代码:

import numpy
import Image as im
image = im.open('1.tiff')
ycbcr = image.convert('YCbCr')

# output of ycbcr.getbands() put in order
Y = 0
Cb = 1
Cr = 2

YCbCr=list(ycbcr.getdata()) # flat list of tuples
# reshape
imYCbCr = numpy.reshape(YCbCr, (image.size[1], image.size[0], 3))
# Convert 32-bit elements to 8-bit
imYCbCr = imYCbCr.astype(numpy.uint8)

# now, display the 3 channels
im.fromarray(imYCbCr[:,:,Y], "L").show()
im.fromarray(imYCbCr[:,:,Cb], "L").show()
im.fromarray(imYCbCr[:,:,Cr], "L").show()

只需使用.split()方法将图像分割成不同的通道(在PIL中称为bands)。无需使用 numpy。

(y, cb, cr) = ycbcr.split()
# y, cb and cr are all in "L" mode. 

完成转换后,使用PIL.Image.merge()再次组合它们。

ycbcr2 = im.merge('YCbCr', (y, cb, cr))