PIL Image Converts from RGB to YCbCr. Results in 4 channels instead of 3 and run as RGB

Well, the name is pretty clear. I have an image file that I want to split into Y, Cb and Cr respectively. After opening the file, convert it from RGB (which is the default mode when opening the image file) to YCbCr, and then turn it into an array using numpy.array (), the result is a two-dimensional array with 4 channels, which I did not expect, as according to the documentation at http://www.nmt.edu/tcc/help/pubs/pil/pil.pdf

Here is what I do in the translator:

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

Led to

 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], ..., 

And when I divide it into channels

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

This led to the appearance of a red channel, as if it were RGB. How can I get the values โ€‹โ€‹of Y, Cb, Cr, respectively?

UPDATE: Numpy version 1.3, Python 2.6 Linux Backtrack 5

+6
source share
2 answers

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

This is an old mistake with Numpy. To fix it

 >>> 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() 
+8
source

For your information, for future Google users:

It seems to work now.

For reference, I have Pillow 6.1.0 and numpy 1.17.0. By doing

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

produces the same as

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

and not the same as RGB.

0
source

Source: https://habr.com/ru/post/1309245/


All Articles