PIL Image to wx.Image and wx.Bitmap
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.
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)
Thats it. I hope that helps future googlers.
2 years, 8 months ago
Thanks, yes it did help a future Googler. But here’s a version that will actually execute without complaint:
from PIL import Image
import wx
myApp = wx.App()
mypilimage = Image.open(‘my.png’)
mywximage = wx.EmptyImage(mypilimage.size[0],mypilimage.size[1])
mywximage.SetData(mypilimage.convert(“RGB”).tostring())
mywximage.SetAlphaData(mypilimage.convert(“RGBA”).tostring()[3::4])
use the wx.Image or convert it to wx.Bitmap
mywxbitmap = wx.BitmapFromImage(mywximage)
print “done”
2 years, 1 month ago
Any idea how to do a reverse conversion?
From wxBitmap (or wxImage) to PIL image.
1 year, 6 months ago
Thanks. I wasn’t getting the information on:
http://wiki.wxpython.org/index.cgi/WorkingWithImages
to work, but that wx.BitmapFromImage(mywximage) saved my day. Thanks!