PIL Image to wx.Image and wx.Bitmap

by @jehiah on 2007-10-02 14:48
Filed under: All, Programming, Python, wxPython

Here is how you convert a PNG image from a PIL (the Python Image Library format) object to a wxPython Image (or Bitmap) while keeping the alpha transparency layer.

Why convert? so you can use that bitmap to draw onto something like FloatCanvas.

[python]
from PIL import Image
import wx

pilImage = Image.open('my.png')
image = wx.EmptyImage(pilImage.size[0],pilImage.size[1])
image.setData(pil.convert("RGB").tostring())
image.setAlphaData(pil.convert("RGBA").tostring()[3::4]

## use the wx.Image or convert it to wx.Bitmap
bitmap = wx.BitmapFromImage(image)

[/python]

Thats it. I hope that helps future googlers.

Subscribe via RSS ı Email
© 2016 - Jehiah Czebotar