Reputation: 566
After 1 week of constant failure I'm still not able to do a simple task: Load a png with alpha channel or with white background (in example bellow) and have it maintain its transparency in wx.StaticBitmap.
This I need in a wx.panel later. It should stay like this or similar.
This is one of my approaches (white background):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.loc = wx.Image("intro/image.png",wx.BITMAP_TYPE_PNG).ConvertToBitmap()
z = wx.Mask(self.loc, wx.WHITE)
self.loc.SetMask(z)
self.locopic = wx.StaticBitmap(self, -1, self.loc, (0, 0))
I read tons on this topic. I'm rubbish. Sorry. I think I miss something obvious here. wx.Mask , Transparent images
Update:
I managed to get this far with the example found at WorkinWithImages:
import ImageConversions
...
puFilename = "intro/imagealpha.png"
pilImage = Image.open( puFilename )
pilImageWithAlpha = ImageConversions.WxImageFromPilImage( pilImage, createAlpha=True )
self.puWxBitmap = pilImageWithAlpha.ConvertToBitmap()
self.locopic = wx.StaticBitmap(self, -1, self.puWxBitmap)
This is creating the transparent wx.image from a PNG with alpha channel BUT in wx.StaticBitmap would have an ugly black colour where the transparency should be. This is driving me maaad!!! HELP PLEASE!
If only I would manage to display in the wx.panel that image with the transparency in the right place Thank you community!
Upvotes: 2
Views: 3923
Reputation: 1427
As discussed in the python SO chat:
class MyPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.loc = wx.Bitmap("intro/image.png")
def OnPaint(self, evt):
dc = wx.PaintDC(self)
dc.SetBackground(wx.Brush("WHITE"))
# ... drawing here all other images in order of overlapping
dc.DrawBitmap(self.loc, 0, 0, True)
The trick is to draw all overlapping images with the wx.PaintDC
.
Additionally it's more convenient to use wx.Bitmap
instead of wx.Image(..., wx.BITMAP_TYPE_PNG).ConvertToBitmap()
for loading PNGs from the file system.
Upvotes: 4