Convert RenderCapture to PIL image
#1
Hi,

I'm currently adapting Cees' Hue Ambilight Addon for XBMC with another color algorithm (k-mean vq). Then there's this tiny little problem: I'm rather new to python...

How do I convert an image from xbmc.RenderCapture.getImage to a standard PIL image (I need to use the "getcolors" method)?

When I use
Code:
size = (capture.getWidth(), capture.getHeight())
mode = 'RGBA'
img = Image.frombuffer(mode, size, capture.getImage(), 'raw', mode, 0, 1)
img.save("/storage/downloads/test.jpg")

I only get an black image with the correct dimensions as result.

Best,
Marc
Reply
#2
Ok the code above works, it's just XBMC which returns some black frames in the beginning (which lead to problems with my code).
Then again XBMC delivers BGRA instead of RGBA and PIL does not accept BGRA as input from buffer.
Is there a more elegant way to convert it to RGBA than the code below?

Code:
size = (capture.getWidth(), capture.getHeight())
img = Image.frombuffer('RGBA', size, capture.getImage(), 'raw', 'RGBA', 0, 1)  
imgStr = img.tostring()
img = Image.fromstring('RGBA', size, imgStr, 'raw', 'BGRA', 0, 1)
Reply
#3
I know it is 4 year old thread Smile but I can't resist replying...
 
Code:
screen = Screenshot(capture.getImage(), capture.getWidth(), capture.getHeight())
size = (screen.getWidth(), screen.getHeight())
ba = screen.pixels
ba[0::4], ba[2::4] = ba[2::4], ba[0::4]
img = Image.frombuffer('RGBA', size, ba, 'raw', 'RGBA', 0, 1)  


Key line being ba[0::4], ba[2::4] = ba[2::4], ba[0::4]
Reply

Logout Mark Read Team Forum Stats Members Help
Convert RenderCapture to PIL image0