好吧,标题是相当不言自明的。我有一个图像文件,我想把它分别分成Y, Cb和Cr。打开文件后,将其从RGB(这是打开图像文件时的默认模式)转换为YCbCr,然后使用numpy.array()将其转换为数组,结果是具有4个通道的2D数组,根据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版本1.3,Python 2.6 Linux Backtrack 5
https://mail.python.org/pipermail/image-sig/2010-October/006526.html
这是Numpy的一个老bug。修改
>>> 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()
仅供参考,对于未来来自谷歌的人:
参考,我有枕头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())