Skip to main content
 首页 » 编程设计

python之将 Ycbcr 的不同 channel 保存为单独的图像 | Python

2024年10月17日8txw1958

我需要对 Ycbcr 颜色空间的各个 channel 应用一些转换。

我有一个 tiff 格式的图像作为源,我需要将它转换为 ycbcr 色彩空间。我无法将不同的 channel 成功保存为单独的图像。我只能使用以下代码提取发光 channel :

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