gpt4 book ai didi

python - wx.StaticBitmap - 简单透明度( mask 、png、bmp?)

转载 作者:行者123 更新时间:2023-11-28 22:56:23 26 4
gpt4 key购买 nike

在持续失败 1 周后,我仍然无法完成一项简单的任务:加载带有 alpha channel 或白色背景的 png(在下面的示例中)并让它在 wx.StaticBitmap 中保持其透明度。

稍后我需要在 wx.panel 中使用它。它应该保持这样或类似的状态。

这是我的一种方法(白色背景):

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))

我读了很多关于这个主题的文章。我是垃圾。对不起。我想我在这里错过了一些明显的东西。 wx.Mask , Transparent images

更新:

我设法通过找到的示例做到了这一点 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)

这是从带有 alpha channel 的 PNG 创建透明的 wx.image,但是在 wx.StaticBitmap 中应该是透明的黑色。这让我很兴奋!!!请帮助!

要是我能设法在 wx.panel 中显示那个在正确位置具有透明度的图像就好了感谢社区!

最佳答案

正如在 python SO 聊天中讨论的那样:

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)

诀窍是用wx.PaintDC绘制所有重叠的图像。

此外,使用 wx.Bitmap 而不是 wx.Image(..., wx.BITMAP_TYPE_PNG).ConvertToBitmap() 从文件加载 PNG 更方便系统。

关于python - wx.StaticBitmap - 简单透明度( mask 、png、bmp?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15760525/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com