[Image-SIG] How to convert from transparent PIL to transparent wx.Image ?

Laszlo Zsolt Nagy gandalf at designaproduct.biz
Sun Sep 18 11:29:33 CEST 2005


>But this does not work if I try to use 'RGBA' mode. Do you know a solution?
>
>  
>
I found one. Here it is:

def piltoimage(pil,alpha=True):
    """Convert PIL Image to wx.Image."""
    if alpha:
        image = apply( wx.EmptyImage, pil.size )
        image.SetData( pil.convert( "RGB").tostring() )
        image.SetAlphaData(pil.convert("RGBA").tostring()[3::4])
    else:
        image = wx.EmptyImage(pil.size[0], pil.size[1])
        new_image = pil.convert('RGB')
        data = new_image.tostring()
        image.SetData(data)
    return image



More information about the Image-SIG mailing list