Python Imaging: YCbCr problems

Posted by daver on Stack Overflow See other posts from Stack Overflow or by daver
Published on 2010-05-09T08:48:57Z Indexed on 2010/05/09 8:58 UTC
Read the original article Hit count: 296

Filed under:
|

Hi, I'm doing some image processing in Python using PIL, I need to extract the luminance layer from a series of images, and do some processing on that using numpy, then put the edited luminance layer back into the image and save it. The problem is, I can't seem to get any meaningful representation of my Image in a YCbCr format, or at least I don't understand what PIL is giving me in YCbCr. PIL documentation claims YCbCr format gives three channels, but when I grab the data out of the image using np.asarray, I get 4 channels. Ok, so I figure one must be alpha.

Here is some code I'm using to test this process:

import Image as im
import numpy as np    
pengIm = im.open("Data\\Test\\Penguins.bmp")
yIm = pengIm.convert("YCbCr")
testIm = np.asarray(yIm)

grey = testIm[:,:,0]
grey = grey.astype('uint8')
greyIm = im.fromarray(grey, "L")
greyIm.save("Data\\Test\\grey.bmp")

I'm expecting a greyscale version of my image, but what I get is this jumbled up mess:

http://i.imgur.com/zlhIh.png

Can anybody explain to me where I'm going wrong? The same code in matlab works exactly as I expect.

© Stack Overflow or respective owner

Related posts about python

Related posts about pil