从RGB到YCbCr的PIL图像转换产生4个通道,而不是3个,其行为类似于RGB

2024-09-25 08:38:08 发布

您现在位置:Python中文网/ 问答频道 /正文

好吧,这个标题是不言而喻的。我有一个图像文件,我想分别分为Y,Cb和Cr。打开文件后,将其从RGB(这是打开图像文件时的默认模式)转换为YCbCr,然后使用numpy.array()将其转换为数组,它生成了一个具有4个通道的二维数组,这不是我所期望的,如http://www.nmt.edu/tcc/help/pubs/pil/pil.pdf中的文档所示

以下是我在口译员中的工作:

ImageFile = Image.open('filePath', 'r')
ImageFile = ImageFile.convert('YCbCr')
ImageFileYCbCr = numpy.array(ImageFile)
ImageFileYCbCr

导致

array([[[103, 140, 133,  95],
    [140, 133,  91, 141],
    [132,  88, 141, 131],
    ..., 
    [129,  65, 146, 129],
    [ 64, 146, 130,  65],
    [146, 129,  64, 147]],

   [[129,  64, 147, 129],
    [ 62, 149, 130,  62],
    [149, 130,  62, 149],
    ..., 

当我把它分成不同的通道

ImageFileY = copy.deepcopy(ImageFileYCbCr) # to make a separate copy as array is immutable
ImageFileY[:,:,1] *= 0
ImageFileY[:,:,2] *= 0
ImageFileY[:,:,3] *= 0
ImageFileYOnly = Image.fromarray(ImageFileY)
ImageFileYOnly.show()

它产生了一个红色通道,就好像它是一个RGB。我怎样才能分别得到Y,Cb,Cr值?

编辑:Numpy version 1.3,Python 2.6linux回溯5


Tags: imagenumpypil图像文件rgb数组arraycr
2条回答

仅供参考,对于来自谷歌的未来人士:

现在似乎可以了。

作为参考,我有枕头6.1.0和numpy 1.17.0。做

img = np.array(Image.open(p).convert('YCbCr'))

给予相同的

img = Image.open(p).convert('YCbCr')
img = np.ndarray((img.size[1], img.size[0], 3), 'u1', img.tobytes())

与RGB不同。

https://mail.python.org/pipermail/image-sig/2010-October/006526.html

这是一只老虫子。纠正它

>>> import numpy
>>> import Image as im
>>> image = im.open('bush640x360.png')
>>> ycbcr = image.convert('YCbCr')

>>> B = numpy.ndarray((image.size[1], image.size[0], 3), 'u1', ycbcr.tostring())
>>> print B.shape
(360, 640, 3)
>>> im.fromarray(B[:,:,0], "L").show()

相关问题 更多 >