Python 从RGB到YCbCr的PIL图像转换将产生4个通道,而不是3个通道,其行为类似于RGB
好吧,这个标题是不言自明的。我有一个图像文件,我想分为Y,Cb和Cr分别。打开文件后,将其从RGB(这是打开图像文件时的默认模式)转换为YCbCr,然后使用numpy.array()将其转换为数组,它生成了一个具有4个通道的2D数组,这与中的文档中的预期不同 以下是我在解释器中所做的:Python 从RGB到YCbCr的PIL图像转换将产生4个通道,而不是3个通道,其行为类似于RGB,python,python-imaging-library,Python,Python Imaging Library,好吧,这个标题是不言自明的。我有一个图像文件,我想分为Y,Cb和Cr分别。打开文件后,将其从RGB(这是打开图像文件时的默认模式)转换为YCbCr,然后使用numpy.array()将其转换为数组,它生成了一个具有4个通道的2D数组,这与中的文档中的预期不同 以下是我在解释器中所做的: ImageFile = Image.open('filePath', 'r') ImageFile = ImageFile.convert('YCbCr') ImageFileYCbCr = numpy.arra
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回溯5
这是Numpy的老毛病。纠正它
>>> 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())
与RGB不同。“…根据
http://www.google.com/url?sa=t&rct=j
…”该链接将我带到一个空白页面。看看url=
参数,我猜你是想链接到?看看这里:这可能会有帮助哦,你是对的Kevin。我会编辑它。谢谢。如果我在图像上调用getbands()
,它将返回('Y','Cb','Cr')
和getpixel((0,0))
返回一个包含3个成员的元组,表示3个带。错误一定是在转换为numpy
时发生的。马克,有什么建议吗?在PIL的更高版本(1.1.7,可能更早)中,tostring()
已被删除,因此第5行应该是:B=numpy.ndarray((image.size[1],image.size[0],3),'u1',ycbcr.tobytes())