wxPython and PIL

Laszlo Nagy gandalf at designaproduct.biz
Thu Oct 19 12:03:29 EDT 2006


Odalrick wrote:
> I'm making a simple program to crop and scale images, essentially make
> thumbnails from a user defined subset of the image.
>
> I'm planning to use Python Image Library to crop and resize the images,
> mostly to make the resized smaller images look good.
>
> How do I display a PIL image with wxPython?
>
>   
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

def imagetopil(image):
    """Convert wx.Image to PIL Image."""
    pil = Image.new('RGB', (image.GetWidth(), image.GetHeight()))
    pil.fromstring(image.GetData())
    return pil
 

Best,

   Laszlo




More information about the Python-list mailing list