Python 3.11.5
wxPython 4.2.1
Pillow 10.1.0
I would like to make fancy windows (or panels) with Pillow. As you see below, I am having a trouble with mask (or transparency, alpha, …). I also tried “ConvertAlphaToMask” but it didn’t work. I found that the white corners of the inner rectangle is colored by its “BackgroundColour”. What should I do?
import wx
from PIL import Image, ImageDraw
def get_rounded_rectangle_image(width:int, height:int, fill:tuple, outline:tuple):
factor = 4 # for antialias
image = Image.new('RGBA', (factor*width, factor*height), (0, 0, 0, 0))
draw = ImageDraw.Draw(image)
draw.rounded_rectangle(
(0, 0, factor*width, factor*height),
fill = fill,
outline = outline,
width = 20,
radius = 200
)
image = image.resize((width, height), Image.Resampling.LANCZOS)
return image
class RoundedWindow(wx.Window):
def __init__(self, parent:wx.Window, fill:tuple, outline:tuple):
wx.Window.__init__(self, parent)
self.fill = fill
self.outline = outline
self.Bind(wx.EVT_SIZE, self.OnSize)
self.Bind(wx.EVT_PAINT, self.OnPaint)
def OnSize(self, event):
event.Skip()
self.Refresh()
def OnPaint(self, event):
dc = wx.PaintDC(self)
size = dc.GetSize()
pil_image = get_rounded_rectangle_image(size[0], size[1], self.fill, self.outline)
wx_image = wx.Image(size[0], size[1])
wx_image.SetData(pil_image.convert('RGB').tobytes())
wx_image.SetAlpha(pil_image.tobytes()[3::4])
pil_image.close()
bmp = wx_image.ConvertToBitmap()
dc.DrawBitmap(bmp, 0, 0, True)
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None)
panel = wx.Panel(self)
window1 = RoundedWindow(panel , (255, 0, 0, 255), (0, 0, 0, 255))
window2 = RoundedWindow(window1, (0, 255, 0, 255), (0, 0, 0, 255))
sz1 = wx.BoxSizer(wx.HORIZONTAL)
sz1.Add(window2, 1, wx.EXPAND|wx.ALL, 30)
window1.SetSizer(sz1)
sz = wx.BoxSizer(wx.HORIZONTAL)
sz.Add(window1, 1, wx.EXPAND|wx.ALL, 30)
panel.SetSizer(sz)
self.CenterOnScreen()
if __name__ == '__main__':
app = wx.App()
MainFrame().Show()
app.MainLoop()