Search code examples
pythonpython-imaging-librarycolor-space

Save Different Channels of Ycbcr as seperate images | Python


I need to apply some transformations to the individual channels of the Ycbcr color space.

I have an tiff format image as the source and I need to convert it to ycbcr color space. I have not been able to successfully save the different channels as separate images. I have been only able to extract the luminescence channel using this code:

import numpy
import Image as im
image = im.open('1.tiff')
ycbcr = image.convert('YCbCr')

B = numpy.ndarray((image.size[1], image.size[0], 3), 'u1', ycbcr.tobytes())
im.fromarray(B[:,:,0], "L").show()

Can someone pls help.

Thank you


Solution

  • Here is my code:

    import numpy
    import Image as im
    image = im.open('1.tiff')
    ycbcr = image.convert('YCbCr')
    
    # output of ycbcr.getbands() put in order
    Y = 0
    Cb = 1
    Cr = 2
    
    YCbCr=list(ycbcr.getdata()) # flat list of tuples
    # reshape
    imYCbCr = numpy.reshape(YCbCr, (image.size[1], image.size[0], 3))
    # Convert 32-bit elements to 8-bit
    imYCbCr = imYCbCr.astype(numpy.uint8)
    
    # now, display the 3 channels
    im.fromarray(imYCbCr[:,:,Y], "L").show()
    im.fromarray(imYCbCr[:,:,Cb], "L").show()
    im.fromarray(imYCbCr[:,:,Cr], "L").show()