Recreating image from Y Cb Cr numpy arrays Python

Viewed 284

I have 3 numpy arrays with Y, Cb and Cr. I was able to show them individually and now I want to merge them and show the result Image. I tried this:

Y, Cb, Cr = somefunction() //returns 3 numpy.ndarrays

print(Y.shape)  //(160, 160)
print(Y.dtype)  //float64
print(Cb.shape) //(160,160)
print(Cb.dtype) //float32
print(Cr.shape) //(160,160)
print(Cr.dtype) // float32

img_Y = im.fromarray(Y, mode=None)
img_Cb = im.fromarray(Cb, mode=None)
img_Cr = im.fromarray(Cr, mode=None)

im.merge('YCbCr', (img_Y, img_Cb, img_Cr))

but I get this error:

    im.merge('YCbCr', (img_Y, img_Cb, img_Cr))
  File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\Image.py", line 2957, in merge
    raise ValueError("mode mismatch")
ValueError: mode mismatch
1 Answers

You cannot merge a float64 Y-channel with float32 Cb/Cr-channels, and PIL doesn't support any colourspaces (modes) with more than a one 32-bit float channel.

So, you will need to scale your values to the range 0..255 and convert to uint8, as per the link above, in order to go to a YCbCr colourspace.

Related